最近看了这本和QT相关的书,下面是第五章的习题 及我的理解
题目1:Qt的信号和槽函数分别指什么?
当信号发出的时候,信号绑定的槽函数会自动回调.
信号的本质应该是事件,然后槽函数的本质应该是成员函数,分布是Signal和SLOT
然后它有三种写法:
qt5.0之前
// 发送者 信号 接收者 槽函数
connect(sender,SIGNAL(SIGNAL()),receiver,SLOT(handle()));
qt5.0之后
connect(sender,&sender::signal,receiver,&receiver::handle);//最后还有个type,一般默认
使用lambda表达式,c++11出来的新特性
connect(sender,&sender::signal::signal,[=](){
信号触发之后的一些操作
});
题目2 输入十进制 文本框中显示二进制,八进制,十六进制
思路:要将十进制变成其他进制并且显示到文本框中,考虑使用QString::number(int,几进制) 页面布局主要是定义一个输入框,三个输出框,一个确定按钮,一个清楚按钮,然后使用QLayout进行布局,使用它的addwidget函数放置上面的组件,最后setlayout.
源代码如下:
h文件
#ifndef TEST2_H
#define TEST2_H
#include <QObject>
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
class test2 : public QWidget //继承否则报错
{
Q_OBJECT
public:
test2();
public:
//定义变量
QLabel * title; //标题
QLabel * l_input;
QLabel * l_2;
QLabel * l_8;
QLabel * l_16;
QLineEdit * m_Input; //输入框
QLineEdit * m_2; //2进制
QLineEdit * m_8; //8进制
QLineEdit * m_16; //16进制
QPushButton * btn_sure; //确定按钮
QPushButton * btn_clear; //清除
private slots:
void on_btn_sure_clicked();
void on_btn_clear_clicked();
;
};
#endif // TEST2_H
c文件
#include "test2.h"
#include <QGridLayout>
#include <QString>
#include <QString>
#include <QDebug>
test2::test2()
{
this->setFixedSize(300,180); //变量初始化
title = new QLabel("5单元习题2", this);
l_input=new QLabel("输入框",this);
l_2 = new QLabel("2进制",this);
l_8 = new QLabel("8进制",this);
l_16 = new QLabel("16进制",this);
m_Input = new QLineEdit(this);
m_2 = new QLineEdit(this);
m_8 = new QLineEdit(this);
m_16 = new QLineEdit(this);
btn_sure = new QPushButton("确定", this);
btn_clear = new QPushButton("清除", this);
QGridLayout * Grid = new QGridLayout;
Grid->addWidget(title,1,1,1,4,Qt::AlignHCenter); //标题
Grid->addWidget(l_input,2,1,1,4,Qt::AlignLeft);
Grid->addWidget(m_Input,2,2,1,4,Qt::Alignment());
Grid->addWidget(l_2,3,1,1,4,Qt::AlignLeft); //标题
Grid->addWidget(m_2,3,2,1,4,Qt::Alignment()); //2进制
//m_2->setReadOnly(true);
Grid->addWidget(l_8,4,1,1,4,Qt::AlignLeft); //标题
Grid->addWidget(m_8,4,2,1,4,Qt::Alignment()); //8进制
//m_8->setReadOnly(true);
Grid->addWidget(l_16,5,1,1,4,Qt::AlignLeft); //标题
Grid->addWidget(m_16,5,2,1,4,Qt::Alignment()); //16进制
//m_16->setReadOnly(true);
Grid->addWidget(btn_sure, 6, 1, 1, 2); // 确定按钮
Grid->addWidget(btn_clear, 6, 3, 1, 2); // 清除按钮
this->setLayout(Grid);
// 连接信号与槽
connect(btn_sure, SIGNAL(clicked(bool)), this, SLOT(on_btn_sure_clicked()));
connect(btn_clear, SIGNAL(clicked(bool)), this, SLOT(on_btn_clear_clicked()));
}
void test2::on_btn_sure_clicked()
{
qDebug()<<"xxx";
//将输入的转换为int
int input_content = m_Input->text().toInt();
qDebug()<<input_content;
QString n_2 = QString::number(input_content,2);//2进制
QString n_8 = QString::number(input_content,8);//8进制
QString n_16 = QString::number(input_content,16);//16进制
m_2->setText(n_2);
m_8->setText(n_8);
m_16->setText(n_16);
}
void test2::on_btn_clear_clicked()
{
m_2->clear();
m_8->clear();
m_16->clear();
}
最后在main函数里面调用,show出来就行了.
题目三 通过实现两个对话框互相发送消息的功能来熟悉Qt的信号和槽.具体功能:程序为单对话框程序,单击打开按钮可以再次打开一个对话框.两个对话框都有发送消息按钮,输入消息文本框以及编辑消息文本框,并可实现互发消息的功能.
实现思路:首先是定义两个类,使用声明那些变量,然后使用layout完成界面布局,两边定义signal和slot,绑定按钮触发signal,使用emit关键字将信息传递出去
源码如下:
diag1:
#ifndef DIALOGCONNECT_H
#define DIALOGCONNECT_H
#include "dialog2.h"
#include <QDialog>
#include <QObject>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QTextBrowser>
class DialogConnect : public QDialog
{
Q_OBJECT
signals:
void sendMessage(QString &Message);
public:
DialogConnect();
QPushButton * btn_Open;
QPushButton * btn_Send;
QLineEdit * line;
QTextBrowser * receive;
//dialog2 * x = nullptr;
public slots:
void receiveMessage(QString &Message);
};
#endif // DIALOGCONNECT_H
#include "dialogconnect.h"
#include <QGridLayout>
#include <QPushButton>
#include <QDebug>
#include"dialog2.h"
DialogConnect::DialogConnect()
{
btn_Open = new QPushButton("打开",this);
btn_Send = new QPushButton("发送",this);
line = new QLineEdit("",this);
receive = new QTextBrowser();
QGridLayout * grid = new QGridLayout;
grid->addWidget(btn_Open,1,1,1,2,Qt::Alignment());
grid->addWidget(receive,2,1,2,2,Qt::Alignment());
grid->addWidget(btn_Send,5,1,1,2,Qt::Alignment());
grid->addWidget(line,4,1,1,4,Qt::Alignment());
this->setLayout(grid);
// connect(DialogConnect::btn_Open,&QPushButton::clicked,this,[=]{
// if(x == nullptr){
// x = new dialog2;
// qDebug()<<"已经打开";
// x->show();
// }
// });
connect(btn_Send,&QPushButton::clicked,[=]{
QString x = this->line->text();
emit sendMessage(x);
});
}
void DialogConnect::receiveMessage(QString &Message)
{
this->receive->setPlainText(Message);
}
dialog2
#ifndef DIALOG2_H
#define DIALOG2_H
#include <QDialog>
#include <QLineEdit>
#include <QObject>
#include <QTextBrowser>
#include <QWidget>
class dialog2 : public QDialog
{
Q_OBJECT
signals:
void sendMessage(QString &Message);
public:
dialog2();
QPushButton * btn_Send;
QLineEdit * line;
QTextBrowser * receive;
public slots:
void receiveMessage(QString &Message);
};
#endif // DIALOG2_H
#include "dialog2.h"
#include <QGridLayout>
#include <QPushButton>
dialog2::dialog2()
{
btn_Send = new QPushButton("发送",this);
line = new QLineEdit("",this);
receive = new QTextBrowser();
QGridLayout * grid = new QGridLayout;
grid->addWidget(receive,2,1,2,2,Qt::Alignment());
grid->addWidget(btn_Send,5,1,1,2,Qt::Alignment());
grid->addWidget(line,4,1,1,4,Qt::Alignment());
this->setLayout(grid);
connect(btn_Send,&QPushButton::clicked,[=]{
QString x = this->line->text();
emit sendMessage(x);
});
}
void dialog2::receiveMessage(QString &Message)
{
this->receive->setPlainText(Message);
}
最后在mian函数里面绑定信号槽,并且show出来
#include "dialog.h"
#include "test2.h"
#include "dialogconnect.h"
#include "dialog2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
DialogConnect * w = new DialogConnect ;
dialog2 * w1 = new dialog2;
w->show();
QObject::connect(w->btn_Open,&QPushButton::clicked,[=]{
w1->show();
});
QObject::connect(w,&DialogConnect::sendMessage,w1,&dialog2::receiveMessage);
QObject::connect(w1,&dialog2::sendMessage,w,&DialogConnect::receiveMessage);
return a.exec();
}