前一章已经实现了主程序调用加载插件功能,这一章描述主程序和插件间通信功能

说道Qt的通信必须要了解信号和槽的机制原理,这里不做论述,不清楚的同学去看看信号和槽机制

不废话直接上步骤,在上一章的基础下进行修改

第一步:在插件中定义一个接收槽

#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

复制粘贴到主程序的程序运行目录下

第三步 主程序加载插件并设置信号槽

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

效果图:

1)加载插件成功后默认时间是不断刷新

2)点击停止后时间显示stop

3)点击开始,时间继续刷新

这里就完成了主界面操作插件的功能

 源码下载http://download.csdn.net/detail/huangyuancao/6654197

 

posted on 2013-12-04 10:29  夜&枫  阅读(5154)  评论(0编辑  收藏  举报