当前位置: 首页 > news >正文

株洲专业做网站设计的西安专业网络推广平台

株洲专业做网站设计的,西安专业网络推广平台,单页网站推广,成都私人做网站文章目录 方式一 :使用QStackedWidget讲解代码结构main.cpp完整代码运行结果: 方式二 :代码结构完整代码mainwindow.hnewmainwindow.hmain.cppmainwindow.cppnewmainwindow.cppmainwindow.uinewmainwindow.ui 效果 方式一 :使用QS…

文章目录

  • 方式一 :使用QStackedWidget
    • 讲解
    • 代码结构
    • main.cpp完整代码
    • 运行结果:
  • 方式二 :
    • 代码结构
    • 完整代码
      • mainwindow.h
      • newmainwindow.h
      • main.cpp
      • mainwindow.cpp
      • newmainwindow.cpp
      • mainwindow.ui
      • newmainwindow.ui
    • 效果

方式一 :使用QStackedWidget

讲解

在Qt中,可以使用QStackedWidget来实现两个UI界面的互相转换。QStackedWidget是一个堆叠窗口小部件,可以在其中添加多个子窗口,并且只显示其中一个子窗口。


注意:QStackedWidget只能用来装widget,不能装mainwindow!!

注意:
上面这种想法是错的!
下面这种想法才是对的!

注意:QStackedWidget既可以能用来装widget,也可以装mainwindow!!!(想必QDialog也可以)


项目名:PageSwitching1
使用QStackedWidget这种方法,其实就是先创建一个QWidget作为”容器“,然后再创建一个QStackedWidget部件作为这个QWidget的唯一部件,
然后再将不同的ui页面放入到QStackedWidget中即可!

注意:

  • 1.如果你想在main.cpp中创建其他ui页面实力(比如:MainWindow mainw;),就需要加上他的头文件,如:
    #include “mainwindow.h”;
  • 2.如果你想在main.cpp中,通过ui变量访问这个ui界面上的某个部件,你还需要加上他的ui头文件,如:
    #include “ui_mainwindow.h”
    并且,你还要让他的ui变量公开,如:
public:Ui::MainWindow *ui;

代码结构

在这里插入图片描述
说明:项目名为1111111111是随便取的,
然后,只有main.cpp是存放了自己写的代码,其他都是编译器自动生成。
然后,在mainwindow.h文件中,将Ui::MainWindow *ui;设置为了pubilc。
widget.ui是空白的。
mainwindow.ui如下所示:(最主要其实就是加了一个button,用来跳转到其他页面,那个日历没什么作用)
在这里插入图片描述

main.cpp完整代码

#include "mainwindow.h"
#include "ui_mainwindow.h"//两两一组,mainwindow.h与ui_mainwindow.h缺一不可!#include <QApplication>
#include <QLabel>
#include <QPushButton>
#include <QStackedWidget>
#include <QVBoxLayout>int main(int argc, char *argv[])
{QApplication a(argc, argv);// 创建主窗口QWidget mainWindow;mainWindow.setWindowTitle("PageSwitching1");mainWindow.setFixedSize(600,400);// 创建堆叠窗口QStackedWidget stackedWidget(&mainWindow);// 创建第一个UI界面QWidget uiPage1;QVBoxLayout layout1(&uiPage1);QLabel label1("Page 1");QPushButton button1("Go to Page 2");layout1.addWidget(&label1);layout1.addWidget(&button1);// 创建第二个UI界面QWidget uiPage2;QVBoxLayout layout2(&uiPage2);QLabel label2("Page 2");QLabel label22("Page 22");QPushButton button2("Go to Page 3");layout2.addWidget(&label2);layout2.addWidget(&label22);layout2.addWidget(&button2);MainWindow mainw;// 将两个UI界面添加到堆叠窗口stackedWidget.addWidget(&uiPage1);stackedWidget.addWidget(&uiPage2);stackedWidget.addWidget(&mainw);// 信号槽连接,实现界面切换QObject::connect(&button1, &QPushButton::clicked, [&stackedWidget]() {stackedWidget.setCurrentIndex(1); // 切换到第二个界面});QObject::connect(&button2, &QPushButton::clicked, [&stackedWidget]() {stackedWidget.setCurrentIndex(2); // 切换到第一个界面});QObject::connect((mainw.ui->pushButton), &QPushButton::clicked, [&stackedWidget]() {stackedWidget.setCurrentIndex(0); // 切换到第一个界面});QLabel label3("Page 3");// 设置主窗口布局QVBoxLayout mainLayout(&mainWindow);mainLayout.addWidget(&stackedWidget);mainLayout.addWidget(&label3);// 显示主窗口mainWindow.show();return a.exec();
}

说明:你可以使用信号与槽,在 当前页面 跳转到 任意其他页面 !

运行结果:

在这里插入图片描述

点击“Go to Page 3”后,
在这里插入图片描述

点击“PushButton”后,

在这里插入图片描述

补充:你其实可以在每个页面设计两个button,然后通过信号与槽跳转到其他任意两个页面(这个是可以实现的)

方式二 :

hide(),show(),信号与槽,拉姆达表达式

代码结构

在这里插入图片描述

完整代码

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();signals:void changenew();private slots:void on_pushButton_clicked();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

newmainwindow.h

#ifndef NEWMAINWINDOW_H
#define NEWMAINWINDOW_H#include <QMainWindow>namespace Ui {
class NewMainWindow;
}class NewMainWindow : public QMainWindow
{Q_OBJECTpublic:explicit NewMainWindow(QWidget *parent = nullptr);~NewMainWindow();
signals:void changeFirst();
private slots:void on_pushButton_clicked();private:Ui::NewMainWindow *ui;
};#endif // NEWMAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "newmainwindow.h"
#include "ui_newmainwindow.h"#include <QApplication>
#include <QtWidgets> // Include the necessary Qt header for the widgets module
#include <QApplication> // Include the necessary Qt header for the application module
int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;NewMainWindow nw;w.setWindowTitle("The First Interface");nw.setWindowTitle("The Second Interface");QObject::connect(&w,&MainWindow::changenew,&nw,[&](){nw.show();});QObject::connect(&nw,&NewMainWindow::changeFirst,&w,[&](){w.show();});w.show();//nw.show();return a.exec();
}

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()
{this->hide();emit changenew();
}

newmainwindow.cpp

#include "newmainwindow.h"
#include "ui_newmainwindow.h"NewMainWindow::NewMainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::NewMainWindow)
{ui->setupUi(this);
}NewMainWindow::~NewMainWindow()
{delete ui;
}void NewMainWindow::on_pushButton_clicked()
{this->hide();emit changeFirst();
}

mainwindow.ui

在这里插入图片描述

newmainwindow.ui

在这里插入图片描述

效果

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

http://www.hkea.cn/news/192903/

相关文章:

  • 做房产中介网站怎么注册一个自己的网站
  • 天津网站设计成功柚米全网推广成功再收费
  • 建设公司网站靠谱吗企业网站设计制作
  • 电子商务学什么课程内容兰州搜索引擎优化
  • 沧州网站建设制作设计优化能打开的a站
  • 石家庄网站建设推广报价怎么让百度快速收录网站
  • 建设局网站上开工日期选不了制作网站需要多少费用
  • 犬舍网站怎么做网页推广怎么做
  • 镇江核酸检测最新通知如何优化网页加载速度
  • wpf入可以做网站吗竞价托管外包费用
  • 公司设计网站需要包含什么资料优化排名软件
  • 日本樱花云服务器wan亚马逊seo关键词优化软件
  • layui框架的wordpress厦门站长优化工具
  • 微网站设计尺寸培训课程总结
  • 保险平台官网湖北搜索引擎优化
  • 西安微信小程序制作公司关键词优化方法
  • 手机网站建设用乐云seo搜索引擎是什么意思啊
  • 昆明做大的网站开发公司google网页搜索
  • 做网站运营需要什么证宁波靠谱营销型网站建设
  • 天津进口网站建设电话青岛网站建设公司
  • 游戏币网站建设win7优化大师官方网站
  • 技术专业网站建设班级优化大师网页版登录
  • 外国网站上做雅思考试台州百度推广优化
  • 男女做那种的的视频网站国内最好的搜索引擎
  • 泉州做网站优化价格成功品牌策划案例
  • 做网站去哪个平台资源优化排名网站
  • 备案的网站名称可以改吗百度青岛代理公司
  • 专做进口批发的网站关键词优化多少钱
  • 做网站有了空间在备案吗百度权重高的网站有哪些
  • 做空间的网站著名的网络营销案例