搜索
Table_bottom

标签云
Table_bottom

分类
Table_bottom

声明
文章若未特別註明,皆採用 知识共享许可协议 請自覺遵守
Table_bottom

鏈。。。
Table_bottom

存档
Table_bottom

匆匆过客
82355
Table_bottom

功能
Table_bottom

Qt中鏈接信號和槽的方法

人云E云 posted @ 2015年7月03日 16:07 in 信息技術 with tags Coding Qt , 1180 阅读

最近又在用Qt寫東西,順便翻了翻Qt的文檔,發現鏈接信號和槽的方法有不少,Qt5和C++11的組合也帶來了一些不小變化。

所以有此一篇文章,對其稍加總結。

文中約定

  1. 使用“鏈接”這一術語而非“連接”
  2. 默認所有對象都(直接或間接)繼承了QObject

Qt對信號和槽的鏈接有如下規則:

  1. 使用信號和槽機制需要在類聲明中加入Q_OBJECT宏
  2. 可以鏈接信號和槽
  3. 可以鏈接信號和信號
  4. 同一個信號或槽可鏈接或被鏈接多次
  5. 信號比槽中多出的參數會被忽略
  6. 鏈接可以通過disconnect斷開

其鏈接方法如下:

  1.  connect( 信號源, SIGNAL( 信號( 參數表 ) ), 接收者, SLOT( 槽( 參數表 ) ) )
    • 目前最爲常用的一個方法,是static函數
    • 在任何繼承了QObject且寫有Q_OBJECT宏的類中使用
    • 其中SIGNAL()和SLOT()均爲宏
    • 參數表中只需()按順序寫出參數類型
    • 其原型爲:
      • bool QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection)
      • QMetaObject::Connection QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection)
    • 其還有幾個重載,常用的爲:
      • 接收者爲當前對象時可不寫接收者
  2. connect( 信號源, &信號源類::信號, 接收者, &接收者類::信號 )
    • 需要Qt5
    • 編譯器會檢查信號和槽的參數類型是否匹配
    • 不支持槽使用默認參數來填補信號中不足的參數
    • 其原型爲:
      QMetaObject::Connection QObject::connect(const QObject * sender, PointerToMemberFunction signal, const QObject * receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
  3. connect( 信號源, &信號源類::信號, lambda表達式 )
    • 需要編譯器支持C++11且需要Qt5
    • 若編譯器不支持可變參數模板,則最多允許信號攜帶6個參數
    • 其原型爲:
      QMetaObject::Connection QObject::connect(const QObject * sender, PointerToMemberFunction signal, Functor functor)

Qt5和Q4中,connect函數幾個主要不同點在於:

  1. Qt5中返回值類型爲QMetaObject::Connection,而Qt4中爲bool
  2. Qt5中支持槽爲lambda表達式,而Qt4中不支持
SBI HRMS 说:
2022年8月04日 05:45

SBI HRMS is a self service ESS portal for all the employees of State Bank of India all across the country, and this portal serves as a Human Resource Management System for present working and retired employees of SBI in order to exercise different information and benefits through ‘Multi-factor Authorization’. SBI HRMS In simple terms, the SBI HRMS stands for State Bank of Indian Human Resource Management System which is an all in one platform to get different employee services right away such as details of the salary, Profile Information, Vacation, Career History, Apply Leave, Download Salary slip, View Pay or Pension details, Check Holidays, Apply Tour and other financial beneficial information of SBI working employee and pensioner.the SBI HRMS stands for State Bank of Indian Human Resource Management System which is an all in one platform to get different employee services right away such as details of the salary, Profile Information, Vacation, Career History.

NCERT Sanskrit Quest 说:
2022年9月23日 10:33

Sanskrit is one of the ancient languages of the Country. This is introduced in primary education to get an alert on other languages also. Sanskrit is somewhat related to the another languages such as English, NCERT Sanskrit Question Paper Class 3 Spanish and etc… Learning Sanskrit is not difficult to learn and may helps the scholars in their future. Department of Education has introduced the Sanskrit Language from the elementary level at all Central Board Schools like CBSE, KVS, JNV and etc. Most of the students have chosen Sanskrit as their Second or Third language, each education board have conducted the Term 1 & Term 2 exams as per the new revised syllabus and curriculum of the course.

12thmodelpaper.in 说:
2023年5月03日 17:32

Board 12th Class Model Paper 2023 Aspirants who had Registered for the Maha Board 12th Class Exam can Download the Previous Paper When Maha Board 12th Announces the Dates to Download the Question Paper. 12thmodelpaper.in Board 12th Question Papers will be Provided Very Soon Through Online Mode and all the Applicants Should Visit the Official Page Regularly for More Information Regarding Previous Paper, Examination Date. Check the Below Provided Details for More Information About the Board 12th Model Paper.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter