Class TitleBar
{
Private:
Theme *_theme;
QLabel *_textLabel;
QPushButton *_button;
QString _text;
Public:
void TitleBar::setText(QString text)
{
_text = text;
}
void TitleBar::showEvent(QShowEvent *event)
{
if(_button->isVisible())
{
_textLabel->setFixedWidth(this->width()-_button->width()-MARGIN*3);
}
else
{
_textLabel->setFixedWidth(this->width()-MARGIN*2);
}
_textLabel->setText(_textLabel->fontMetrics().elidedText(_text, Qt::ElideRight, _textLabel->width(), Qt::TextSingleLine));
}
};
_titleBar->setText(location->getTitle());
问题:_titleBar->setText(location->getTitle());后titleBar上面的字符没有显示。
原因:setText方法只是把字符串赋值给了TitleBar的成员变量_text。要想显示必须调用_textLabel的setText方法。
方法:触发titleBar的showevent事件。 Titlebar->hide() Titlebar->show()