QDialog派生的窗体立即关闭 [英] QDialog-derived form closes immediately

查看:45
本文介绍了QDialog派生的窗体立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在主要表单上单击按钮时,我试图使带有数据表的表单出现.但是,实际上,第二种形式的闪烁"-看起来少于第二种形式-然后消失了.可能是什么原因,应如何解决?

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();
}

推荐答案

问题是,您在 on_goodTable_clicked 方法中的堆栈上有一个本地对话框对象.因此,您创建了 view ,调用了 show ,该对话框显示了对话框并立即返回,然后在离开函数时销毁了 view .如果您仍然将对话框设置为模态,为什么不使用 show QDialog exec 方法.它显示该对话框并阻塞主窗口,直到您单击该对话框的确定"或取消"按钮,然后 exec 最终返回.如果要使用非模式对话框(意味着在打开对话框时主窗口可以工作),则需要动态创建对话框(或使其成为主窗口的成员,或者两者兼而有之.)

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

这篇关于QDialog派生的窗体立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆