Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8543605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:20:43+00:00 2026-06-11T12:20:43+00:00

I have subclassed QDialog to implement functionality similar to QMessageBox ( I needed this

  • 0

I have subclassed QDialog to implement functionality similar to QMessageBox ( I needed this to allow for customization). It has a text message and OK, Cancel buttons. I am showing the dialog using exec() to make it blocking. Now, how do I return values of true/false when the user clicks on OK/Cancel?

I tried connecting the buttons to setResult() and then, return the result value when clicked, but

  1. Clicking the buttons does not close the dialog box
  2. the return value is incorrect.
    Following is the code I have written. I think I am wrong in the exec/result part – but I am not sure how to fix it.
class MyMessageBox : public QDialog {
    Q_OBJECT

private slots:

    void onOKButtonClicked() { this->setResult(QDialog::Accepted); }
    void onCancelButtonClicked() { this->setResult(QDialog::Rejected); }

public:
    MyMessageBox(QMessageBox::Icon icon, const QString& title,
        const QString& text, bool showCancelButton = true,
        QWidget* parent = 0);

    virtual void resizeEvent(QResizeEvent* e);

    QDialog::DialogCode showYourself()
    {
        this->setWindowModality(Qt::ApplicationModal);
        this->exec();
        return static_cast<QDialog::DialogCode>(this->result());
    }
};

The user will instantiate the class and call showYourself() which is expected to return the value and also close(and delete) the dialog.

I have posted partial code. Let me know if you need more and I will post the complete version.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:20:45+00:00Added an answer on June 11, 2026 at 12:20 pm

    Some points :

    1. Rather than using setResult() yourself, use QDialog::accept() and QDialog::reject().
    2. It seems you are not taking full advantage of the signals and slots. You need the object which create the dialog (or another one) to listen to the signals of the dialog.
    3. In your code you are not connecting signals to slots either.
    4. With my fix onOKButtonClicked and onCancelButtonClicked are unnecessary.
    5. With my fix you don’t need showYourself(). Just call exec and with the events
      information will flow.

    You need to add this code before showing the dialog (this assume it is in a dialog method):

    QObject::connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
    QObject::connect(rejectButton, SIGNAL(clicked()), this, SLOT(reject()));
    

    In the caller object you have

    void someInitFunctionOrConstructor(){
       QObject::connect(mydialog, SIGNAL(finished (int)), this, SLOT(dialogIsFinished(int)));
    }
    
    void dialogIsFinished(int){ //this is a slot
       if(result == QDialog::Accepted){
           //do something
           return
       }
       //do another thing
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have subclassed a control in C# WinForms, and am custom drawing text in
We have subclassed the Silverlight Application class to add some additional functionality and then
I have subclassed a UITableViewCell and within this class I want to get it's
I have a subclassed NSManagedObject that conforms to the MKAnnotation protocol and it has
I have subclassed the UIToolbar to make it easier to implement a safari next,
I have subclassed Dialog in order to display a popup. This dialog contains a
I have subclassed a UIImageView, and have implemented touchesBegan/Moved/Finished like this: - (void)touchesBegan:(NSSet *)touches
I have subclassed Form to include some extra functionality, which boils down to a
I have subclassed RestDatasource to create my own data source. This is the constructor
I have subclassed UIViewController to provide common functionality for all UIViewControllers (for example I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.