PDA

View Full Version : QButtonBox, how to find index of QDialogButtonBox::Ok



HappyCoder
31st August 2015, 15:16
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:



// change "OK" to "Export"
QList<QAbstractButton *> buttonList = ui->buttonBox->buttons();
int indexOfOk = -1;
for( int i=0; i<buttonList.size(); i++ )
{
if ( ? ) // how must my if looks like
{
indexOfOk = i;
break;
}
}
if( indexOfOk != -1 )
ui->buttonBox->buttons().at( indexOfOk )->setText(QString("Export"));


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

Thx

Lesiok
31st August 2015, 15:32
Read about QDialogButtonBox::button(StandardButton which)

HappyCoder
1st September 2015, 08:30
Thx, here is the solution:



// change "OK" to "Export"
QList<QAbstractButton *> buttonList = ui->buttonBox->buttons();
int indexOfOk = -1;
for( int i=0; i<buttonList.size(); i++ )
{
QDialogButtonBox::StandardButton stdButton = ui->buttonBox->standardButton( buttonList.at(i) );
if ( stdButton == QDialogButtonBox::Ok)
{
indexOfOk = i;
break;
}
}
if( indexOfOk != -1 )
ui->buttonBox->buttons().at( indexOfOk )->setText(QString("Export"));

anda_skoa
1st September 2015, 09:14
Thx, here is the solution:

Not really, the actual solution is already in comment #2

Cheers,
_

HappyCoder
1st September 2015, 09:39
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.

anda_skoa
1st September 2015, 10:10
#2 only works if "OK" is the standard button of the dialog

No, it works regardless of which button is the default.



IMO it doesn't work if you want to change "retry" or "cancel" etc.

Yes, it does.


my solution is a general way i guess.
It is no more generic, just less efficient and with more unnecessary code.

Cheers,
_

HappyCoder
4th September 2015, 10:13
Ok, i think i got it :)



ui->buttonBox->button( QDialogButtonBox::Ok )->setText( tr("Export"));

anda_skoa
4th September 2015, 11:39
Exactly!

Way shorter and simpler, wouldn't you agree? :)

Cheers,
_

Lesiok
4th September 2015, 11:57
And so it will be safer when there is no button :
QPushButton *ptr = ui->buttonBox->button( QDialogButtonBox::Ok );
if( ptr )
ptr->setText( tr("Export"));