现在的位置: 首页 > 综合 > 正文

Qt自定义信号槽

2014年03月01日 ⁄ 综合 ⁄ 共 962字 ⁄ 字号 评论关闭

//signal_slot.h

#ifndef SIGNAL_SLOT_H
#define SIGNAL_SLOT_H

#include <QtGui/QMainWindow>
#include "ui_signal_slot.h"

class signal_slot : public QMainWindow
{
	Q_OBJECT

public:
	signal_slot(QWidget *parent = 0, Qt::WFlags flags = 0);
	~signal_slot();

private:
	Ui::signal_slotClass ui;

public slots:
	void recieve(QString str);
};

class sender : public QObject{
	Q_OBJECT
signals:
	void send(QString str);
public:
	void sendMessage(QString message);
};

#endif // SIGNAL_SLOT_H

//signal_slot.cpp

#include "signal_slot.h"

signal_slot::signal_slot(QWidget *parent, Qt::WFlags flags)
	: QMainWindow(parent, flags)
{
	ui.setupUi(this);
}

signal_slot::~signal_slot()
{

}
void signal_slot::recieve(QString str){
	ui.lineEdit->setText(str);
}

void sender::sendMessage(QString message){
	emit send(message);
}

//main.cpp

#include "signal_slot.h"
#include <QtGui/QApplication>

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	signal_slot w;

	sender s;
	w.show();

	QObject::connect(&s,SIGNAL(send(QString)),&w,SLOT(recieve(QString)));

	s.sendMessage("hello");

	return a.exec();
}

 

抱歉!评论已关闭.