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

qt-C++笔记之两个窗口ui的交互

qt-C++笔记之两个窗口ui的交互

code review!

文章目录

  • qt-C++笔记之两个窗口ui的交互
    • 0.运行
    • 1.文件结构
    • 2.先创建widget项目,搞一个窗口ui出来
    • 3.项目添加第二个widget窗口出来
    • 4.补充代码
      • 4.1.qt_widget_interaction.pro
      • 4.2.main.cpp
      • 4.3.widget.h
      • 4.4.widget.cpp
      • 4.5.second_widget.h
      • 4.6.second_widget.cpp
      • 4.7.widget.ui
      • 4.8.second_widget.ui

0.运行

在这里插入图片描述

1.文件结构

在这里插入图片描述

2.先创建widget项目,搞一个窗口ui出来

在这里插入图片描述

3.项目添加第二个widget窗口出来

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

4.补充代码

4.1.qt_widget_interaction.pro

代码

QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \second_widget.cpp \widget.cppHEADERS += \second_widget.h \widget.hFORMS += \second_widget.ui \widget.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

4.2.main.cpp

在这里插入图片描述

代码

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

4.3.widget.h

在这里插入图片描述

代码

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <second_widget.h>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_push_second_widget_clicked();void show_widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H

4.4.widget.cpp

在这里插入图片描述

代码

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);}Widget::~Widget()
{delete ui;
}void Widget::show_widget()
{this->show();
}void Widget::on_push_second_widget_clicked()
{second_widget* f = new second_widget;f->show();this->hide();connect(f,SIGNAL(close_and_open()),this,SLOT(show_widget()));
}

4.5.second_widget.h

在这里插入图片描述

代码

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

4.6.second_widget.cpp

在这里插入图片描述

代码

#include "second_widget.h"
#include "ui_second_widget.h"second_widget::second_widget(QWidget *parent) :QWidget(parent),ui(new Ui::second_widget)
{ui->setupUi(this);
}second_widget::~second_widget()
{delete ui;
}void second_widget::on_pushButton_clicked()
{emit close_and_open();this->hide();
}

4.7.widget.ui

代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>Widget</class><widget class="QWidget" name="Widget"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>Widget</string></property><widget class="QLabel" name="label"><property name="geometry"><rect><x>350</x><y>210</y><width>171</width><height>41</height></rect></property><property name="text"><string>first_widget</string></property></widget><widget class="QPushButton" name="push_second_widget"><property name="geometry"><rect><x>70</x><y>340</y><width>281</width><height>51</height></rect></property><property name="text"><string>open scond_widget</string></property></widget></widget><resources/><connections/>
</ui>

4.8.second_widget.ui

代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>second_widget</class><widget class="QWidget" name="second_widget"><property name="geometry"><rect><x>0</x><y>0</y><width>460</width><height>312</height></rect></property><property name="windowTitle"><string>Form</string></property><widget class="QLabel" name="label"><property name="geometry"><rect><x>100</x><y>120</y><width>211</width><height>41</height></rect></property><property name="text"><string>second_widget</string></property></widget><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>20</x><y>210</y><width>411</width><height>41</height></rect></property><property name="text"><string>close_second_and_open_first</string></property></widget></widget><resources/><connections/>
</ui>
http://www.lryc.cn/news/237161.html

相关文章:

  • Redis-核心数据结构
  • 设计模式—结构型模式之外观模式(门面模式)
  • CentOS Stream 9-使用 systemd 管理自己程序时自定义日志路径
  • 动态页面调研及设计方案
  • 鸿蒙4.0开发笔记之DevEco Studio之配置代码片段快速生成(三)
  • HarmonyOS真机调试报错:INSTALL_PARSE_FAILED_USESDK_ERROR处理
  • webSocket基于面向对象二次封装
  • 【Web】PHP反序列化的一些trick
  • 【测试功能篇 01】Jmeter 压测接口最大并发量、吞吐量、TPS
  • 代码随想录算法训练营第四十九天| 123.买卖股票的最佳时机III 188.买卖股票的最佳时机IV
  • 11.20 知识总结(choices参数、MVC和MTV的模式、Django与Ajax技术)
  • 深度学习二维码识别 计算机竞赛
  • C#关于TimeSpan结构的使用和获取两个时间差
  • Git分支管理
  • 《视觉SLAM十四讲》-- 建图
  • 智能配电箱柜管理系统
  • 聊聊近些年 CPU 在微架构、IO 速率上的演进过程
  • PS学习笔记——移动工具
  • 信息中心网络提出的背景、研究现状及研究内容
  • 【计算机视觉】24-Object Detection
  • 【mac 解决eclipse意外退出】
  • mysql innodb buffer pool缓冲池命中率和命中了哪些表?—— 筑梦之路
  • 牛掰的dd命令,cpi0配合find备份(不会主动备份),od查看
  • pip list 和 conda list的区别
  • 多目标应用:基于多目标灰狼优化算法MOGWO求解微电网多目标优化调度(MATLAB代码)
  • LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字
  • linux nas
  • 控制您的音乐、视频等媒体内容
  • xlua源码分析(三)C#访问lua的映射
  • 2023 极术通讯-汽车“新四化”路上,需要一片安全山海