[SOLVED] Extending widgets
-
@nitzan
Yes, just a normal cpp and h file.
Just make sure you type ClassName (of your widget) correctly as it don't validate it :)wrote on 21 Sept 2015, 13:16 last edited bythanks!
that worked well, except for one problem.
calling thesetTest
method seems to have no effect at all..
I'm doing this:MyWidget::MyWidget(QWidget* parent) : QLabel(parent) { qDebug() << "MyWidget ctor"; this->setText("hey there"); this->setAttribute(Qt::WA_Hover); } bool MyWidget::event(QEvent* event) { switch (event->type()) { case QEvent::HoverEnter: this->setStyleSheet("color: rgb(255, 0, 0)"); break; case QEvent::HoverLeave: this->setStyleSheet("color: rgb(0, 0, 0)"); break; default: return QLabel::event(event); } return true; }
And the label has no text in it.
Any idea why? -
@nitzan said:
this->setText(..)
Have you tried SetText from outside, like via a button ?
Sometimes I get fooled by stuff overwritten by the ui->setup which is run after
your constructor. -
@nitzan said:
this->setText(..)
Have you tried SetText from outside, like via a button ?
Sometimes I get fooled by stuff overwritten by the ui->setup which is run after
your constructor. -
@mrjj
but that creates coupling i would prefer to avoid..
is there anyway to have that in the same class in a way that will be called after the ui->setup?@nitzan
It was just for testing.Have you tried to set it on the label you promote. ?
And not call SetText at all in constructor.I get the text here. (from designer)
-
@nitzan
It was just for testing.Have you tried to set it on the label you promote. ?
And not call SetText at all in constructor.I get the text here. (from designer)
wrote on 21 Sept 2015, 13:40 last edited by@mrjj
callingsetText
from outside the class works well, even if I do it from theevent
method it works, but not in the ctor.I can't set the text from the designer as the actual text I'm trying to put there is from font awesome (using QtAwesome), and so the actual code is:
UserWidget::UserWidget(QWidget* parent) : QLabel(parent) { qDebug() << "UserWidget ctor"; this->setText(QString(QChar(fa::user)).append(" login")); this->setAttribute(Qt::WA_Hover); }
-
If you don't want the text overwritten by the text from designer then instead of promoting QLabel to your class promote a plain QWidget. It won't have the
Text
property in the editor so the generatedsetupUi
code won't callsetText
and the value set in your constructor will be preserved. -
If you don't want the text overwritten by the text from designer then instead of promoting QLabel to your class promote a plain QWidget. It won't have the
Text
property in the editor so the generatedsetupUi
code won't callsetText
and the value set in your constructor will be preserved.wrote on 21 Sept 2015, 13:54 last edited by@Chris-Kawa
Sounds like a workaround, but I would like to understand why it's needed.As the ctor of
QLabel
is called before the code in my widget is executed, then thesetupUi
code should be executed before my code as well.
Unless I'm missing something? -
Well its the setupui for the mainform/dialog.
It creates the Qlabel and set its text.
So calls setText after the constructor. -
It's not a workaround. You should always promote from the "lowest" widget of which functionality you want to preserve. In this case you don't want to have the label's ability to edit text in the designer so you promote from a widget, not label.
As the ctor of QLabel is called before the code in my widget is executed, then the setupUi code should be executed before my code as well. Unless I'm missing something?
The code of the window widget's constructor usually looks something like this:
Foo::Foo(QWidget* parent) : QWidget(parent) { ui->setupUi(this); }
which "expands" to something like this:
Foo::Foo(QWidget* parent) : QWidget(parent) { ... ui->someLabel = new YourLabel(this); ui->someLabel->setText(whateverWasSetinTheDesigner); ... }
so as you can see whatever you set in the constructor is immediately overwritten by what was set in the designer.
If you promote from a QWidget the designer doesn't "know" it's a label and it won't generate thesetText()
call. -
It's not a workaround. You should always promote from the "lowest" widget of which functionality you want to preserve. In this case you don't want to have the label's ability to edit text in the designer so you promote from a widget, not label.
As the ctor of QLabel is called before the code in my widget is executed, then the setupUi code should be executed before my code as well. Unless I'm missing something?
The code of the window widget's constructor usually looks something like this:
Foo::Foo(QWidget* parent) : QWidget(parent) { ui->setupUi(this); }
which "expands" to something like this:
Foo::Foo(QWidget* parent) : QWidget(parent) { ... ui->someLabel = new YourLabel(this); ui->someLabel->setText(whateverWasSetinTheDesigner); ... }
so as you can see whatever you set in the constructor is immediately overwritten by what was set in the designer.
If you promote from a QWidget the designer doesn't "know" it's a label and it won't generate thesetText()
call.wrote on 21 Sept 2015, 14:12 last edited byAlright, promoting from
QWidget
indeed did the job and now it works well!
Thanks for the both of you.
14/14