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();
}
The problem is, that you have a local dialog object on the stack in your
on_goodTable_clickedmethod. So you create theview, callshow, which shows the dialog and immediately returns, then yourviewget’s destroyed as you leave the function. If you make the dialog modal anyway, why not useQDialog‘sexecmethod intead ofshow. It shows the dialog and blocks the main window until you clicked the dialog’s Ok or cancel button and thenexecfinally 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).