QT-功能-实现主页面和子页面之间的跳转(最简单)

用简单的方法实现两个界面之间的跳转

1.新建一个带ui界面的工程,其基类为mainwindow

2.向工程中Add new添加一个新的界面

在这里插入图片描述

3.选中添加界面模板,可选择Dialog对话框类或者mainwindow类等,视情况而定。我这里选择mainwindow类

在这里插入图片描述

4.将新添加的界面的类名设置为与主页面不同的类名,如mainwindow2,之后点击下一步

在这里插入图片描述

工程构建如图

在这里插入图片描述

5.代码实现

主页面mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMouseEvent>
#include <QPushButton>
#include <QPoint>
#include <mainwindow2.h>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void dealmainwindow2();           //处理子窗口槽函数

private:
    Ui::MainWindow *ui;
    //定义关闭按钮
    QPushButton * btnClose;
    //定义QPoint坐标常量
    QPoint last;

    //声明新按钮
    QPushButton * btnopen;
    //声明新窗口
    MainWindow2 w2;

private slots:
   void showMainwindow2();            //显示子页面槽函数
   //void reshowMainwindow();         //回到主页面槽函数

protected:

   /*
    *
    //私有信号保护

    //鼠标按下
    void MousePressEvent(QMouseEvent *e);
    //鼠标移动
    void MouseMoveEvent(QMouseEvent *e);
    //鼠标释放
    void MouseReleaseEvent(QMouseEvent *e);
    *
    */
};

#endif // MAINWINDOW_H

主页面mainwindow.cpp

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


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

/********************第一部分********************************/
    this->setWindowTitle("窗体变化");
    this->setMaximumSize(300,300);                      //最大尺寸
    this->setMinimumSize(300,300);                      //最小尺寸
    this->setStyleSheet("background:white");            //设置背景色
    this->setWindowIcon(QIcon(":/image/pig.ico"));      //设置窗体图片
    this->setWindowFlags(Qt::WindowCloseButtonHint);    //去掉最大化和最小化按钮,保留关闭按钮

/**********************************************************/

/**************************第二部分*************************/

    //新增关闭按钮
    btnClose = new QPushButton(this);
    btnClose->setText("关闭按钮");
    btnClose->move(100,100);
    //链接信号与槽函数
    connect(btnClose,SIGNAL(clicked()),this,SLOT(close()));


    //定义打开的第二个窗口的按钮
    btnopen = new QPushButton(this);
    btnopen->setGeometry(QRect(100,200,100,25));
    btnopen->setText("打开窗口");
    connect(btnopen,SIGNAL(clicked()),this,SLOT(showMainwindow2()));
    //connect(w2,SIGNAL(sensignal()),this,SLOT(reshowMainwindow()));
	
	//处理子窗口的信号
    connect(&w2,&MainWindow2::Mysignal,this,&MainWindow::dealmainwindow2);
/**********************************************************/


}

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

/**************************鼠标事件*************************/
/*
//
void MousePressEvent(QMouseEvent *e)
{
    QPoint last = e->globalPos();
}

//
void MouseMoveEvent(QMouseEvent *e)
{
    QPoint last;
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    last = e->globalPos();
    move(last.x() + dx, last.y() + dy);
}

//
void MouseReleaseEvent(QMouseEvent *e)
{
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    move(x()+dx, y()+dy);

}

*/

/**********************************************************/

void MainWindow::dealmainwindow2()
{
	//按钮回到主窗口
    w2.hide();
    this->show();

}

void MainWindow::showMainwindow2()
{
    //按钮显示mainwindow2窗口
    w2.show();
}


子页面mainwindow2.h

#ifndef MAINWINDOW2_H
#define MAINWINDOW2_H

#include <QMainWindow> 
#include <QPushButton>
namespace Ui {
class MainWindow2;
}

class MainWindow2 : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow2(QWidget *parent = nullptr);
    ~MainWindow2();
    
    void Sendslot();                //发送信号槽函数

private:
    Ui::MainWindow2 *ui;
    
signals:
    void Mysignal();                //返回值信号

private:
    QPushButton *returnButton;      //控件声明

private slots:
    //void returnMainwindow();        //子页面

};

#endif // MAINWINDOW2_H

子页面mainwindow2.cpp

#include "mainwindow2.h"
#include "ui_mainwindow2.h"
#include <mainwindow.h>

MainWindow2::MainWindow2(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow2)
{
    ui->setupUi(this);
    this->setWindowTitle("窗口2");
    this->resize(300,300);

    returnButton = new QPushButton(this);
    returnButton->setText("返回主窗口");
    returnButton->move(100,150);

    connect(returnButton,&QPushButton::clicked,this,&MainWindow2::Sendslot);


}

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

void MainWindow2::Sendslot()
{
    emit Mysignal();
}

/*
void MainWindow2::returnMainwindow()
{
    this->close();

}
*/

main.cpp不做改变,为默认

6.效果展示

在这里插入图片描述
在这里插入图片描述

7.不足与需改进之处

1.通过工程文件Add new添加一个新页面的方式,没能很好的通过槽函数的形式销毁界面。可以通过新建一个class界面类的方式更好的对界面进行操作转换

2.可通过调用函数的形式,实现界面的转换和销毁,待考究

3.这里取消了对鼠标事件的功能实现

8.改进

第二点已经就行优化

发布了43 篇原创文章 · 获赞 7 · 访问量 9045

猜你喜欢

转载自blog.csdn.net/qq_41488943/article/details/96453771