0

i'am searching a easy way to get the pressed button from QDialog widget. (all buttons finished the dialog after a pressed event, Many Buttons). this way:

MyDialog *ptrMyDialog = new MyDialog;
ptrMyDialog->exec(); // blocked til finished 
// ??? 
QAbstractButton * btn = ptrMyDialog->Function_I_Need();
// or. I only need a spezific return value, set by button. e.g
auto retValIneed= ptrMyDialog->exec();

Any hint will be helpful

regards Lars

larsDD
  • 41
  • 1
  • 2
  • 8

2 Answers2

1

The QDialog exec() already returns whether the dialog was accepted or rejected. If the distinction between these two is not sufficient, since you already have a custom class you can easily implement such behaviour yourself.

Any button already calls one of the functions which closes the dialog, so you can simply store which one was pressed within a member of the class and retrieve that value using the method you desire.

A different possibility would be to overload exec(), call the base class implementation within it and return your custom member which stores what button was pressed as before directly.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
0

You will have call the setResult(int r) method when handling the buttons in MyDialog, then result() will give you the according value (and probably also exec()). For details how this is done you could check the QMessageBox implementation, since there exec() returns the actual button ID.

Gert Wollny
  • 529
  • 2
  • 8
  • hi and thanks. i tried the setResult/result mechanism without success. how i should close the dialog in buttonhandler. i tried setResult/close ; setResult/finished ; setResult/done. seems there only to states 0 & 1 – larsDD Mar 10 '17 at 10:04
  • 1
    In *QMessageBox* a button ID is passed to setResult, see [qmessagebox.cpp:1356](https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qmessagebox.cpp.html), you should probably do something similar. – Gert Wollny Mar 10 '17 at 14:39