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

01.01 QT信号和槽

1.思维导图

2.写1个widget窗口,窗口里面放1个按钮,按钮随便叫什么

创建2个widget对象
Widget w1,w2
w1.show()
w2不管
要求:点击 w1.btn,w1隐藏,w2显示
点击 w2.btn ,w2隐藏,w1 显示

程序代码:

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>

class Widget : public QWidget {
    QPushButton* btn;
    Widget* otherWindow; // 指向另一个窗口的指针

public:
    Widget(const QString& buttonName, Widget* other = nullptr, QWidget* parent = nullptr);
    void setOtherWindow(Widget* other) { otherWindow = other; }

private slots:
    void onButtonClicked();
};

Widget::Widget(const QString& buttonName, Widget* other, QWidget* parent)
    : QWidget(parent), otherWindow(other) {
    // 创建按钮
    btn = new QPushButton(buttonName, this);

    // 设置布局
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(btn);
    setLayout(layout);

    // 连接按钮的点击信号到槽函数
    connect(btn, &QPushButton::clicked, this, &Widget::onButtonClicked);
}

void Widget::onButtonClicked() {
    if (otherWindow) {
        this->hide();       // 隐藏当前窗口
        otherWindow->show(); // 显示另一个窗口
    }
}

int main(int argc, char** argv) {
    QApplication app(argc, argv); // QT应用程序的入口类

    // 创建两个 Widget 对象
    Widget w1("Hide Me (W1)");
    Widget w2("Hide Me (W2)");

    // 设置窗口标题
    w1.setWindowTitle("Window 1");
    w2.setWindowTitle("Window 2");

    // 设置相互指向
    w1.setOtherWindow(&w2);
    w2.setOtherWindow(&w1);

    // 显示 w1,w2不管
    w1.show();

    return app.exec(); // 执行QT应用程序的事件循环
}


http://www.kler.cn/a/569539.html

相关文章:

  • FastExcel vs EasyExcel vs Apache POI:三者的全面对比分析
  • Kali Linux 2024.4版本全局代理(wide Proxy)配置,适用于浏览器、命令行
  • 初阶数据结构(C语言实现)——3顺序表和链表(2)
  • React+Antd-Mobile遇到的问题记录
  • 主题爬虫(Focused Crawler)
  • 内网渗透测试-Vulnerable Docker靶场
  • 【开源免费】基于SpringBoot+Vue.JS医院药品管理系统(JAVA毕业设计)
  • 如何在Spring Boot项目中集成JWT实现安全认证?
  • nio多线程版本
  • 大夏龙雀科技4G Cat1 CT511-AT0 MQTT联网实战教程
  • C++格式讲解
  • PhyloSuite v1.2.3安装与使用-生信工具049
  • 大模型学习笔记-基于《Deep Dive into LLMs like ChatGPT》
  • 第1章 基础网络和安全工具(网络安全防御实战--蓝军武器库)
  • 谈谈 Node.js 中的模块系统,CommonJS 和 ES Modules 的区别是什么?
  • 不要升级,Flutter Debug 在 iOS 18.4 beta 无法运行,提示 mprotect failed: Permission denied
  • ubuntu:桌面版磁盘合并扩容
  • Stapler: 1靶场渗透测试
  • 中间件专栏之Redis篇——Redis的三大持久化方式及其优劣势对比
  • LeetCode-81. 搜索旋转排序数组 II