QT信号与槽及标准对话框(笔记)
强调的是对象间的通信
关键点:把握住是谁发出信号,怎么发出的信号,谁的槽来接收信号,接收到去用怎么样的函数实现槽的功能
一、信号和槽的介绍
信号
能被触发的一个东西
槽
关键是可以和信号连接起来使用,除此之外就是一个普通的成员函数
作用
对象间通信的高级接口
二、信号与槽的实现(连接)
基本语句
connect函数 第一个参数 一般是一个指针(信号的)
第二个参数,是信号的的函数,带参数
第三个参数是槽的指针 ,第四个是槽的函数,带参数
第五个是连接方式(一般不写,使用默认)
1.直接执行,阻塞
2.入队列去执行,不阻塞
3. 根据线程去自动选择()
其他关联的模式
一个信号连接多个槽,同样用connect函数
多个信号可以连接到一个槽
一个信号可以和另外一个信号相连接
注意点:
一般在构造函数中connect
connect的参数中,信号和槽的函数的参数只写个类型,没有实参,而且是qt已知的
信号和信号,信号和槽的链接成功,参数类型必须具有相同的类型和顺序以及个数
信号的参数比槽的参数多,多余的被忽略,会出错
用Dessigner连接信号槽
三、自定义信号和槽
实现过程
类的声明开始处加入Q_OBJECT 在函数声明前加入signal与slots关键字
信号只需要声明无需实现,使用的时候用emit signal(参数列表)
槽一般的权限是private,实现的时候和普通的c++成员函数一样。
注意:
1.注意其中的this的使用
2.在UI下进行goto slot 的使用(对一个对象使用右键)
3.关键是怎么去自定义一个信号和槽
(记得补上过程)
信号与信号
信号传给一个信号
QTimer 定时器
定时发送信号
继承于QOBJECT,所以没有界面
四、常用标准对话框
QFileDialog
QMessageBox
练习一
去做这样一个东西 显示隐藏上面的字体,可以关闭窗口
同样拉滑块,实现进度的修改
这些可以使用代码和UI两种方式来实现(实际去做,体会到底代码去实现和UI去实现有什么区别)
下面是代码
#ifndef WIDGET_H
#define WIDGET_H
#include
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
signals:
void mysignal(int);
private slots:
void myslot(int);
};
#endif// WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "QtDebug"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);/* connect(ui->showbutton,SIGNAL(clicked()),ui->label,SLOT(show()));
connect(ui->hidebutton,SIGNAL(clicked()),ui->label,SLOT(hide()));
connect(ui->quitebutton,SIGNAL(clicked()),this,SLOT(close()));
connect(ui->horizontalSlider,SIGNAL(valueChanged(int))
,ui->progressBar,SLOT(setValue(int)));*/
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
this,SLOT(myslot(int)));//myslot
connect(this,SIGNAL(mysignal(int)),this,SLOT(close()));//mysignal,//这种情况参数就被忽略了,但是可以执行,不会报错,这种参数的忽略是可以的
}
Widget::~Widget()
{
delete ui;
}
void Widget::myslot(int a)
{
ui->label->setText(QString::number(a));
if(100==a)
{
emit mysignal(100);
qDebug()<
}
}
#include
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
练习二 使用定时器,而且融合了信号和信号的传递,goto的使用,UI槽的使用和编程槽的使用
代码如下
#ifndef WIDGET_H
#define WIDGET_H
#include
namespace Ui
{
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
int num;
Ui::Widget *ui;
QTimer *timer;//如果是黄线标明,那么表示没有包含头文件
signals:
void mysignal(int);
void showall();
private slots:
void on_showprocess_clicked();
void on_hidebutton_2_clicked();
void myslot(int);
void hideother();
};
#endif// WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "QtDebug"
#include
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);/* connect(ui->showbutton,SIGNAL(clicked()),ui->label,SLOT(show()));
connect(ui->hidebutton,SIGNAL(clicked()),ui->label,SLOT(hide()));
connect(ui->quitebutton,SIGNAL(clicked()),this,SLOT(close()));
connect(ui->horizontalSlider,SIGNAL(valueChanged(int))
,ui->progressBar,SLOT(setValue(int)));*/
connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),
this,SLOT(myslot(int)));//myslot
connect(this,SIGNAL(mysignal(int)),
this,SLOT(close()));//mysignal,
ui->lcdNumber->display(num);connect(ui->hidebutton_2,SIGNAL(clicked()),this,SLOT(hideother()));
connect(ui->showprocess,SIGNAL(clicked()),this,SLOT(show()));
connect(ui->showbutton,SIGNAL(clicked()),this,SIGNAL(showall()));//在UI下实现的这个信号的使用
num = 0;
timer = new QTimer(this);//括号中标明,父窗口是谁
connect(timer,SIGNAL(timeout()),this,SLOT(hideother()));
timer->start(1000);
}
Widget::~Widget()
{
delete ui;
}
void Widget::myslot(int a)
{
ui->label->setText(QString::number(a));
if (100==a)
{
emit mysignal(100);//亮点:满足条件发出一个自定义的信号
qDebug()<
}
}
void Widget::hideother()
{
num++;
ui->label->setText(QString::number(num));
ui->progressBar->hide();
ui->horizontalSlider->hide();
qDebug()<
if( 10==num )
{
timer->stop();
}
}
void Widget::on_hidebutton_2_clicked()
{
qDebug()<
}
void Widget::on_showprocess_clicked()
{
ui->progressBar->show();
ui->horizontalSlider->show();
}
/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created: Tue Nov 23 11:23:35 2010
** by: Qt User Interface Compiler version 4.7.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QGridLayout *gridLayout;
QLabel *label;
QPushButton *showbutton;
QPushButton *hidebutton;
QPushButton *hidebutton_2;
QPushButton *showprocess;
QPushButton *quitebutton;
QSlider *horizontalSlider;
QProgressBar *progressBar;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(400, 300);
gridLayout = new QGridLayout(Widget);
gridLayout->setSpacing(6);
gridLayout->setContentsMargins(11, 11, 11, 11);
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
QFont font;
font.setFamily(QString::fromUtf8("\346\226\260\345\256\213\344\275\223"));
font.setPointSize(22);
label->setFont(font);
gridLayout->addWidget(label, 0, 0, 1, 2);
showbutton = new QPushButton(Widget);
showbutton->setObjectName(QString::fromUtf8("showbutton"));
gridLayout->addWidget(showbutton, 1, 0, 1, 1);
hidebutton = new QPushButton(Widget);
hidebutton->setObjectName(QString::fromUtf8("hidebutton"));
gridLayout->addWidget(hidebutton, 1, 1, 1, 1);
hidebutton_2 = new QPushButton(Widget);
hidebutton_2->setObjectName(QString::fromUtf8("hidebutton_2"));
gridLayout->addWidget(hidebutton_2, 2, 0, 1, 1);
showprocess = new QPushButton(Widget);
showprocess->setObjectName(QString::fromUtf8("showprocess"));
gridLayout->addWidget(showprocess, 2, 1, 1, 1);
quitebutton = new QPushButton(Widget);
quitebutton->setObjectName(QString::fromUtf8("quitebutton"));
gridLayout->addWidget(quitebutton, 3, 0, 1, 2);
horizontalSlider = new QSlider(Widget);
horizontalSlider->setObjectName(QString::fromUtf8("horizontalSlider"));
horizontalSlider->setMaximum(100);
horizontalSlider->setPageStep(0);
horizontalSlider->setOrientation(Qt::Horizontal);
gridLayout->addWidget(horizontalSlider, 4, 0, 1, 2);
progressBar = new QProgressBar(Widget);
progressBar->setObjectName(QString::fromUtf8("progressBar"));
progressBar->setValue(0);
gridLayout->addWidget(progressBar, 5, 0, 1, 2);
retranslateUi(Widget);
QObject::connect(showbutton, SIGNAL(clicked()), label, SLOT(show()));
QObject::connect(hidebutton, SIGNAL(clicked()), label, SLOT(hide()));
QObject::connect(quitebutton, SIGNAL(clicked()), Widget, SLOT(close()));
QObject::connect(horizontalSlider, SIGNAL(valueChanged(int)), progressBar, SLOT(setValue(int)));
QObject::connect(showbutton, SIGNAL(clicked()), horizontalSlider, SLOT(show()));
QObject::connect(showbutton, SIGNAL(clicked()), progressBar, SLOT(show()));
QObject::connect(hidebutton, SIGNAL(clicked()), horizontalSlider, SLOT(hide()));
QObject::connect(hidebutton, SIGNAL(clicked()), progressBar, SLOT(hide()));
QMetaObject::connectSlotsByName(Widget);
}// setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("Widget", " signal and slot", 0, QApplication::UnicodeUTF8));
showbutton->setText(QApplication::translate("Widget", "\346\230\276\347\244\272", 0, QApplication::UnicodeUTF8));
hidebutton->setText(QApplication::translate("Widget", "\351\232\220\350\227\217", 0, QApplication::UnicodeUTF8));
hidebutton_2->setText(QApplication::translate("Widget", "\351\232\220\350\227\217\350\277\233\345\272\246", 0, QApplication::UnicodeUTF8));
showprocess->setText(QApplication::translate("Widget", "\346\230\276\347\244\272\350\277\233\345\272\246", 0, QApplication::UnicodeUTF8));
quitebutton->setText(QApplication::translate("Widget", "\345\205\263\351\227\255", 0, QApplication::UnicodeUTF8));
}// retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
}// namespace Ui
QT_END_NAMESPACE
#endif// UI_WIDGET_H
没有显示出main,main同上面的函数