Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDialog with close button and X button
Forum Updated to NodeBB v4.3 + New Features

QDialog with close button and X button

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abhi_Varma
    wrote on 20 Aug 2021, 15:13 last edited by Abhi_Varma
    #1

    I'm new to Qt, facing some issue with dialogs. Here is my basic UI design I have a Class TabA that inherits QWidget (it is a tab in a window, there are multiple tabs like tabA, tabB...) . In the tab there is a button call "Send" on clicking it a custom dialog is created which shows some progress(lets call it MyDialog). The dialog also has a "close " button at the bottom on clicking the button it will close the dialog. Below are my member functions of TabA class.

    CreateMyDialog() slot will take care of creating the custom dialog when we click that "Send" button. You also see a signal slot connection in CreateMyDialog() where I connected rejected() signal to a slot, this rejected() signal is emitted (correct me if I'm wrong) when user clicks the "close" button or the X button on the top right corner of the dialog. When this signal is emitted I basically want to destroy the MyDialog class object. I read that on clicking the "X" button closeEvent() is called and at the end QDialog::rejected() is emitted. The "close" button I have on clicking that MyDialog::On_btnClose_Clicked() slot is triggered which will call the close() which I'm assuming that at a later stage will also emit QDialog::rejected() because I notice the corresponding slot OnMyDialogClosed() is called.

    void CreateMyDialog()
    {
          MyDialog *pMyDialog = new MyDialog(this);
          connect(pMyDialog, SIGNAL(rejected()), this, SLOT(OnMyDialogClosed());
          pMyDialog->setModel(true);
          pMyDialog->show();
    }
    
    

    The Following slot is used to destroy the MyDialog variable

    void TabA ::OnMyDialogClosed()
    {
         if(pMyDialog != NULL)
            delete MyDialog;
    }
    
    
    Class MyDialog: public QDialog
    {
    }
    

    MyDialog has one button called "cancel" on clicking it following slot will be invoked.

    void MyDialog::On_btnClose_Clicked()
    {
          close();
    }
    

    Even though on clicking either "X" button or "close" the necessary slot is invoked, its also giving me exceptions. Any idea where I made a mistake

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stretchthebits
      wrote on 20 Aug 2021, 16:50 last edited by
      #2

      Please use punctuations in the appropriate places since this is difficult to read.
      Instead of QDialog::Rejected, it returns QDialog::Accepted?

      A 1 Reply Last reply 21 Aug 2021, 12:48
      0
      • S stretchthebits
        20 Aug 2021, 16:50

        Please use punctuations in the appropriate places since this is difficult to read.
        Instead of QDialog::Rejected, it returns QDialog::Accepted?

        A Offline
        A Offline
        Abhi_Varma
        wrote on 21 Aug 2021, 12:48 last edited by
        #3

        @stretchthebits It is emitting rejected() in both cases but getting exceptions. I have edited my post kindly recheck

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 21 Aug 2021, 13:07 last edited by mrjj
          #4

          Hi

          Just as a note
          You give the dialog a parent
          MyDialog *pMyDialog = new MyDialog(this);

          so also using delete MyDialog; is a no go as
          Qt has an owner system and you risk double deletes
          https://doc.qt.io/qt-5/objecttrees.html

          So basically Dont give it a parent and use delete or
          let Qt clean it up.

          Also please note the flag
          diag->setAttribute(Qt::WA_DeleteOnClose);

          that you can set so the dialog will auto delete (when closed) when its not optimal to let Qt do it later via the parent assignment. Like long running app and you create this dialog many times.

          1 Reply Last reply
          1

          1/4

          20 Aug 2021, 15:13

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved