Generated buttons, Hiding button text upon a button press
-
wrote on 6 Dec 2020, 13:27 last edited by Kris Revi 12 Jun 2020, 13:29
I generate buttons for each entry in a json file like this
QJsonDocument document = QJsonDocument::fromJson(jsonData); QJsonObject object = document.object(); QJsonValue value = object.value("Device"); QJsonArray array = value.toArray(); foreach (const QJsonValue & v, array) { QPushButton *button = new QPushButton(this); button->setObjectName("DeviceButton0"); button->setMinimumHeight(46); button->setMaximumHeight(46); button->setText(v.toObject().value("Name").toString()); ui->VB_LedStrip_Container->addWidget(button); }
So when i press a certain button i set the width of this menu so that ONLY the icon of the buttons shows but i can see the first character in the button text! and i can't set the width lower (for design reasons) so my question is.
- How would you go about hiding the text of every button in this menu when the button is pressed and when the button is pressed again to show the menu, show the text in the button again.
-
Lifetime Qt Championwrote on 6 Dec 2020, 13:46 last edited by mrjj 12 Jun 2020, 13:49
Hi
You could store the buttons in a QList so you can loop over them to set/reset text as needed.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.
Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.
-
Hi
You could store the buttons in a QList so you can loop over them to set/reset text as needed.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.
Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.
wrote on 6 Dec 2020, 14:10 last edited by@mrjj said in Generated buttons, Hiding button text upon a button press:
Hi
You could store the buttons in a QList so you can loop over them to set/reset text as needed.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.
Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.
that is cool, so let me get this right if i would do
button->setText("");
i can do
button->setProperty("orgtext", button->text());
to get the original text back? or did i get that wrong?
-
Hi
You could store the buttons in a QList so you can loop over them to set/reset text as needed.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.
Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.
wrote on 6 Dec 2020, 15:19 last edited by@mrjj i did this
mainwindow.h
List<QPushButton*> m_DeviceButtonList;
mainwindow.cpp
QJsonDocument document = QJsonDocument::fromJson(jsonData); QJsonObject object = document.object(); QJsonValue value = object.value("Device"); QJsonArray array = value.toArray(); foreach (const QJsonValue & v, array) { QPushButton *button = new QPushButton(this); button->setObjectName("DeviceButton0"); button->setMinimumHeight(46); button->setMaximumHeight(46); button->setText(v.toObject().value("Name").toString()); ui->VB_LedStrip_Container->addWidget(button); m_DeviceButtonList.append(button); } for (QPushButton * dButtons : m_DeviceButtonList) { qDebug() << "Found : " << dButtons; }
and i get this
Found : QPushButton(0x9ec5c8, name="DeviceButton0") Found : QPushButton(0x978840, name="DeviceButton0") Found : QPushButton(0x598d030, name="DeviceButton0") Found : QPushButton(0x598ccd0, name="DeviceButton0") Found : QPushButton(0x598d7c8, name="DeviceButton0")
i thought each button would get a unique object name? like
DeviceButton0
DeviceButton1
DeviceButton2
DeviceButton3
.... -
Lifetime Qt Championwrote on 6 Dec 2020, 15:26 last edited by mrjj 12 Jun 2020, 15:28
Hi
button->setProperty("orgtext", button->text());
stores the text into a dynamic property,. ( the orgtext name)button->setText( button->property("orgtext").toString() );
would set the text back.
Regarding the naming
you set it to
button->setObjectName("DeviceButton0");
so all will get same name.You will need to add the number manually.
something likeint cc=0; foreach (const QJsonValue & v, array) { QPushButton *button = new QPushButton(this); button->setObjectName("DeviceButton"+ QString::number(cc++)); ...
But else you seem on the right track. :)
-
Hi
button->setProperty("orgtext", button->text());
stores the text into a dynamic property,. ( the orgtext name)button->setText( button->property("orgtext").toString() );
would set the text back.
Regarding the naming
you set it to
button->setObjectName("DeviceButton0");
so all will get same name.You will need to add the number manually.
something likeint cc=0; foreach (const QJsonValue & v, array) { QPushButton *button = new QPushButton(this); button->setObjectName("DeviceButton"+ QString::number(cc++)); ...
But else you seem on the right track. :)
wrote on 6 Dec 2020, 15:33 last edited by@mrjj said in Generated buttons, Hiding button text upon a button press:
button->property("orgtext").toString()
this didn't work for some reason :S
for (QPushButton * dButtons : m_DeviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); }
-
@mrjj said in Generated buttons, Hiding button text upon a button press:
button->property("orgtext").toString()
this didn't work for some reason :S
for (QPushButton * dButtons : m_DeviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); }
Lifetime Qt Championwrote on 6 Dec 2020, 15:36 last edited by mrjj 12 Jun 2020, 15:36@Kris-Revi
In what way didn't work? -
@Kris-Revi
In what way didn't work?wrote on 6 Dec 2020, 15:37 last edited by Kris Revi 12 Jun 2020, 15:38So on button press i remove the text
void MainWindow::on_B_ToggleMenu_clicked() { if (ui->B_ToggleMenu->isChecked() == true) { for (QPushButton * dButtons : m_DeviceButtonList) { dButtons->setText(""); } } else { for (QPushButton * dButtons : m_DeviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); } } }
but the text never shows :S
-
@Kris-Revi said in Generated buttons, Hiding button text upon a button press:
but the text never shows
When you never set the property how do you expect that there is text in it?
-
@Kris-Revi said in Generated buttons, Hiding button text upon a button press:
but the text never shows
When you never set the property how do you expect that there is text in it?
wrote on 6 Dec 2020, 15:39 last edited by Kris Revi 12 Jun 2020, 15:40@Christian-Ehrlicher said in Generated buttons, Hiding button text upon a button press:
@Kris-Revi said in Generated buttons, Hiding button text upon a button press:
but the text never shows
When you never set the property how do you expect that there is text in it?
@mrjj said in Generated buttons, Hiding button text upon a button press:
Hi
You could store the buttons in a QList so you can loop over them to set/reset text as needed.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.
Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );did i read this wrong? was it supposed to be
you should also store the text in a dynamic property ?
-
@Christian-Ehrlicher said in Generated buttons, Hiding button text upon a button press:
@Kris-Revi said in Generated buttons, Hiding button text upon a button press:
but the text never shows
When you never set the property how do you expect that there is text in it?
@mrjj said in Generated buttons, Hiding button text upon a button press:
Hi
You could store the buttons in a QList so you can loop over them to set/reset text as needed.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );that way you can hide it by setting it to empty text and then you have the text in the property to restore it when needed.
Alternative 2 is to make a custom QPushButton with custom paintEvent where you can tell it to draw text or not.
When you assign the text the first time
button->setText(v.toObject().value("Name").toString());
you also store the text in a dynamic property
button->setProperty("orgtext", button->text() );did i read this wrong? was it supposed to be
you should also store the text in a dynamic property ?
Lifetime Qt Championwrote on 6 Dec 2020, 15:43 last edited by mrjj 12 Jun 2020, 15:44Hi
Yes. you should also store it in the property as else it will just be empty.
So when you create teh buttons, also do and have set the text.
button->setProperty("orgtext", button->text() );foreach (const QJsonValue & v, array) { QPushButton *button = new QPushButton(this); button->setObjectName("DeviceButton0"); button->setMinimumHeight(46); button->setMaximumHeight(46); button->setText(v.toObject().value("Name").toString()); button->setProperty("orgtext", button->text() ); // store a copy ......1
2/11