Qt 5.4.1学习(1)
来源:互联网 发布:centos ntfs 编辑:程序博客网 时间:2024/06/01 20:57
工程名称:TestQt1
最终实现了使用代码动态注册按钮事件的功能,并且可以支持在按钮当中获取sender
main.cpp
#include "windowmain.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); WindowMain w; w.show(); return a.exec();}
windowmain.h
#ifndef WINDOWMAIN_H#define WINDOWMAIN_H#include <QMainWindow>#include <QPushButton>namespace Ui {class WindowMain;}class WindowMain : public QMainWindow{ Q_OBJECTpublic: explicit WindowMain(QWidget *parent = 0); ~WindowMain(); bool init();private slots: void on_pushButton_clicked(); void onClickUi();private: Ui::WindowMain *ui; QPushButton* _btn2;};#endif // WINDOWMAIN_H
windowmain.cpp
#include "windowmain.h"#include "ui_windowmain.h"WindowMain::WindowMain(QWidget *parent) :QMainWindow(parent), ui(new Ui::WindowMain){ ui->setupUi(this); init();}WindowMain::~WindowMain(){ delete ui;A}bool WindowMain::init(){ _btn2 = ui->btn2; // 关闭按钮的连发 //_btn2->setAutoRepeat(false); QObject::connect(_btn2, SIGNAL(clicked()), this, SLOT(onClickUi()) ); return true;}void WindowMain::on_pushButton_clicked(){ qDebug("onClicked test1 !");}void WindowMain::onClickUi(){ QPushButton* btn= qobject_cast<QPushButton*>(sender()); if( btn->objectName() == "btn2") { qDebug("onClicked btn2 !"); }}
windowmain.ui
<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>WindowMain</class> <widget class="QMainWindow" name="WindowMain"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>806</width> <height>556</height> </rect> </property> <property name="windowTitle"> <string>WindowMain</string> </property> <widget class="QWidget" name="centralWidget"> <widget class="QPushButton" name="pushButton"> <property name="geometry"> <rect> <x>100</x> <y>280</y> <width>91</width> <height>41</height> </rect> </property> <property name="text"> <string>test1</string> </property> </widget> <widget class="QPushButton" name="btn2"> <property name="geometry"> <rect> <x>220</x> <y>280</y> <width>91</width> <height>41</height> </rect> </property> <property name="text"> <string>test2</string> </property> </widget> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>806</width> <height>23</height> </rect> </property> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>TopToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/></ui>
0 0
- Qt 5.4.1学习(1)
- QT 学习记录1
- QT学习笔记1
- Qt学习旅程(1)
- Qt学习笔记【1】
- QT学习笔记-1
- Qt学习(1)
- Qt学习(1)
- Qt学习笔记(1)
- Qt学习1
- Qt学习笔记1
- qt学习笔记1
- Qt状态机学习1
- Qt学习笔记1
- qt学习1
- Qt学习手记 - 1
- Qt学习—1
- Qt学习笔记1
- 排序算法—快速排序
- java 页面提交到后台是乱码问题
- 使用TortoiseGit在github上创建开源工程
- 第9周项目3-分数类中的运算符重载(续)
- matlab 直接修改fig图数据显示
- Qt 5.4.1学习(1)
- <div>中插入文本部分显示超出部分用省略号显示
- JDBC----数据库连接池(connection pool)
- 怀念这一路走来。
- <jsp:include flush="true" />
- 怎样从MATLAB图形文件fig中提取数据
- 框架技术
- Windows下启动/停止django自带的Web服务器
- 浩易南:10个年赚10万的项目(上)