QT之QSignalMapper(可以理解為轉寄站,多個按鈕綁定到一個Edit上,且能分辨。每個單獨串連的話,反而麻煩)

來源:互聯網
上載者:User

標籤:是你   處理   lin   pre   img   區分   轉寄   sub   title   

QT之QSignalMapper

 

  • QT之QSignalMapper
    • 簡述
    • 上代碼
    • 相關知識點文章
    • 結尾

 

簡述

QSignalMapper我們可以理解為轉寄站,此話怎講呢?比如,按鈕點擊的響應槽,綁定到QSignalMapper上,QSignalMapper收到按鈕的點擊後,又通知到另外的控制項上做處理。有的朋友會問了,為什麼要這麼麻煩,需要轉一手,不能去掉中間的QSignalMapper,而直接調用嗎。答案當然是可以的,為什麼需要QSignalMapper這個轉寄站呢,我們先來看下效果,然後再看看代碼,相信大家會喜歡上QSignalMapper這個轉寄站的。

上代碼
void QSignalMapper_Demo::initControl(){    QVBoxLayout *pLayout = new QVBoxLayout(this);    QString str = "Button1 Button2 Button3 Button4 Button5";    QStringList strList = str.split(" ");    QSignalMapper *pMapper = new QSignalMapper(this);    int nRowCnt = 0;    foreach(QString itor, strList)    {        QPushButton *pBtn = new QPushButton(this);        pBtn->setFixedHeight(CON_HEGIHT);        pBtn->setText(itor);        connect(pBtn, SIGNAL(clicked()), pMapper, SLOT(map()));        pMapper->setMapping(pBtn, pBtn->text());        pLayout->addWidget(pBtn, nRowCnt++, 0);    }    QLineEdit *pEdit = new QLineEdit(this);    pEdit->setFixedHeight(CON_HEGIHT);    connect(pMapper, SIGNAL(mapped(QString)), pEdit, SLOT(setText(QString)));    pLayout->addWidget(pEdit, nRowCnt, 0);    pLayout->addStretch();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

眼光利索的朋友看到了我代碼中,代碼不規範,說你代碼怎麼就new,怎麼沒deltete,你這樣不會記憶體流失嗎,這裡告訴大家是不會的哦,相關知識可以看我之前寫的QT之記憶體流失管理。

回到本節,看完QSignalMapper的用法,我們回過頭再看看,不用QSignalMapper,又該如何?上面的效果呢,編輯框中我們可以看到,是哪個按鈕被按下了。

//去掉QSignalMappervoid QSignalMapper_Demo::initControl(){    QVBoxLayout *pLayout = new QVBoxLayout(this);    QString str = "Button1 Button2 Button3 Button4 Button5";    QStringList strList = str.split(" ");    int nRowCnt = 0;    foreach(QString itor, strList)    {        QPushButton *pBtn = new QPushButton(this);        pBtn->setFixedHeight(CON_HEGIHT);        pBtn->setText(itor);        //這裡,響應的是同一個槽,按鈕被點擊後,我們卻不能知道是哪個按鈕被按下。        connect(pBtn, SIGNAL(clicked()), this, SLOT(onclicked()()));        //有的朋友說,可以每個按鈕綁定不同的槽做區分呀,這樣做可以是可以,但是你覺得使用QSignalMapper是不是要更簡潔些呢?        pLayout->addWidget(pBtn, nRowCnt++, 0);    }    QLineEdit *pEdit = new QLineEdit(this);    pEdit->setFixedHeight(CON_HEGIHT);    pLayout->addWidget(pEdit, nRowCnt, 0);    pLayout->addStretch();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

看完代碼,相信大家都知道了QSignalMapper的美!有的朋友又問了,我什麼時候該用QSignalMapper呢?通過上面的例子很簡單呀,我們需要處理同一個事情時,而簡化多個不同的槽卻做著相同的事,我們的QSignalMapper就派上用場啦!

相關知識點文章

QT之記憶體流失管理。

結尾

http://blog.csdn.net/ly305750665/article/details/53790209

QT之QSignalMapper(可以理解為轉寄站,多個按鈕綁定到一個Edit上,且能分辨。每個單獨串連的話,反而麻煩)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.