问题函数关于SDK回调Qt函数的问题

本文是一篇关于问题函数的子帖

    1、问题:

        近来碰到纯C、c++回调Qt的函数问题,并且在Qt的程线里注册,第一次见看这样用,貌似问题不大,但是也有值得疑怀和不稳定的地方。但是临时还不能言断种这使用方法有问题,写出来作为经验总结,也供大家互相学习和探讨。

    

        值得注意的是,在程线注册回调函数的时候传的参数是函数体。

    

    2、相干子帖和博文:

    

如何让外部纯C函数与Qt通信

    http://bbs.csdn.net/topics/390278368?page=1

    

    我在现的方法是供提一个callback函数和一个文件域的void* 针指
callback()
{
    b::up(void* pt)// static;
}
在 b::static void up(void* pt)
{
     b * pt_ = (b *) pt;
     emit pt_->signal();
}

    ---------------------------------------------------------------------------------------

    widget.h

    

C/C++ code ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef WIDGET_H
#define WIDGET_H
 
#include    <QtGui>
#include    <QtCore>
 
class  Widget :  public  QWidget
{
     Q_OBJECT
public :
     Widget();
     static  Widget *Instance();
     static  Widget *widget;
     void  emit_signal();
 
public  slots:
     void  create();
     void  use_fun();
 
signals:
     void  create_label();
 
};
#endif

    widget.cpp

    

C/C++ code ?
    每日一道理
书籍好比一架梯子,它能引领人们登上文化的殿堂;书籍如同一把钥匙,它将帮助我们开启心灵的智慧之窗;书籍犹如一条小船,它会载着我们驶向知识的海洋。

    

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
39
40
41
42
43
44
45
46
47
48
#include    "widget.h"
 
static  void  show_new_label()
{
     qDebug() <<  "in static fun\n" ;
     Widget *w = Widget::Instance();
     w->emit_signal();
     //return;
}
 
Widget::Widget()
{
     QPushButton *button =  new  QPushButton( "button" , this );
     connect(button, SIGNAL(clicked()),  this , SLOT(use_fun()));
 
     connect( this , SIGNAL(create_label()),  this , SLOT(create()));
     resize(400,400);
}
 
Widget *Widget::widget = NULL;
Widget *Widget::Instance()
{
     if (widget == NULL)
     {
         widget =  new  Widget;
     }
     return  widget;
}
 
void  Widget::emit_signal()
{
     qDebug() <<  "emit signal" ;
     emit create_label();
}
 
void  Widget::use_fun()
{
     show_new_label();
}
 
void  Widget::create()
{
     QLabel *label =  new  QLabel( this );
     label->setText( "this is a new label" );
     label->resize(100,100);
     label->move(100,100);
     label->show();
}     

文章结束给大家分享下程序员的一些笑话语录: 乔布斯:怎么样还是咱安全吧!黑客:你的浏览器支持国内网银吗?苹果可以玩国内的网游吗乔布斯:......不可以黑客:那我研究你的漏洞干嘛,我也需要买奶粉!

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3069819.html