Results 1 to 9 of 9

Thread: QButtonBox, how to find index of QDialogButtonBox::Ok

  1. #1
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default QButtonBox, how to find index of QDialogButtonBox::Ok

    Hi,

    i have in one of my form class a QButtonBox and i wand to change the text from "OK" to "Export" (translatable).

    1) The easy way is to search for the QString "OK", but if i activate other languages it is not "OK" anymore.
    2) An other easy way is to use a fixed indexOfOk, "try and error method" till i have the right index to OK Button, but it is not very flexible

    I use this so far:

    Qt Code:
    1. // change "OK" to "Export"
    2. QList<QAbstractButton *> buttonList = ui->buttonBox->buttons();
    3. int indexOfOk = -1;
    4. for( int i=0; i<buttonList.size(); i++ )
    5. {
    6. if ( ? ) // how must my if looks like
    7. {
    8. indexOfOk = i;
    9. break;
    10. }
    11. }
    12. if( indexOfOk != -1 )
    13. ui->buttonBox->buttons().at( indexOfOk )->setText(QString("Export"));
    To copy to clipboard, switch view to plain text mode 

    QButtonBox::buttons() gives me a QList<QAbstractButtons *>.
    I can't find any relation between QAbstractButton and QDialogButtonBox::Ok ( Widget vs. enum )?

    Thx

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok


  3. #3
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    Thx, here is the solution:

    Qt Code:
    1. // change "OK" to "Export"
    2. QList<QAbstractButton *> buttonList = ui->buttonBox->buttons();
    3. int indexOfOk = -1;
    4. for( int i=0; i<buttonList.size(); i++ )
    5. {
    6. QDialogButtonBox::StandardButton stdButton = ui->buttonBox->standardButton( buttonList.at(i) );
    7. if ( stdButton == QDialogButtonBox::Ok)
    8. {
    9. indexOfOk = i;
    10. break;
    11. }
    12. }
    13. if( indexOfOk != -1 )
    14. ui->buttonBox->buttons().at( indexOfOk )->setText(QString("Export"));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    Quote Originally Posted by HappyCoder View Post
    Thx, here is the solution:
    Not really, the actual solution is already in comment #2

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    Quote Originally Posted by anda_skoa View Post
    Not really, the actual solution is already in comment #2

    Cheers,
    _
    #2 only works if "OK" is the standard button of the dialog, IMO it doesn't work if you want to
    change "retry" or "cancel" etc., my solution is a general way i guess.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    Quote Originally Posted by HappyCoder View Post
    #2 only works if "OK" is the standard button of the dialog
    No, it works regardless of which button is the default.

    Quote Originally Posted by HappyCoder View Post
    IMO it doesn't work if you want to change "retry" or "cancel" etc.
    Yes, it does.

    Quote Originally Posted by HappyCoder View Post
    my solution is a general way i guess.
    It is no more generic, just less efficient and with more unnecessary code.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2015
    Posts
    87
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    Ok, i think i got it

    Qt Code:
    1. ui->buttonBox->button( QDialogButtonBox::Ok )->setText( tr("Export"));
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    Exactly!

    Way shorter and simpler, wouldn't you agree?

    Cheers,
    _

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QButtonBox, how to find index of QDialogButtonBox::Ok

    And so it will be safer when there is no button :
    Qt Code:
    1. QPushButton *ptr = ui->buttonBox->button( QDialogButtonBox::Ok );
    2. if( ptr )
    3. ptr->setText( tr("Export"));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Find item index in QGraphicsLinearLayout
    By bmn in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2014, 00:06
  2. Replies: 2
    Last Post: 13th March 2014, 19:11
  3. How to find out row index of a QTableWidget.
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2012, 15:32
  4. Replies: 1
    Last Post: 14th September 2010, 16:26
  5. a QDialogButtonBox question
    By bnilsson in forum Qt Programming
    Replies: 3
    Last Post: 16th July 2008, 22:42

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.