Qt 使用样式表时,子类QLabel中的文本换行不起作用
我已将QLabel子类化如下:Qt 使用样式表时,子类QLabel中的文本换行不起作用,qt,Qt,我已将QLabel子类化如下: class TestLabel: public QLabel { Q_OBJECT public: TestLabel(QWidget *parent = 0):QLabel(parent){} TestLabel(const QString& text, QWidget *parent = 0):QLabel(text, parent){} protected: void paintEvent(QPaintEvent *
class TestLabel: public QLabel
{
Q_OBJECT
public:
TestLabel(QWidget *parent = 0):QLabel(parent){}
TestLabel(const QString& text, QWidget *parent = 0):QLabel(text, parent){}
protected:
void paintEvent(QPaintEvent *paint)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
style()->drawItemText(&p,this->rect(),Qt::AlignLeft, (this->palette()), true, this->text());
}
};
TestLabel {
font-size: 16px;
color: rgb(127, 127, 127);
}
我已按如下方式设置样式表:
class TestLabel: public QLabel
{
Q_OBJECT
public:
TestLabel(QWidget *parent = 0):QLabel(parent){}
TestLabel(const QString& text, QWidget *parent = 0):QLabel(text, parent){}
protected:
void paintEvent(QPaintEvent *paint)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
style()->drawItemText(&p,this->rect(),Qt::AlignLeft, (this->palette()), true, this->text());
}
};
TestLabel {
font-size: 16px;
color: rgb(127, 127, 127);
}
我正在尝试使用以下方式包装文本:tlab->setWordWrap(true)代码>
在覆盖paintEvent之前,文本已正确包装。但是现在,文本不再被包装。说明“文本根据标志对齐和包装。”,但如何传入两个标志(它们都是不同的枚举)?确定。我得到了答案。我看错班级了。我在看QTExtraption::WordWrap
我需要使用Qt::TextWordWrap作为Qt::AlignLeft | Qt::TextWordWrap