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() function. The format should be a Latin-1 string.

Example:

 qDebug("Items in list: %d", myList.size());

If you include <QtDebug>, a more convenient syntax is also available:

 qDebug() << "Brush:" << myQBrush << "Other value:" << i;

With this syntax, the function returns a QDebug object that is configured to use the QtDebugMsg message type. It automatically puts a single space between each item, and outputs a newline at the end. It supports many C++ and Qt types.

To suppress the output at run-time, install your own message handler with qInstallMessageHandler().

Note: This function is thread-safe.

信号和槽

我们要看某个控件的信号时,可在文档中查找,如果找不到,就去看他的父类。

连接信号和槽 


     * param1 信号发送者
     * param2 发送的信号(函数的地址)
     * param3 信号的接受者
     * param4 处理的函数

    connect(btn, &QPushButton::clicked, this, &MainWindow::close);

类比python中:btn.clicked.connect(self.close)

当按钮按下时(触发信号 ),self(mainwindow)的close(槽函数)会执行 

注意:是clicked, 不要写成click!!!

编译运行,我们发现并不起作用,这是因为我们使用的是QMainWindow,受到mainwindow.ui文件影响,我们的按钮不会有任何交互效果。我们用qtdesigner绑定信号。如下,点击加号设置发送者,信号,接收者,槽。如果你想要让窗口华丽些,可以设置MainWindow的styleSheet属性。(qss属性后面会探讨)

我们也可以手动绑定,我们可以看到对象这一栏可以为对象命名,(或者右键命名)命名后可以通过ui->xxx访问,若修改后无自动提示,则需要ctrl+s保存UI文件再 ,如果还不行,项目-取消shallow build,构建。

小练习:三个按钮,一个最小化窗口,一个最大化窗口,一个关闭窗口

    ui->setupUi(this);

    //关联信号
    connect(ui->closeButton, &QPushButton::clicked, this, &QMainWindow::close);
    connect(ui->maxButton, &QPushButton::clicked, this, &QMainWindow::showMaximized);
    connect(ui->minButton, &QPushButton::clicked, this, &QMainWindow::showMinimized);

 Lambda表达式
 

注意:pro中要有CONFIG += c++11

[](参数){语句}

此时会报错,这时候看不到局部变量
QPushButton *btn = new QPushButton ;
...
[](){
    btn->setText("lambda");
};



此时不会报错,以值传递的方式
QPushButton *btn = new QPushButton ;
...
[=](){
    btn->setText("lambda");
};


【=】函数体内可以使用lambda所在作用范围内的所有可见局部变量(包括this),并且是值传递方式
(相当于编译器自动为我们按值传递了所有局部变量)

【&】函数体内可以使用lambda所在作用范围内的所有可见局部变量(包括this),并且是引用传递方式
(相当于编译器自动为我们按引用传递了所有局部变量)

【this】可用所在类的成员变量

【xxx】可以访问xxx(不能修改xxx,要修改就得添加mutable修饰),如
[btn](){
    btn->setText("lambda");
};是可以的

【&xxx】引用
【=,&a, &b】a和b引用传值外,其他按值传递


multable 可以修改按值传递的,但修改的是传递的拷贝,不是本体
eg: [m]()multable{m=100+10;qDebug() << m;} 


要返回类型,eg:[]()->int{return 0;}

 这样的好处是可以连接一些有参数的槽函数

connect(ui->minButton, &QPushButton::clicked, this, [=](){this->close()});

UI这边也设置下。右侧显示红色禁止标志是因为控件没有设置layout,要在该控件上右键,选择布局,进行设置。否则子控件的大小不会随着父控件的改变而改变。 

  • 33
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值