I have subclassed QLabel as follows:
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());
}
};
I have setup the stylesheet as follows:
TestLabel {
font-size: 16px;
color: rgb(127, 127, 127);
}
I am trying to wrap text by using: tlab->setWordWrap(true);
Before overriding paintEvent, the text was being wrapped properly. But now, the text is no longer wrapped. The documentation says that the “The text is aligned and wrapped according to flags.”, but how do I pass in both flags(both of them are distinct enums)?
OK. I got the answer. I was looking at the wrong class. I was looking at QTextOption::WordWrap.
I needed to use Qt::TextWordWrap as
Qt::AlignLeft | Qt::TextWordWrap