Solved 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.
-
@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?
-
@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
.... -
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. :)
-
@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() ); }
-
@Kris-Revi
In what way didn't work? -
So 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?
-
@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 ?
-
Hi
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