Qt的初探与认识——Qt概述

Qt概述


概念解析

  • 1.信号与槽机制用于完成界面操作的响应,是任意两个Qt对象之间的通信机制;
  • 2.信号会在某个特定的情况被触发;
  • 3.槽等同接受信号的函数;
  • 4.当一个类被继承时,该类的信号与槽也同时被继承;
  • 5.信号与槽的连接方式

    • 5.1 一个信号与另一个信号相连

      • conent(object1,SIGNAL(signal1),object2,SIGNAL(signal1));
    • 5.2 同一个信号可以与多个槽连接

      • conent(object1,SIGNAL(signal2),object2,SIGNAL(slot2));
      • conent(object1,SIGNAL(signal2),object2,SIGNAL(slot1));
    • 5.3 同一个槽可以响应多个信号

      • conent(object1,SIGNAL(signal2),object2,SIGNAL(slot2));
      • conent(object3,SIGNAL(signal2),object2,SIGNAL(slot2));
    • 5.4 最常使用的连接方式

      • conent(object3,SIGNAL(signal2),object2,SLOT(slot2));
    • 其中signal为对象Object1的信号,slot为对象Object2的槽;
  • 6.信号与槽机制的优点

    • 6.1 类型安全
    • 6.2 松散耦合
  • 7.信号与槽机制的效率
  • 8.Qt5元对象系统
  • 9.布局管理器
    QGridLayout *mainLayout=new QGridLayout(this);//布局管理器,将所有控件的位置固定(创建一个网络布局器对象mainLayout)
    mainLayout->addWidget(label1,0,0);
    mainLayout->addWidget(lineEdit,0,1);
    mainLayout->addWidget(label2,1,0);
    mainLayout->addWidget(button,1,1);
    setLayout(mainLayout);

演示代码

1.dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QGridLayout>
#include<QDialog>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    QLabel *label1,*label2;
    QLineEdit *lineEdit;
    QPushButton *button;
private slots:
    void showArea();
};

#endif // DIALOG_H

2.dialog.cpp

#include "dialog.h"
const static double PI=3.1415926;
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    label1=new QLabel(this);
    label1->setText(tr("请输入圆的半径:"));
    lineEdit=new QLineEdit(this);
    label2=new QLabel(this);
    button=new QPushButton(this);
    button->setText(tr("显示对应圆的面积"));
    QGridLayout *mainLayout=new QGridLayout(this);//布局管理器,将所有控件的位置固定
    mainLayout->addWidget(label1,0,0);
    mainLayout->addWidget(lineEdit,0,1);
    mainLayout->addWidget(label2,1,0);
    mainLayout->addWidget(button,1,1);
    QObject::connect(lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea()));//信号与槽机制连接
    //QObject::connect(button,SIGNAL(clicked()),this,SLOT(showArea()));
}

Dialog::~Dialog()
{

}

void Dialog::showArea(){
    bool ok;
    QString tmpStr;
    QString valueStr=lineEdit->text();
    double valueDouble=valueStr.toDouble(&ok);
    double area=valueDouble*valueDouble*PI;
    label2->setText(tmpStr.setNum(area));
}

3.main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

最终结果:

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值