2010-12-07 5 views
0

私は現在アクティブなウィンドウのすべてのボタン子ウィジェットを取得しようとしています。ボタンはQDialogBu​​ttonBoxによって作成されました。私は各ボタンの役割を取得しようとしているので、どのボタンがOK、CANCEL、またはSAVEボタンであるかを識別できます。しかし、私は次のコードでエラーを取得しています:QDialogBu​​ttonBoxによって作成されたQPushButtonの役割を取得する方法は?

QWidget *pWin = QApplication::activeWindow(); 
QList<QPushButton *> allPButtons = pWin->findChildren<QPushButton *>(); 
QListIterator<QPushButton*> i(allPButtons); 
while(i.hasNext()) 
{ 
    QDialogButtonBox *pButtonRole = new QDialogButtonBox(); 
    QDialogButtonBox::ButtonRole role = pButtonRole->buttonRole(i.next()); 
    qDebug() << "buttonRole: " << role << endl ; 
    //the value of role here is -1, which means it's an invalid role...  
} 

ボタンの役割を取得するとき、私は負の値を取得しています:(

誰かがコードと間違って何を教えてもらえます

+0

どちらのjkerianが言っどのように行うか、標準のボタンを使用すると、単純にボタンのいずれかが必要な場合:のQPushButton * OK = buttonBox- >ボタン(QDialogBu​​ttonBox :: Ok) –

+0

私はQDialogBu​​ttonBoxを試しました。pButtonRole =新しいQDialogBu​​ttonBox(); QDialogBu​​ttonBox :: ButtonRole role = pButtonRole-> buttonRole(i.next()); qDebug()<< "buttonRole:" << role << endl;私は無効な役割を持っています... -1 ... – Owen

答えて

1

QDialogButtonBoxの新しい空を作成しています。buttonsは考えていません。allPButtonsリストにあります。それらにbuttonRole()を呼び出すと、がそのbutton-boxに含まれていないため、-1(InvalidRole)が返されます。

あなたはjkerianが書いた通りにしなければならず、myButtonBoxPtrはすでにウィンドウに入っているQDialogButtonBoxを指さなければなりません。

(あなたは1 ButtonBoxを持っている場合)あなたはこのような何かを試すことができます。

QDialogButtonBox *box = pWin->findChild<QDialogButtonBox *>(); 
foreach(QAbstractButton* button, box->buttons()) 
{ qDebug() << box->buttonRole(button); } 
+0

私は参照してください。しかし、私はQDialogBu​​ttonBoxのインスタンスを取得する方法を理解することはできません。私が持っているのは、ウィンドウ内のすべてのボタンのリストです。 myButtonBoxPtrのインスタンスを取得するにはどうすればよいですか? – Owen

+0

QPushButtonsリストと同じ方法でQDialogBu​​ttonBoxのリストを取得できます。 – graphite

3

あなたはそのような非静的メソッドを呼び出すことはできません。あなたがQDialogButtonBox変数を持って仕事にbuttonRole()ため、その特定のインスタンスを呼び出す必要があります。

QDialogButtonBox::ButtonRole role = myButtonBoxPtr->buttonRole(i.next()); 
+0

私はQDialogBu​​ttonBoxを試しましたpButtonRole =新しいQDialogBu​​ttonBox(); \t \t QDialogBu​​ttonBox :: ButtonRole role = pButtonRole-> buttonRole(i.next()); \t qDebug()<< "buttonRole:" <<ロール<< endl;しかし、私は無効な役割をしています... -1 ... – Owen

関連する問題