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. [SOLVED] how to stop instances of dialog and close the dialog
QtWS25 Last Chance

[SOLVED] how to stop instances of dialog and close the dialog

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 7.9k Views
  • 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.
  • K Offline
    K Offline
    kalster
    wrote on 3 Aug 2011, 02:03 last edited by
    #1

    I have set setModal to false and i am now able to interact between the mainwindow and the dialog. in the mainwindow i have a button that once it is pressed will open up the dialog. once the dialog is displayed, i am able to click the button on the mainwindow to open up a second dialog that i don't want opened.

    I have a if conditional statement in the mainwindow.cpp that will check if the dialog isVisible() == false. if the isVisible() == false then it will show the dialog else it will not. the code is not working for me as expected as i am able to open up many dialogs.

    also, i am not able to close the dialog. The code is at the end of the mainwindow.cpp of the code below.

    Note that i am not experienced in parent child programming and i need some help or examples for this code to work.

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "dialog.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    Dialog *w = new Dialog(this);
    w->setModal(false);
    if (w->isVisible() == false){
    w->show();
    }
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    Dialog *w = new Dialog(this);
    w->setAttribute(Qt::WA_DeleteOnClose);
    delete w;
    w = 0;
    }@

    dialog.cpp
    @#include "dialog.h"
    #include "ui_dialog.h"
    #include "mainwindow.h"

    Dialog::Dialog(QWidget *Parent) :
    QDialog(Parent, Qt::CustomizeWindowHint |
    Qt::WindowCloseButtonHint ),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    this->setFixedSize(width(), height());
    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::on_pushButton_clicked()
    {

    }@

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zakirenish
      wrote on 3 Aug 2011, 04:21 last edited by
      #2

      As i see from the code, every time you press pushButton and pushButton_2 you allocating NEW LOCAL variable variable "w" which is a pointer to Dialog, so when you check if w->isVisible you check for the variable you just created and its by default is invisible, that is why you creating many dialog windows.

      Solution depends on what you trying to do, as i understood what you was trying to do is when you press one button you create dialog when press the other you close it.

      In order to do this you can make MEMBER variable w. When you press pushbutton, first you check if dialog already exist i.e not points to null. Then if it does you can check if its visible and if not then show it. If the dialog does not exist you allocate new DIalog and make w point to it and show it.

      If you want to close dialog by pressing pushButton_2 you can just use w->close() or w->accept. then clear up allocated memory and make w point to NULL;

      P.S Sorry if misunderstood anything, cuz just from the gym.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on 3 Aug 2011, 04:30 last edited by
        #3

        Instead of creating (and/or deleting) a new Dialog every time in your on_*_clicked() methods, create one dialog as a member of your class, and just call show (or hide) as appropriate.

        in mainwindow.h:
        @
        private:
        Dialog *m_dialog;
        @

        in mainwindow.cpp:

        @
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "dialog.h"

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);
        m_dialog = new Dialog(this);
        m_dialog->setModal(false);
        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow::on_pushButton_clicked()
        {
        // show the dialog
        if (!m_dialog->isVisible())
        m_dialog->show();
        }

        void MainWindow::on_pushButton_2_clicked()
        {
        // hide the dialog
        if (m_dialog->isVisible())
        m_dialog->hide();
        }
        @

        and unless there's a specific reason to delete the dialog, you can just let the normal QObject cleanup delete it. Since the Dialog's parent is "this", deleting MainWindow will destroy the dialog for you when MainWindow is destroyed.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalster
          wrote on 3 Aug 2011, 05:11 last edited by
          #4

          thank you mlong. it worked. ;-)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on 3 Aug 2011, 05:14 last edited by
            #5

            Good deal! Glad to help.

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on 3 Aug 2011, 05:37 last edited by
              #6

              There is one little detail that i forget to mention. After I added your code, when i click the push button from the main window, the main window flickers a bit as the dialog opens up. is there a way to fix the flickering?

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on 3 Aug 2011, 05:43 last edited by
                #7

                i found what was creating the flickering. it was created by the centerWidgetOnScreen(this);

                i marked this topic as solved.

                1 Reply Last reply
                0

                5/7

                3 Aug 2011, 05:14

                • Login

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