Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QDialog-derived form closes immediately

Tags:

c++

qt

I am trying to make a form with data table appear when the button is clicked on thr main form. However, in practice second form 'blinks' - appears less than on second - and then vanished. What could be the reason and how that should be fixed?

Here is the derived form header and source files' content:

#ifndef GOODTABLE_H
#define GOODTABLE_H

#include <QDialog>
#include <QSqlTableModel>
namespace Ui {
    class GoodTable;
}

class GoodTable : public QDialog
{
    Q_OBJECT

public:
    explicit GoodTable(QDialog *parent = 0);
    GoodTable(QDialog *parent,QSqlTableModel* model);
    ~GoodTable();

private:
    Ui::GoodTable *ui;
};

#endif // GOODTABLE_H

#include "goodtable.h"
#include "ui_goodtable.h"

GoodTable::GoodTable(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::GoodTable)
{
    ui->setupUi(this);
}
GoodTable::GoodTable(QDialog *parent,QSqlTableModel* model) :
    QDialog(parent),
    ui(new Ui::GoodTable)
{
    ui->setupUi(this);
    ui->tableView->setModel(model);
}
GoodTable::~GoodTable()
{
    delete ui;
}

The code creating second window:

void MainWindow::on_goodTable_clicked()
{
    QSqlTableModel model;

    initializeGoodModel(&model);
    //! [4]
    GoodTable view(NULL,&model);
    view.setWindowFlags(Qt::Window);
    view.setWindowModality(Qt::ApplicationModal);
    view.show();
}
like image 672
Srv19 Avatar asked Nov 02 '25 16:11

Srv19


1 Answers

The problem is, that you have a local dialog object on the stack in your on_goodTable_clicked method. So you create the view, call show, which shows the dialog and immediately returns, then your view get's destroyed as you leave the function. If you make the dialog modal anyway, why not use QDialog's exec method intead of show. It shows the dialog and blocks the main window until you clicked the dialog's Ok or cancel button and then exec finally returns. When you want a non-modal dialog (meaning your main window works, while the dialog is open), you need to create your dialog dynamically (or make it a member of your main window, or both).

like image 199
Christian Rau Avatar answered Nov 05 '25 08:11

Christian Rau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!