一.Qt中的信號(hào)處理
Qt消息模型:
- Qt封裝了具體操作系統(tǒng)的消息機(jī)制
- Qt遵循經(jīng)典的GUI消息驅(qū)動(dòng)事件模型
Qt中定義了與系統(tǒng)消息相關(guān)的概念;
?
Qt中的消息處理機(jī)制:
Qt的核心 QObject::cinnect函數(shù):
Qt中的“新”關(guān)鍵字:
實(shí)驗(yàn)1 初探信號(hào)與槽?
#include <QtGui/QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);//應(yīng)用程序?qū)ο? QPushButton b;//按鈕對(duì)象
b.setText("Click me to quit!");
b.show();
//通過connect函數(shù) 將按鈕對(duì)象的點(diǎn)擊事件信號(hào) 映射到 a對(duì)象的quit()成員函數(shù)
QObject::connect(&b, SIGNAL(clicked()), &a, SLOT(quit()));
return a.exec();
}
?運(yùn)行結(jié)果:
二.自定義槽
- 只有QObject的子類才能自定義槽
- 定義槽的類必須在聲明的最開始處使用Q_OBJECT
- 類中的聲明槽時(shí)需要使用slots關(guān)鍵字
- 槽與所處理的信號(hào)在函數(shù)簽名上必須一致
- SIGNAL和SLOT所指定的名稱中:可以包含參數(shù)類型,不能包含具體的參數(shù)名
實(shí)驗(yàn)2 為計(jì)算器界面添加消息處理函數(shù),所有按鈕對(duì)應(yīng)一個(gè)槽,打印同樣log
目錄:
QCalculatorUI.h:
#ifndef _QCALCULATORUI_H_
#define _QCALCULATORUI_H_
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
class QCalculatorUI : public QWidget
{
Q_OBJECT
private:
QLineEdit* m_edit;
QPushButton* m_buttons[20];
QCalculatorUI();
bool construct();
private slots:
void onButtonClicked();
public:
static QCalculatorUI* NewInstance();
void show();
~QCalculatorUI();
};
#endif
QCalculatorUI.cpp:
#include "QCalculatorUI.h"
#include <QDebug>
QCalculatorUI::QCalculatorUI() : QWidget(NULL, Qt::WindowCloseButtonHint)
{
}
bool QCalculatorUI::construct()
{
bool ret = true;
const char* btnText[20] =
{
"7", "8", "9", "+", "(",
"4", "5", "6", "-", ")",
"1", "2", "3", "*", "<-",
"0", ".", "=", "/", "C",
};
m_edit = new QLineEdit(this);
if( m_edit != NULL )
{
m_edit->move(10, 10);
m_edit->resize(240, 30);
m_edit->setReadOnly(true);
}
else
{
ret = false;
}
for(int i=0; (i<4) && ret; i++)
{
for(int j=0; (j<5) && ret; j++)
{
m_buttons[i*5 + j] = new QPushButton(this);
if( m_buttons[i*5 + j] != NULL )
{
m_buttons[i*5 + j]->resize(40, 40);
m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
connect(m_buttons[i*5 + j], SIGNAL(clicked()), this, SLOT(onButtonClicked()));
}
else
{
ret = false;
}
}
}
return ret;
}
QCalculatorUI* QCalculatorUI::NewInstance()
{
QCalculatorUI* ret = new QCalculatorUI();
if( (ret == NULL) || !ret->construct() )
{
delete ret;
ret = NULL;
}
return ret;
}
void QCalculatorUI::show()
{
QWidget::show();
setFixedSize(width(), height());
}
void QCalculatorUI::onButtonClicked()
{
QPushButton* btn = (QPushButton*)sender();
qDebug() << "onButtonClicked()";
qDebug() << btn->text();
}
QCalculatorUI::~QCalculatorUI()
{
}
main.cpp:
#include <QApplication>
#include "QCalculatorUI.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCalculatorUI* cal = QCalculatorUI::NewInstance();
int ret = -1;
if( cal != NULL )
{
cal->show();
ret = a.exec();
delete cal;
}
return ret;
}
運(yùn)行結(jié)果:點(diǎn)擊 ”7“? ”8“? ”9“
?文章來源:http://www.zghlxwxcb.cn/news/detail-835002.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-835002.html
到了這里,關(guān)于嵌入式Qt Qt中的信號(hào)處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!