弹出对话框练习

来源:互联网 发布:设计装修房子的软件 编辑:程序博客网 时间:2024/05/16 11:48
-----------------------------------------------------------------------
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = 0);
    ~Widget();
private slots:
    void OpenDlg();
};
#endif // WIDGET_H
--------------------------------------------------------------
#include "widget.h"
#include <QPushButton>
#include <QDialog>
#include <QGridLayout>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QPushButton *quit = new QPushButton(tr("&OpenDialog"));
    quit->setFont(QFont("Times", 18, QFont::Bold));
    connect(quit, SIGNAL(clicked()), this, SLOT(OpenDlg()));
    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(quit, 0, 0);///估计从0,0位置开始
     setLayout(gridLayout); ///显示出来
}
Widget::~Widget()
{
}
void Widget::OpenDlg()
{
   //----弹出对话框----
    QDialog * dialog = new QDialog();
     dialog->setWindowTitle("A QDialog xxxWindow");
     dialog->setMinimumSize(200,80);
     dialog->show();   ///模态对话框
    // dialog->exec();  //调出非摸态对话框
   //弹出widget
    QWidget* widget1= new QWidget();
    widget1->setWindowTitle("1123124");
    widget1->show();
    //弹出MessageBox----
    QMessageBox::information(this,tr("hello"),tr("Mars"));
    //加载已知的ui界面
    /*QUiLoader uiLoader;
    QFile file("./embeddeddialog.ui");
    QWidget *myWidget = uiLoader.load(&file);  
//如没有QtUiTools 则在.pro文件中加入  CONFIG += uitools
    myWidget->show();
    */
}

模态对话框即无法转移焦点.必须关闭后方可释放焦点

"模态对话框":在Form1窗口上点击按钮弹出一个Form2窗口,在关闭Form2窗口之前,不能在Form1窗口上操作  "非模态对话框":在Form1窗口上点击按钮弹出一个Form2窗口,这个时候既可以在Form1窗口上操作,也可以在Form2窗口上操作

举例:  比如现在你在菜单里面打开页面Form:  当作模式窗体打开就用:  Form frm = new Form();  frm.ShowDialog();  而当作非模式窗体打开就用:  Form frm = new Form();  frm.Show();