Results 1 to 3 of 3

Thread: QMessageBox Details button label

  1. #1
    Join Date
    Aug 2006
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QMessageBox Details button label

    I'm seeking for a solution to change the label of the "Show Details..."/"Hide Details..." button of a message box.
    I did it for a role based button, but this peculiar button is not in the role list and I m wondering how to get access to the button pointer to handle this.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMessageBox Details button label

    I think this text is hardcoded in Qt sources (at least in 4.5.2):
    Qt Code:
    1. // qmessagebox.cpp, lines 121 - 123
    2. QString label(DetailButtonLabel label)
    3. { return label == ShowLabel ? QMessageBox::tr("Show Details...")
    4. : QMessageBox::tr("Hide Details..."); }
    To copy to clipboard, switch view to plain text mode 
    I have a working solution, it's not very nice but maybe you can use it.
    Buttons are added in showEvent, so I've reimplemented it in order to get pointer to "hide/show details" button, it's added to the box with ActionRole. Next I've connected it to slot that will change it's text each time it's clicked. Here is the class:
    Qt Code:
    1. // MyMsgBox.h
    2.  
    3. class MyMsgBox : public QMessageBox{
    4. Q_OBJECT
    5. public:
    6. MyMsgBox( QWidget * parent = NULL ) : QMessageBox(parent){
    7. details = NULL;
    8. }
    9. MyMsgBox( Icon icon, const QString & title, const QString & text, StandardButtons buttons = NoButton, QWidget * parent = 0, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint )
    10. :QMessageBox(icon,title,text,buttons,parent,f)
    11. {
    12. details = NULL;
    13. }
    14. void setDetailedText( const QString& text );
    15. void setDetailsButtonText( const QString& t1, const QString& t2 );
    16. protected:
    17. virtual void showEvent( QShowEvent * event );
    18. protected slots:
    19. void detailsClicked();
    20. protected:
    21. QString _t1, _t2;
    22. QAbstractButton * details;
    23. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // MyMsgBox.cpp
    2.  
    3. void MyMsgBox::setDetailedText( const QString& text ){
    4. QMessageBox::setDetailedText(text);
    5. details = NULL;
    6. }
    7.  
    8. void MyMsgBox::setDetailsButtonText( const QString& t1, const QString& t2 ){
    9. _t1 = t1;
    10. _t2 = t2;
    11. }
    12.  
    13. void MyMsgBox::detailsClicked(){
    14. QAbstractButton * btn = qobject_cast<QAbstractButton*>(sender());
    15. if( btn ){
    16. if( btn->text() == tr("Show Details...") and _t1!=QString() ){
    17. btn->setText(_t1);
    18. } else if( _t2!=QString() ){
    19. btn->setText(_t2);
    20. }
    21. }
    22. }
    23.  
    24. void MyMsgBox::showEvent( QShowEvent * event ){
    25. QMessageBox::showEvent(event);
    26. if( details == NULL ){
    27. foreach( QAbstractButton * btn, this->buttons() ){
    28. if( buttonRole(btn) == QMessageBox::ActionRole and btn->text() == QMessageBox::tr("Show Details...") ){
    29. details = btn;
    30. connect(details, SIGNAL(clicked()), this, SLOT(detailsClicked()));
    31. if( _t1 != QString() ){
    32. details->setText(_t1);
    33. }
    34. break;
    35. }
    36. }
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 
    test app:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. MyMsgBox msg(QMessageBox::Information, "title", "text");
    6. msg.setDetailsButtonText("text1","text2");
    7. msg.setDetailedText("details");
    8. msg.show();
    9.  
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    As I've said, it's not very nice solution and I'm not even sure if it will work in new versions of Qt, but maybe you can use it somehow

  3. The following 2 users say thank you to stampede for this useful post:

    Chromatix (3rd September 2014), Cupidvogel (20th June 2015)

  4. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: QMessageBox Details button label

    Doesn't work in Qt 5.3, 32 bit, Mac. The this->buttons() method returns an empty QList.

Similar Threads

  1. How to add a a Button to a QMessageBox?
    By Bong.Da.City in forum Newbie
    Replies: 5
    Last Post: 29th August 2010, 20:02
  2. Replies: 6
    Last Post: 21st August 2010, 21:09
  3. Replies: 1
    Last Post: 2nd August 2010, 05:40
  4. Button icons in QMessageBox?
    By holst in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 16:27
  5. get button on QMessageBox
    By manhds in forum Qt Programming
    Replies: 3
    Last Post: 27th June 2006, 04:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.