88 lines
2.2 KiB
C++
88 lines
2.2 KiB
C++
|
|
#include "formtableitem.h"
|
|||
|
|
#include "ui_formtableitem.h"
|
|||
|
|
#include <QPainter>
|
|||
|
|
|
|||
|
|
FormTableItem::FormTableItem(QWidget *parent) :
|
|||
|
|
QWidget(parent),
|
|||
|
|
ui(new Ui::FormTableItem)
|
|||
|
|
{
|
|||
|
|
ui->setupUi(this);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
FormTableItem::~FormTableItem()
|
|||
|
|
{
|
|||
|
|
delete ui;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void FormTableItem::paintEvent(QPaintEvent* event)
|
|||
|
|
{
|
|||
|
|
QPainter painter(this);
|
|||
|
|
QRect rect = this->rect();
|
|||
|
|
|
|||
|
|
// 获取RGB值
|
|||
|
|
int red = m_backColor.red();
|
|||
|
|
int green = m_backColor.green();
|
|||
|
|
int blue = m_backColor.blue();
|
|||
|
|
if(red==255 && green==255 && blue==255)
|
|||
|
|
{
|
|||
|
|
//背景(透明)
|
|||
|
|
painter.fillRect(rect.left(), rect.top(), rect.width(), rect.height(), QColor(255, 255, 255, 0));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//背景
|
|||
|
|
painter.fillRect(rect.left(), rect.top(), rect.width(), rect.height(), m_backColor);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if(!m_pixmap.isNull())
|
|||
|
|
{
|
|||
|
|
painter.drawPixmap(0, 0, m_pixmap);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// //测试画框
|
|||
|
|
// painter.setPen(Qt::red);
|
|||
|
|
// QRect rectRound(rect.left(), rect.top(), rect.width(), rect.height());
|
|||
|
|
// painter.drawRect(rectRound);
|
|||
|
|
|
|||
|
|
//
|
|||
|
|
painter.setBrush(Qt::NoBrush); // 确保文字不被填充色遮挡
|
|||
|
|
painter.setFont(m_txtFont);
|
|||
|
|
painter.setPen(m_txtColor);
|
|||
|
|
|
|||
|
|
if (m_strShowTxt.length()>0)
|
|||
|
|
{
|
|||
|
|
QRect rt = rect;
|
|||
|
|
QString text = "";
|
|||
|
|
bool bVer = false;
|
|||
|
|
if (!m_bVerticaDrawing)
|
|||
|
|
{
|
|||
|
|
QFontMetrics fm1(m_txtFont);
|
|||
|
|
QRect textRect = fm1.boundingRect(m_strShowTxt);
|
|||
|
|
if (textRect.width() > rt.width())
|
|||
|
|
{
|
|||
|
|
bVer = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (m_bVerticaDrawing || bVer)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < m_strShowTxt.size(); i++) {
|
|||
|
|
if (!i)text += m_strShowTxt.at(i);
|
|||
|
|
else {
|
|||
|
|
text += "\n";
|
|||
|
|
text += m_strShowTxt.at(i);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
// rt.setX(rect.left());
|
|||
|
|
// rt.setY(rect.top() + rect.height() / 3);
|
|||
|
|
// rt.setWidth(rect.width());
|
|||
|
|
// rt.setHeight(rect.height() / 3);
|
|||
|
|
text = m_strShowTxt;
|
|||
|
|
}
|
|||
|
|
//文字
|
|||
|
|
painter.drawText(rt, Qt::AlignCenter, text);
|
|||
|
|
}
|
|||
|
|
}
|