0

I would like to use my own string on the button, in the the below mentioned functions "msgBox.setDefaultButton" and "msgBox.addButton" :

msgBox.setDefaultButton(QMessageBox::Save);
msgBox.addButton(QMessageBox::Abort);

instead of "Save" and "Abort", which are inbuilt, i would like to put my own text.

please let me know if its possible or please give me an alternative for above lines to create a button with my own random text.

ex :

msgBox.setDefaultButton(QMessageBox::"Lakshmi");
msgBox.addButton(QMessageBox::"Kanth");

TIA.

Regards, Lakshmikanth .G

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
SirKappe
  • 55
  • 1
  • 7
  • Instead of setting texts manually you should look into [how Qt handles translations](https://wiki.qt.io/How_to_create_a_multi_language_application). – nwp Oct 09 '17 at 13:50
  • Actually we have a separate built in function which handles the language translation, and we are supposed to use that and i just want to use the translated "YES" and "NO" string output from that builtin function, within these buttons. – SirKappe Oct 09 '17 at 13:56

1 Answers1

1

QMessageBox::Save and QMessageBox::Abort are not variables where you take the text but are part of an enumeration and internally creates the buttons with the texts previously set. If you want to set custom texts the addButton() function is overloaded:

void QMessageBox::addButton(QAbstractButton *button, ButtonRole role)
QPushButton *QMessageBox::addButton(const QString &text, ButtonRole role)
QPushButton *QMessageBox::addButton(StandardButton button)

So for your case you could use any of the other options as shown below:

QMessageBox w;
QPushButton* Lakshmi = w.addButton("Lakshmi", QMessageBox::YesRole);
w.addButton("Kanth", QMessageBox::NoRole);
w.setDefaultButton(Lakshmi);
w.show();
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • My previous code : bool func() { QMessageBox msgBox; bool retValue=false; msgBox.setText(MY_STRING); msgBox.setStandardButtons(QMessageBox::Yes); msgBox.addButton(QMessageBox::No); if(msgBox.exec() == QMessageBox::Yes){ retValue= true; } return retValue; } edited to : bool func() { QMessageBox msgBox; QMessageBox w; bool retValue=false; msgBox.setText(MY_STRING); QPushButton* Lakshmi = w.addButton("Lakshmi", QMessageBox::YesRole); w.addButton("Kanth", QMessageBox::NoRole); } Am i doing it right? please let me know – SirKappe Oct 10 '17 at 08:12
  • Uses the same QMessageBox: `bool func() { QMessageBox msgBox; bool retValue=false; msgBox.setText(MY_STRING); QPushButton* Lakshmi = msgBox.addButton("Lakshmi", QMessageBox::YesRole); msgBox.addButton("Kanth", QMessageBox::NoRole); if(msgBox.exec() == QMessageBox::Yes){ retValue= true; } return retValue; }`, If my answer helped you do not forget to mark it as correct please. – eyllanesc Oct 10 '17 at 10:25
  • Hi i can see customized button with given name, but i want that name to be a variable, so that it can be changed based on system language, something like below : std::string MY_STRING_1 std::string MY_STRING_2 bool func() { QMessageBox msgBox; bool retValue=false; msgBox.setText(MY_STRING); QPushButton* Lakshmi = msgBox.addButton(MY_STRING_1, QMessageBox::YesRole); msgBox.addButton(MY_STRING_2, QMessageBox::NoRole); } but above code not working error: no matching function for call to 'QMessageBox::addButton(std::string&, QMessageBox::ButtonRole)' plz help me out. – SirKappe Oct 10 '17 at 11:04
  • The problem is given by the type of string you use, Qt uses its own data types to have no problem changing the compiler, I recommend using `QString` instead of `std::string`, change : `std::string MY_STRING_1` to `QString MY_STRING_1` and `std::string MY_STRING_2` to `QString MY_STRING_2` – eyllanesc Oct 10 '17 at 11:28
  • your answer was useful and it is correct. but please know that i'm new to stackOverflow, so do let me know how to mark your answer as correct.. – SirKappe Oct 11 '17 at 06:19
  • And one last thing, while we are at it, how do we convert u16_t variable to string? i'm interested in the string which is present in that position.thanks – SirKappe Oct 11 '17 at 06:28
  • To mark an answer as correct just press the arrow to the left of my answer, for more information read the following: https://stackoverflow.com/tour . – eyllanesc Oct 11 '17 at 08:15
  • To convert a numeric data type to QString you must use QString :: number (your_numeric_variable) – eyllanesc Oct 11 '17 at 08:15
  • HI Can u plz help me out with this new issue : https://stackoverflow.com/questions/46769630/unable-to-update-the-buttons-in-qt-dialogue-box Thanks. – SirKappe Oct 17 '17 at 06:11
  • I asked you for certain things and you do not provide me with the information, I also asked you to mark my answer as correct since it helped you, but you do not, that discourages me to help you. If you take the time to mark my answer as correct and also share the project as I have then I could help. – eyllanesc Oct 17 '17 at 06:23
  • I marked your answer as correct, but i got message saying if my reputation is less than 15 then me marking answer as correct wont be counted. i'm sorry for that. – SirKappe Oct 25 '17 at 10:23