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



  • 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()
    {

    }@



  • 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.


  • Moderators

    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.



  • thank you mlong. it worked. ;-)


  • Moderators

    Good deal! Glad to help.



  • 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?



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

    i marked this topic as solved.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.