Qt5--多窗口编程实例

  • 登录后进入主窗口
  • 主窗口打开对话框(自带的类无ui文件)
  • 主窗口打开其他窗口(自定义的类+ui)
  • 主窗口打开其他窗口并通信 (自定义的类+ui)

登录后进入主窗口

1、新建工程

(1)Crtl + N 新建工程

这里写图片描述

(2)取名window工程

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

2、往工程里面添加自定义类和ui文件

(1)在工程名字上面点击“Add New ….”

这里写图片描述

(2)这里可以随便选择,我选Dialog without Buttons。

这里写图片描述

(3)修改类名

这里写图片描述

(4)QtCreator自动把新建的窗口类信息添加进.pro工程配置文件中去

这里写图片描述

(5)工程中就有了文件了

这里写图片描述

(6)配置文件中也自动添加了

这里写图片描述

(7)设计登录界面

这里写图片描述

(8)配置登录界面的信号与槽:鼠标点击工具栏的按键,选择clicked(),accept()然后确定。

这里写图片描述

3、编写代码完成首先进入登录页面,按下按钮之后进入主窗口的逻辑

(1)window.h里面添加登录视图类的头文件

这里写图片描述

(2)main.cpp里面添加

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    LoginDlg dlg;
    if (dlg.exec() == QDialog::Accepted){//利用Accepted返回值判断按键是否被按下

        w.show();// 如果被按下,显示主窗口

        return a.exec();// 程序一直执行,直到主窗口关闭
    }
    else return 0;//如果没有被按下,则不会进入主窗口,整个程序结束运行
}

效果如动图
这里写图片描述

源码下载


主窗口打开对话框

代码继续沿用前面的。

(1)在主窗口的视图界面里面添加一个按键,然后使用信号与槽,打开一个对话框。

这里写图片描述

这里写图片描述

(2)在点击实现函数下写代码打开一个对话框
首先在window.h中添加对话框的头文件QDialog

这里写图片描述

然后在window.cpp中的点击实现函数实例化一个对话框。

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QDialog *dlg = new QDialog(this);
    void MainWindow::on_pushButton_clicked()
{
    QDialog *dlg = new QDialog(this);
//    dlg->show();  // 可以连续打开多个,即父窗口可以点击
    dlg->exec();  // 父窗口不可点击
}

Qt里面还内置了很多对话框

  • QColorDialog:选择颜色;
  • QFileDialog:选择文件或者目录;
  • QFontDialog:选择字体;
  • QInputDialog:允许用户输入一个值,并将其值返回;
  • QMessageBox:模态对话框,用于显示信息、询问问题等;
  • QPageSetupDialog:为打印机提供纸张相关的选项;
  • QPrintDialog:打印机配置;
  • QPrintPreviewDialog:打印预览;
  • QProgressDialog:显示操作过程。

比如QMessageBox的使用案例:
案例一:

void MainWindow::on_pushButton_clicked()
{
//    QMessageBox *dlg = new QMessageBox(this);
    if (QMessageBox::Yes == QMessageBox::question(this,
                                                  tr("Question"),
                                                  tr("Are you OK?"),
                                                  QMessageBox::Yes | QMessageBox::No,
                                                  QMessageBox::Yes)) {
        QMessageBox::information(this, tr("Hmmm..."), tr("I'm glad to hear that!"));
    } else {
        QMessageBox::information(this, tr("Hmmm..."), tr("I'm sorry!"));
    }
}

效果:

案例二:

{
    QMessageBox msgBox;
    msgBox.setText(tr("The document has been modified."));
    msgBox.setInformativeText(tr("Do you want to save your changes?"));
    msgBox.setDetailedText(tr("Differences here..."));
    msgBox.setStandardButtons(QMessageBox::Save
                              | QMessageBox::Discard
                              | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
    switch (ret) {
    case QMessageBox::Save:
        qDebug() << "Save document!";
        break;
    case QMessageBox::Discard:
        qDebug() << "Discard changes!";
        break;
    case QMessageBox::Cancel:
        qDebug() << "Close document!";
        break;
}

效果图:

这里写图片描述


主窗口打开其他窗口

代码继续使用前面的。

(1)在工程里面新建一个Qt设计表单类

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

在新的视图页面里面添加一段文字。

这里写图片描述

(2)编写逻辑代码

首先还是在window.h中添加新的视图类的头文件

这里写图片描述

然后在主窗口的视图页面里面添加一个按钮

这里写图片描述

打开新建的按钮的信号与槽函数并添加代码

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QMessageBox msgBox;
    msgBox.setText(tr("The document has been modified."));
    msgBox.setInformativeText(tr("Do you want to save your changes?"));
    msgBox.setDetailedText(tr("Differences here..."));
    msgBox.setStandardButtons(QMessageBox::Save
                              | QMessageBox::Discard
                              | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
    switch (ret) {
    case QMessageBox::Save:
        qDebug() << "Save document!";
        break;
    case QMessageBox::Discard:
        qDebug() << "Discard changes!";
        break;
    case QMessageBox::Cancel:
        qDebug() << "Close document!";
        break;
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    myForm *form = new myForm(this);
    form->exec();  // 打开另外一个窗口
}

效果图:
这里写图片描述


主窗口打开其他窗口并通信

  • 使用信号与槽
  • 子窗口向父窗口传输数据

(1)子窗口的配置

这里写图片描述

(2)子窗口:myform.h—-增加传输数据的信号、按键响应的槽

#ifndef MYFORM_H
#define MYFORM_H

#include <QDialog>

namespace Ui {
class myForm;
}

class myForm : public QDialog
{
    Q_OBJECT

public:
    explicit myForm(QWidget *parent = 0);
    ~myForm();

signals:
    void sendData(QString);  // 用来传输数据的信号

private slots:
    void on_pushButton_clicked();  // 按键点击

private:
    Ui::myForm *ui;
};

#endif // MYFORM_H

子窗口:myform.cpp—按键响应的实现

#include "myform.h"
#include "ui_myform.h"

myForm::myForm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::myForm)
{
    ui->setupUi(this);
}

myForm::~myForm()
{
    delete ui;
}

void myForm::on_pushButton_clicked()
{
    emit sendData(ui->lineEdit->text()); //获取lineEdit的输入并且传递出去
    this->close();  // 传输完毕之后关闭子窗口
}

(3)主窗口的实现
视图的实现增加一个显示的textEdit

这里写图片描述

主窗口文件mainwindow.h—-增加了接收数据的槽

这里写图片描述

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "logindlg.h"
#include <QDialog>
#include <QMessageBox>
#include <QDebug>
#include "myform.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void receiveData(QString data);  //接收传递过来的数据的槽

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp—–增加了信号与槽(数据传输的)实现函数,以及接收数据槽函数的实现

这里写图片描述

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this); 
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QMessageBox msgBox;
    msgBox.setText(tr("The document has been modified."));
    msgBox.setInformativeText(tr("Do you want to save your changes?"));
    msgBox.setDetailedText(tr("Differences here..."));
    msgBox.setStandardButtons(QMessageBox::Save
                              | QMessageBox::Discard
                              | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
    switch (ret) {
    case QMessageBox::Save:
        qDebug() << "Save document!";
        break;
    case QMessageBox::Discard:
        qDebug() << "Discard changes!";
        break;
    case QMessageBox::Cancel:
        qDebug() << "Close document!";
        break;
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    myForm *form = new myForm(this);
    // 连接信号与槽
    connect(form, SIGNAL(sendData(QString)), this, SLOT(receiveData(QString)));
    form->show();
}

void MainWindow::receiveData(QString data)
{
    ui->textEdit->setText(data);
}

效果图

这里写图片描述

下载链接

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值