1. 程式人生 > >Qt一步一步實現外掛通訊(附原始碼)

Qt一步一步實現外掛通訊(附原始碼)

前一章已經實現了主程式呼叫載入外掛功能,這一章描述主程式和外掛間通訊功能

說道Qt的通訊必須要了解訊號和槽的機制原理,這裡不做論述,不清楚的同學去看看訊號和槽機制

不廢話直接上步驟,在上一章的基礎下進行修改

第一步:在外掛中定義一個接收槽

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 28 29 30 31 32 33 34 35 36 37 38 #include "mainwidget.h" #include "ui_mainwidget.h"
#include "qtimer.h" #include "qdatetime.h" mainwidget::mainwidget(QWidget *parent) : QWidget(parent), ui(new Ui::mainwidget) { ui->setupUi(this); timer = new QTimer(this); //新建定時器 connect(timer,SIGNAL(timeout()),this,SLOT(timerUpDate())); //關聯定時器計滿訊號和相應的槽函式 timer->start(1000); } mainwidget::~mainwidget()
{ delete ui; } void mainwidget::timerUpDate() { QDateTime time = QDateTime::currentDateTime();//獲取系統現在的時間 QString str = time.toString("yyyy-MM-dd hh:mm:ss ddd"); //設定顯示格式 QString strr= strr.fromLocal8Bit("當前時間:")+str;//呼叫中文顯示 ui->label->setText(strr); } void mainwidget::time_start()
{ timer->start(); ui->label->setText("start"); } void mainwidget::time_stop() { timer->stop(); ui->label->setText("stop"); }

第二步 生成外掛

這一步和上一章內容不變,替換要實現的外掛的內容即可

點選執行生成外掛,找到外掛testpluginplugin.dll

複製貼上到主程式的程式執行目錄下

第三步 主程式載入外掛並設定訊號槽

1 2 connect(ui->action,SIGNAL(triggered()), testwidget,SLOT(time_start())); connect(ui->action_2,SIGNAL(triggered()),testwidget,SLOT(time_stop()));

效果圖:

1)載入外掛成功後預設時間是不斷重新整理

2)點選停止後時間顯示stop

3)點選開始,時間繼續重新整理

這裡就完成了主介面操作外掛的功能


from:http://www.cnblogs.com/newstart/p/3457072.html