qt4典型的信号槽的实现
2015-07-19 15:51
477 查看
dialog.h
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private slots: //槽函数,右击发送按钮的“转到槽” void on_send_clicked(); signals: void StringChange(QString str); //信号 private: Ui::Dialog *ui; }; #endif // DIALOG_H
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: //槽函数 void showString(QString str); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
dialog.cpp
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); } Dialog::~Dialog() { delete ui; } void Dialog::on_send_clicked() { QString str = ui->StringlineEdit->text(); //保存编辑框上的文本 emit StringChange(str); //发送信号 }
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "dialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); Dialog *dlg = new Dialog(this); connect(dlg,SIGNAL(StringChange(QString)),this,SLOT(showString(QString))); //将对话框上的信号与自己的槽函数进行关联 dlg->show(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::showString(QString str) { ui->stringLabel->setText(str); }main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
相关文章推荐
- Win7下安装和使用Qt5详细图解
- qtp
- Qt之再谈阴影边框
- QTTabBar v1026
- QTP里的DOM应用
- QT 环境的搭建
- QTP中运行错误的捕捉和记录
- Qt MVC学习 模型子类化参考
- 在QT5中设置应用程序图标
- OSG嵌入Qt的第二种方式:使用QOpenGLWidget/QGLWidget
- 自学Qt_2,第一个应用程序
- qt- thread —— 关于槽函数所在线程的问题
- Qt中 抽象列表模型类 的使用
- qtcreator快捷键总结
- qt中设置菜单高度
- Qt界面编程之多窗口切换
- qtp 参数类 VBS 利用Default 关键字来 实现构造函数的功能
- VS2010配置QT5.5.0开发环境
- 利用QTP TextUtil对象获取图片验证码
- ubuntu Qt连接MySQL