Custom dialog using QMessageBox()

839 views
Skip to first unread message
Assigned to ron...@gmail.com by me

M., Ronaldo

unread,
Sep 3, 2013, 3:47:21 PM9/3/13
to qtcon...@googlegroups.com
Is there a way to replace the end of this function ?

like: "RETURN( oMBox:exec() == QMessageBox_Yes )"

// *---------------------------------------------------------------------------*
// Custom_Dlg( cMsg, cTitle )
// *---------------------------------------------------------------------------*
Function Custom_Dlg( cMsg, cTitle )

  LOCAL oButtonYes
  LOCAL oButtonNo
  LOCAL oMBox := QMessageBox()

  oMBox:setInformativeText( cMsg )
  oMBox:setWindowTitle( cTitle )
  oMBox:setWindowFlags( Qt_Dialog )
  oMBox:setIcon( QMessageBox_Critical )
  oButtonYes := oMBox:addButton( "&1 [Yes]", QMessageBox_YesRole )
  oButtonNo  := oMBox:addButton( "&2 [No]", QMessageBox_NoRole )
  oMBox:setDefaultButton( oButtonYes )

RETURN( oMBox:exec() == 0 )

Pritpal Bedi

unread,
Sep 3, 2013, 3:50:59 PM9/3/13
to qtcon...@googlegroups.com
Hi


Is there a way to replace the end of this function ?

like: "RETURN( oMBox:exec() == QMessageBox_Yes )"


What you mean by "replace" ?


Pritpal Bedi 

M., Ronaldo

unread,
Sep 3, 2013, 4:04:36 PM9/3/13
to qtcon...@googlegroups.com
Hi Pritpal :)

I'm looking for a defalut value, like in a Std_Dlg,
replacing RETURN( oMBox:exec() == 0 ) by a better/elegant code

Sample:

// *---------------------------------------------------------------------------*
// Std_Dlg( cMsg, cTitle )
// *---------------------------------------------------------------------------*
Function Std_Dlg( cMsg, cTitle )


  LOCAL oMBox := QMessageBox()

  oMBox:setInformativeText( cMsg )
  oMBox:setWindowTitle( cTitle )
  oMBox:setWindowFlags( Qt_Dialog )
  oMBox:setIcon( QMessageBox_Critical )
  oMBox:setStandardButtons( QMessageBox_Yes + QMessageBox_No )
  oMBox:setDefaultButton( QMessageBox_Yes )

RETURN( oMBox:exec() == QMessageBox_Yes )



Hi


Is there a way to replace the end of this function ?

like: "RETURN( oMBox:exec() == QMessageBox_Yes )"


M., Ronaldo

unread,
Sep 3, 2013, 4:09:56 PM9/3/13
to qtcon...@googlegroups.com
In a better way, I want the Buttom with QMessageBox_YesRole returning the same value as QMessageBox_Yes


regards

Pritpal Bedi

unread,
Sep 3, 2013, 4:35:09 PM9/3/13
to qtcon...@googlegroups.com
Hi



In a better way, I want the Buttom with QMessageBox_YesRole returning the same value as QMessageBox_Yes


oMBox:exec()  always return a value associated with button defined and pressed.

So you can use 

RETURN  oMBox:exec() == QMessageBox_Yes 

without any issues. 

A simple  experimernt should have given you the correct insight.


Pritpal Bedi

M., Ronaldo

unread,
Sep 3, 2013, 4:40:02 PM9/3/13
to qtcon...@googlegroups.com
Hi Pritpal !

I tryed it before my first message, and is returning 0/1 ( Buttom 1 and 2 )

Regards

M., Ronaldo

unread,
Sep 3, 2013, 4:42:22 PM9/3/13
to qtcon...@googlegroups.com
Then:

RETURN  oMBox:exec() == QMessageBox_Yes

will be always ( False )

Pritpal Bedi

unread,
Sep 3, 2013, 4:58:08 PM9/3/13
to qtcon...@googlegroups.com
Hi 


RETURN  oMBox:exec() == QMessageBox_Yes

will be always ( False )

I re-visited QMessageBox documentation. This class inherits from QDialog.
So :exec() will ever return 0 == ESC or 1 == Some Button Clicked.

You are to trap :buttonClicked(QAbstractButton) signal to know what user clicked.


Pritpal Bedi

M., Ronaldo

unread,
Sep 3, 2013, 5:13:37 PM9/3/13
to qtcon...@googlegroups.com
Hi Pritpal !

Return from :exec()

0 - Buttom 1
1 - Buttom 2
2 - Buttom 3 ( New )

All other actions wil return 2 ( or last buttom number )
Esc, Close, etc



Regards

M., Ronaldo

unread,
Sep 3, 2013, 5:15:47 PM9/3/13
to qtcon...@googlegroups.com
Thanks Pritpal :)

I will use buttonClicked() for better event control

Regards

Francesco Perillo

unread,
Sep 3, 2013, 5:24:20 PM9/3/13
to qtcontribs

please read qmessagebox docs: exec will return button pressed only if the buttons are standard, otherwise you need to use buttonclicked.

--
You received this message because you are subscribed to the Google Groups "QtContribs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qtcontribs+...@googlegroups.com.
To post to this group, send email to qtcon...@googlegroups.com.
Visit this group at http://groups.google.com/group/qtcontribs.
For more options, visit https://groups.google.com/groups/opt_out.

M., Ronaldo

unread,
Sep 3, 2013, 10:11:59 PM9/3/13
to qtcon...@googlegroups.com
Hi Francesco !

Did you tryed the sample ?

// *---------------------------------------------------------------------------*
// Custom_Dlg( cMsg, cTitle )
// *---------------------------------------------------------------------------*
Function Custom_Dlg( cMsg, cTitle )

  LOCAL oButtonYes
  LOCAL oButtonNo
  LOCAL oMBox := QMessageBox()

  oMBox:setInformativeText( cMsg )
  oMBox:setWindowTitle( cTitle )
  oMBox:setWindowFlags( Qt_Dialog )
  oMBox:setIcon( QMessageBox_Critical )
  oButtonYes := oMBox:addButton( "&1 [Yes]", QMessageBox_YesRole )
  oButtonNo  := oMBox:addButton( "&2 [No]", QMessageBox_NoRole )
  oMBox:setDefaultButton( oButtonYes )

RETURN( oMBox:exec() )

Francesco Perillo

unread,
Sep 4, 2013, 4:16:23 AM9/4/13
to qtcontribs
Did you tryed the sample ?

No, of course... :-) I was on a mobile

but  I read the docs.  :exec() method is overloaded in QMessageBox so it is not used the one in QDialog as Pritpal said.

From http://harmattan-dev.nokia.com/docs/library/html/qt4/qmessagebox.html#exec

When using a QMessageBox with standard buttons, this functions returns a StandardButton value indicating the standard button that was clicked. When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.

I understand from the samples in the docs that "StandardButton" is when you use:
 msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
 msgbox.exec() returns the button pressed like QMessageBox::Save

Instead, you are using "custom buttons" added with :addButton so :exec() returns an "opaque" value and :clickedButton() must be used, as per docs (samples are in c++ but they may be easily converted to Harbour):
QMessageBox msgBox;
 QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
 QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);

 msgBox.exec();

 if (msgBox.clickedButton() == connectButton) {
     // connect
 } else if (msgBox.clickedButton() == abortButton) {
     // abort
 }


--

M., Ronaldo

unread,
Sep 4, 2013, 12:00:33 PM9/4/13
to qtcon...@googlegroups.com
Thanks Francesco :)
Reply all
Reply to author
Forward
0 new messages