天天看點

QT信号函數和槽函數的基本代碼實作

QT信号函數和槽函數的基本代碼實作

exampleA.h:

#ifndef EXAMPLEA_H      
#define EXAMPLEA_H      
#endif // EXAMPLEA_H      
#include<QCoreApplication>      
class CExampleA:public QObject      
{      
Q_OBJECT      
public:      
CExampleA(){m_Value=0;}      
void SetValue(int nNewval)      
{      
if(m_Value==nNewval)      
{      
return ;      
}      
m_Value=nNewval;      
emit stateChanged(m_Value);      
}      
signals:      
void stateChanged(int nNewVal);      
private:      
int m_Value;      
};      

exampleb.h:

#ifndef EXAMPLEB_H      
#define EXAMPLEB_H      
#endif // EXAMPLEB_H      
#include<QDebug>      
#include<QCoreApplication>      
class CExampleB:public QObject      
{      
Q_OBJECT      
public:      
CExampleB(){}      
public slots:      
void Function(int nNewValue)      
{      
qDebug()<<"new Values = "<<nNewValue;      
}      
};      

main.cpp:

#include<examplea.h>      
#include<exampleb.h>      
int main()      
{      
CExampleA a;      
CExampleB b;      
QObject::connect(&a,SIGNAL(stateChanged(int)),&b,SLOT(Function(int)));      
a.SetValue(100);      
return 100;      
}