[SOLVED] - dialog openes several time in MainWindow



  • hi all,

    i have the following problem: i have a mainWindow and a .ui form which opens if i press a button in the mainWindow. the .ui form has an OK button and a Cancel button. after i open it 1st time, if i Cancel and re-open, the window will be opened 2 x (two times.)
    pressing Cancel on both windows, and re-open the form, will open 4 x (four times) same window.
    closing and opening repetitively will open: number of same windows = 2 x number of previous openings.

    i Cancel the form with:

    @void newA::on_pushButton_clicked()
    {
    newA::close();
    }@

    I think, I'm not sure, but the previous session should be deleted. i found the deleteLater () function for QDialog, but nothing else. and the deleteLater () is not working in my case.

    any idea how the dialog should be deleted/destroyed?
    has anyone experienced this situation?
    much appreciated :)


  • Moderators

    Why does deleteLater not work in your case?
    This happens only when you are pressing "cancel"?



  • thanks for your prompt reply.

    1. i'm not sure how to check which is the reason for deleteLater not working. i can confirm has the same symptom as described above.
    2. i haven't try other signal, except cancel. after i press "cancel" and re-open the form, yes, this happens.

    any idea what might be wrong?


  • Moderators

    The way how you described your problem suggests that it is working correctly with OK button. therefore I was iterating to see, if this is true.
    To give too little information. You need to post more of your code. One needs a crystal ball to give advice.

    You might also want to have a look to the "dialog examples.":http://qt-project.org/doc/qt-5.0/qtwidgets/examples-dialogs.html All those examples should provide you with ideas how to handle a dialog box properly.



  • [quote author="angelicaP" date="1372006390"]

    i Cancel the form with:

    @void newA::on_pushButton_clicked()
    {
    newA::close();
    }@

    [/quote]

    But why not using close() directly?



  • i’m back with some code. Still have the same issue:

    @mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_actionA_2_triggered();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    Newa.h
    #ifndef NEWA_H
    #define NEWA_H

    #include <QDialog>

    namespace Ui {
    class newA;
    }

    class newA : public QDialog
    {
    Q_OBJECT

    public:
    explicit newA(QWidget *parent = 0);
    ~newA();

    private slots:
    void on_addE1Info_clicked();

    void on_pushButton_2_clicked();
    

    private:
    Ui::newA *ui;
    };

    #endif // NEWA_H

    Newe.h

    #ifndef NEWE_H
    #define NEWE_H

    #include <QDialog>

    namespace Ui {
    class newe;
    }

    class newe : public QDialog
    {
    Q_OBJECT

    public:
    explicit newe(QWidget *parent = 0);
    ~newe();

    private slots:

    void on_pushButton_clicked();
    

    private:
    Ui::newe *ui;
    };

    #endif // NEWE_H

    Main.cpp
    #include "mainwindow.h"
    #include <QApplication>
    #include <QDialog>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }

    Mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "newa.h"
    #include "newe.h"
    #include <QToolButton>
    #include <QtCore>
    #include <QTabWidget>
    #include <QLabel>
    #include <QScrollArea>
    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    QMainWindow::showMaximized();
    QScrollArea * scrollArea = new QScrollArea();
    setCentralWidget(scrollArea);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

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

    void MainWindow::on_actionA_2_triggered()
    {
    connect(ui->actionA_2, SIGNAL(triggered()), this , SLOT(on_actionA_2_triggered()));
    newA *nA = new newA;
    nA->show();

    Newa.cpp
    #include "mainwindow.h"
    #include "newa.h"
    #include "ui_newa.h"
    #include "newe.h"
    #include <QScrollArea>
    #include <QtCore>
    #include <QMainWindow>
    #include <QTabWidget>
    #include <QLabel>

    newA::newA(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::newA)
    {
    ui->setupUi(this);

    }

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

    void newA::on_addE1Info_clicked()
    { connect(ui->addE1Info, SIGNAL(clicked()), this, SLOT(on_addE1Info_clicked()));
    newe * aE = new newe();
    aE->show();

    }

    void newA::on_pushButton_2_clicked()
    {
    close();
    }

    newe.cpp
    #include "newe.h"
    #include "ui_newe.h"
    #include "newa.h"
    #include <QString>

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

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

    void newe::on_pushButton_clicked()
    {
    close();
    deleteLater();
    }@

    What i’m trying to say is the following: after i compile and run the project and I press one time the buttons, they work fine. If i close the form newe.ui and reopen it (void newA::on_addE1Info_clicked()
    ), this form will reopen twice. If i close both of them and reopen it, this form will open x 4, and so on; every time the number of same form opened windows are = the number of the “sesions” opened after the compile/build x 2 (times 2).

    Why is happening this?

    Ps: probably you will notice a lot of issues with my code, don’t consider them please, this is a draft.

    Thanks for your input.


  • Lifetime Qt Champion

    Hi,

    Please enclose your code with coding tags, it will make it more readable. One thing that is problematic is that each time you call on_actionA_2_triggered or on_addE1Info_clicked you are adding the same connection again which means these slots will be called twice as much each time



  • SGaist you are right, next time I will use the codding tags. I corrected this issue very easy, taking out the second slot call. Thank you for your comment. This thread is SOLVED.


  • Lifetime Qt Champion

    You're welcome !

    You can always edit your post and add the tags :)



  • like this? :)


  • Lifetime Qt Champion

    Exactly :)


Log in to reply
 

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