QDialog 派生窗体立即关闭
我试图在单击主窗体上的按钮时显示带有数据表的窗体。然而,实际上,第二种形式会“闪烁”——出现的次数比第二种形式少——然后消失。可能是什么原因以及应该如何解决?
这是派生的表单标题和源文件的内容:
#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;
}
创建第二个窗口的代码:
void MainWindow::on_goodTable_clicked()
{
QSqlTableModel model;
initializeGoodModel(&model);
//! [4]
GoodTable view(NULL,&model);
view.setWindowFlags(Qt::Window);
view.setWindowModality(Qt::ApplicationModal);
view.show();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,您的
on_goodTable_clicked
方法中的堆栈上有一个本地对话框对象。因此,您创建view
,调用show
,它显示对话框并立即返回,然后当您离开该函数时,您的view
就会被销毁。如果您无论如何都要使对话框成为模态对话框,为什么不使用QDialog
的exec
方法而不是show
。它显示对话框并阻止主窗口,直到您单击对话框的“确定”或“取消”按钮,然后exec
最终返回。当您想要一个非模式对话框(意味着主窗口在对话框打开时工作)时,您需要动态创建对话框(或使其成为主窗口的成员,或两者兼而有之)。The problem is, that you have a local dialog object on the stack in your
on_goodTable_clicked
method. So you create theview
, callshow
, which shows the dialog and immediately returns, then yourview
get's destroyed as you leave the function. If you make the dialog modal anyway, why not useQDialog
'sexec
method intead ofshow
. It shows the dialog and blocks the main window until you clicked the dialog's Ok or cancel button and thenexec
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).