PDA

View Full Version : Adding button to QMessageBox doesn't work



code_err
8th February 2012, 17:34
I want to personalize button's labels so i do this:


QMessageBox *msgBox = new QMessageBox(QMessageBox::Warning, QString("blablabla"), QString("blablabla"), 0, this);

msgBox->addButton(QString("Option 1"), QMessageBox::Yes);
msgBox->addButton(QString("Option 2"), QMessageBox::No);
and get such a message from compilator:

Window.cpp: In member function 'void PicDevWindow::changeMode(int)':
Window.cpp:268: error: no matching function for call to 'QMessageBox::addButton(QString, QMessageBox::StandardButton)'
c:/Qt/4.8.0/include/QtGui/../../src/gui/dialogs/qmessagebox.h:143: note: candidates are: void QMessageBox::addButton(QAbstractButton*, QMessageBox::ButtonRole)
c:/Qt/4.8.0/include/QtGui/../../src/gui/dialogs/qmessagebox.h:144: note: QPushButton* QMessageBox::addButton(const QString&, QMessageBox::ButtonRole)
c:/Qt/4.8.0/include/QtGui/../../src/gui/dialogs/qmessagebox.h:145: note: QPushButton* QMessageBox::addButton(QMessageBox::StandardButton )
Window.cpp:269: error: no matching function for call to 'QMessageBox::addButton(QString, QMessageBox::StandardButton)'
c:/Qt/4.8.0/include/QtGui/../../src/gui/dialogs/qmessagebox.h:143: note: candidates are: void QMessageBox::addButton(QAbstractButton*, QMessageBox::ButtonRole)
c:/Qt/4.8.0/include/QtGui/../../src/gui/dialogs/qmessagebox.h:144: note: QPushButton* QMessageBox::addButton(const QString&, QMessageBox::ButtonRole)
c:/Qt/4.8.0/include/QtGui/../../src/gui/dialogs/qmessagebox.h:145: note: QPushButton* QMessageBox::addButton(QMessageBox::StandardButton )
make[1]: *** [release/Window.o] Error 1
make[1]: Leaving directory `/h/prg0233'
make: *** [release] Error 2
when i define earlier

const QString label1("Tak");
It still doesn't work.

KillGabio
8th February 2012, 17:53
Maybe including a new before calling the constructor QMessageBox (QMessageBox::Warning...); ?

code_err
8th February 2012, 18:26
Sorry, of course i have new in my code, don't know how it disapeared here.


QMessageBox *msgBox = new QMessageBox(QMessageBox::Warning, QString("blablabla"), QString("blablabla"), 0, this);

msgBox->addButton(QMessageBox::Yes);
msgBox->addButton(QMessageBox::No);

This one works but with default labels

norobro
8th February 2012, 20:35
The second argument to addButton() is your problem not the QString. It should be QMessageBox::ButtonRole (http://doc.qt.nokia.com/4.7-snapshot/qmessagebox.html#ButtonRole-enum)

code_err
8th February 2012, 21:40
Yes, it works, thanks.