QT信号与槽实例
2017-05-09 14:31
211 查看
/* widget.h*/ #ifndef WIDGET_H #define WIDGET_H #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QtGui/QWidget> #include <QGridLayout> #include <QString> #include<QMessageBox> class Widget : public QWidget { Q_OBJECT //告诉QT 当前类中有自定义的槽函数 public: Widget(QWidget *parent = 0); ~Widget(); private: QLabel *label1, *label2; QLineEdit *lineEdit; QPushButton *button; signals: void ss(QString); //自定义信号 private slots: void showArea(); //自定义的槽函数声明 void showBox(QString); //自定义的槽函数声明 }; #endif // WIDGET_H /* widget.cpp*/ #include "widget.h" const static double PI = 3.1416; Widget::Widget(QWidget *parent) : QWidget(parent) { label1 = new QLabel(this); label1->setText(QString::fromLocal8Bit("请输入圆的半径:")); lineEdit = new QLineEdit(this); label2 = new QLabel(this); button = new QPushButton(this); button->setText(QString::fromLocal8Bit("显示对应圆的面积")); 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); //connect(button,SIGNAL(clicked()),this,SLOT(close())); //连接信号与槽函数 connect(button,SIGNAL(clicked()),this,SLOT(showArea())); //连接信号与自定义槽函数 connect (lineEdit,SIGNAL(textChanged(QString)),this,SLOT(showArea())); //连接信号与自定义槽函数 connect(this,SIGNAL(ss(QString)),this,SLOT(showBox(QString))); //连接自定义信号与自定义槽函数 } Widget::~Widget() { } void Widget::showArea() //自定义的槽函数定义 { bool ok; QString tempStr; QString valueStr = lineEdit->text(); int valueInt=valueStr.toInt(&ok); double area=valueInt*valueInt*PI; label2->setText(tempStr.setNum(area)); emit ss(QString::fromLocal8Bit("ShowArea执行了")); //发射信号 } void Widget::showBox(QString a) //自定义的槽函数定义 { QMessageBox msgBox; msgBox.setText(a); msgBox.exec(); }
相关文章推荐
- Qt 中多线程信号传递 实例
- Qt 中多线程信号传递 实例
- 用python3+PyQt5改写Python Qt GUI快速编程的第6章实例主窗口 信号及槽如何使用
- QT 信号槽用法 实例
- Linux下QT图形界面开发在终端窗口中Qt编程信号的应用实例(4)
- Qt基础 06_信号和槽(二)自定义信号和槽的实例
- QT程序实例----信号与插槽(signal and slot)
- QT信号与槽的简单实例
- Windows下qt的环境配置及运行一个c++"hello world"实例详细步骤
- QT的信号与槽机制介绍
- Qt信号与槽之connectSlotsByName函数
- QT +VS2010安装及配置运行实例(一)
- Qt捕捉窗口关闭事件与信号的捕捉
- Qt中UDP协议的使用实例
- 零基础学Qt4编程之Qt核心机制与原理之信号与槽
- Qt中connect函数(信号与槽)初识
- Qt5 信号槽新写法
- 【QT】信号槽传递自定义数据结构
- QT中 QComboBox 类中常见函数与信号的使用
- Qt实例分析——QML创建新的Qt Quick类型