How to 'refer' to a QLabel added to a push button?
-
void Main::on_toolButton_clicked() { int width = ui.toolButton->width(); int height = ui.toolButton->height(); QFont qFont = this->font(); qFont.setFamily("SF Pro Display"); qFont.setPixelSize(15); QString qText = "hello world"; QLabel* label = new QLabel(qText, ui.toolButton); label->setFont(qFont); label->setGeometry(0, 0, width, height); label->setAlignment(Qt::AlignCenter); label->show(); }
Suppose i need to edit the properties of this QLabel added to the
ui.toolButton
in another function, how do I refer to it? -
Hi
findChildren is what we have.You could keep a std::map with the labels to look upon demand.
That is more efficient than for loop if many other widgets on the form.Note that @TheLumbee show to loop one button so its pretty fast anyway.
-
This solution just iterates through each child of the toolButton and then once a label is found, it returns it. If there are multiple labels, it will just return the first. In that case, you can just compare the text that you're looking for.
QLabel* myLabel = nullptr; for (auto child : ui.toolButton.children()) { if (qobject_cast<QLabel*>(child)) { myLabel = qobject_cast<QLabel*>(child); break; } }
-
Hi
You could also just store the pointer to the label.QLabel* label = new QLabel(qText, ui.toolButton); ---> label = new QLabel(qText, ui.toolButton); and have QLabel* label; // in main.h in its class def.
oh I just saw its inside on_toolButton_clicked
so you will make a new label every time you click on it ?
How will that work if you click the same button multiple times? -
@mrjj
If i set an 'unique name' to the label likelabel->setObjectName("label1");
How do i quickly get this label using her name?
I know only this:
QList<QLabel*> labels = this->findChildren<QLabel*>("label1"); QLabel label = labels[0];
But it search in all existing labels.
Similar to @TheLumbee answer, but as I know the label object name there's any 'quickly' way to get it other than using a for loop?
obs: Sorry don't speak English well
-
Hi
findChildren is what we have.You could keep a std::map with the labels to look upon demand.
That is more efficient than for loop if many other widgets on the form.Note that @TheLumbee show to loop one button so its pretty fast anyway.
-
Hi
Just for information. you can also store the label in a property on the button.
QPushButton *AsButton(QObject *w) // just a helper { return qobject_cast<QPushButton *>(w); } void MainWindow::on_pushButton_pressed() { // add label QPushButton *btn = qobject_cast<QPushButton *>(sender()); if (btn) { if (btn->property("mylabel").isNull()) { // only add if none already QLabel *label = new QLabel(btn); label->setText("NAAAH"); label->move(0, 0); label->show(); btn->setProperty("mylabel", QVariant::fromValue(label)); } } } void MainWindow::on_pushButton_released() { // access label QPushButton *btn = AsButton(sender()); QVariant vari = btn->property("mylabel"); QLabel *label = qvariant_cast<QLabel *>(vari); if (label) label->setText("yeah"); }
-
@mrjj thank you, this is very helpful!
btn->setProperty("mylabel", QVariant::fromValue(label));
I can store data of any type like this?static std::vector<std::string> vec{ "test", "test2"}; btn->setProperty("propx", QVariant::fromValue(vec));
then somewhere
QVariant vari = btn->property("propx"); auto vect = qvariant_cast<std::vector<std::string>>(btn->property("propx"));
-
@n34rt
Yes most things. if a custom type, like your class type, then you would need to register it.Hmm yes you could store a vector like that. maybe it needs a & to take address if you mean to store a pointer to it.
However, using a static for this is terrible design and might cause issues.
the std::vectorstd::string vec{ "test", "test2"}; should be a member if a class, at least main(window)
and buttons manipulate and share this data.Its best first to build a data structure to hold the Data you need/have. and the Widgets to alter it. Not contain it,