Qt学习记录---Signal

创建QObject时,我们会传递一个QObject指针作为它的父对象,此时 ,该QObject将会自动添加到它的父对象的children()列表中。Qt为了防止内存泄漏,我们创建的QObject对象如果不添加到对象树中,就会被delete。

当这个对象析构时,所有子对象都会被释放(递归式的)

添加新文件

自定义控件时,可选择Base Class。这样新建文件,Qt会自动在Qmake中添加,放止编译出错。

使用QDebug 

#include <QDebug>

...

qDebug() << 1;

...

文档:

qDebug(const char *message, ...)

Calls the message handler with the debug message message. If no message handler has been installed, the message is printed to stderr. Under Windows the message is sent to the console, if it is a console application; otherwise, it is sent to the debugger. On QNX, the message is sent to slogger2. This function does nothing if QT_NO_DEBUG_OUTPUT was defined during compilation.

If you pass the function a format string and a list of arguments, it works in similar way to the C printf() fu

### Qt信号与槽机制学习教程 #### 一、基础概念 为了使用信号和槽,类必须是从`QObject`派生而来,这是因为信号和槽机制依赖于Qt的元对象系统[^1]。 #### 二、简单示例 下面是一个简单的例子来展示如何创建并连接两个自定义信号。这个案例展示了如何在一个按钮点击事件发生时触发另一个窗口部件中的特定行为: ```cpp // mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "showvoid.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); signals: void SendVoid(); // 定义一个无参信号 private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; ShowVoid* showVoidInstance; // 创建ShowVoid实例 }; #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); showVoidInstance = new ShowVoid(); // 连接按钮点击信号到SendVoid信号上 connect(ui->pushButton, SIGNAL(clicked()), this, SIGNAL(SendVoid())); // 将当前窗口组件的SendVoid信号连接至其他组件的方法或信号 connect(this,SIGNAL(SendVoid()),showVoidInstance,SLOT(handleSignal())); } MainWindow::~MainWindow() { delete ui; } ``` 在这个例子中,在构造函数里加入了`connect`调用语句,其中第四个参数指定了要关联的目标信号名称。当按钮被按下(`clicked`)的时候,不仅会发出自身的信号,还会立即触发展示窗口内的相应处理逻辑[^2]。 #### 三、进一步理解 对于更深入的理解,可以尝试构建更加复杂的场景,比如多级信号传递链路的设计;也可以研究官方文档和其他开源项目的源码实现方式。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值