在QT中如何实现Thread与GUI的主多线程 gui java连通

相关文章推荐:
boost thread获取当前线程
boost的官方例子,有单线程的网络框架,httpserver2是线程池的。下面参照网上某人的代码修改了一点(忘了哪位大仙的代码了)
测试工具,适用stressmark,测试效果非常好, 9000个/s
#include &stdio.h&#include &AuthenHandle.h&#include &configure.h&#ifdef WIN32 //for windows nt/2000/xp#include &winsock.h&#include &windows.h&#include &gelsserver.h&#pragma comment(lib,&Ws2_32.lib&)#else
阅读(90) 回复(0)
其他文章推荐
转载地址: http://blog.chinaunix.net/uid--id-3025534.html
如果系统定义了 _POSIX_thread_PRIORITY_SCHEDULING ,则支持设置线程的实时调度优先级。
我们可以用下面的编译指令来判断:#if defined(_POSIX_thread_PRIORITY_SCHEDULING)
调度策略 policy: 可以取三个值(SCHED_FIFO、SCHED_RR、SCHED_OTHER)。
#include &pthread.h&
int pthread_attr_setschedp...
阅读(30) 回复(0)
线程属性:
使用pthread_attr_t类型表示,我们需要对此结构体进行初始化,
初始化后使用,使用后还要进行去除初始化!
pthread_attr_init:初始化
pthread_attr_destory:去除初始化
&pthread.h&
阅读(0) 回复(0)
问题描述:界面线程MainApp为主线程,工作线程Mythread为一子线程,从工作线程向主线程传递字符串用于在主线程中显示。
Qt的信号与槽机制可以将任何继承自QObject类的对象捆绑在一起,使不同对象之间能够相互通信。
成功的实现:
工作线程:
class Mythread:public Qthread
void MsgSignal(const QString& tep);//用于向主线程传递字符串
protected:
void run();//run 中...
阅读(60) 回复(0)
线程属性:
使用pthread_attr_t类型表示,我们需要对此结构体进行初始化,
初始化后使用,使用后还要进行去除初始化!
pthread_attr_init:初始化
pthread_attr_destory:去除初始化
&pthread.h&
阅读(0) 回复(0)
Posix线程中的线程属性pthread_attr_t主要包括scope属性、detach属性、堆栈地址、堆栈大小、优先级。在pthread_create中,把第二个参数设置为NULL的话,将采用默认的属性配置。
pthread_attr_t的主要属性的意义如下:
__detachstate,表示新线程是否与进程中其他线程脱离同步, 如果设置为Pthread_CREATE_DETACHED 则新线程不能用pthread_join()来同步,且在退出时自行释放所占用的资源。缺省为Pthread_CREATE_JOINAB...
Android平台
阅读(0) 回复(0)
本文介绍的是QT中实现thread与GUI主线程通,目前只会一种,采用信号槽机制。
通常情况下,信号和槽机制可以同步操作,这就意味着在发射信号的时候,使用直接函数即可以立刻调用连接到一个信号上的多个槽。然而,当连接位于不同线程中的对象时,这一机制就会变得不同步起来,可以通过修改QObject::connect()的第5个可选参数而改变。
connect的第五个参数Qt::QueuedConnection表示槽函数由接受信号的线程所执行,如果不加表示槽函数...
阅读(0) 回复(0)
在学习多线程之前,先搞清楚一下几个概念
1,什么是进程,什么是线程,什么是同步?
每个正在系统上运行的程序都是一个进程,每个进程包含一到多个线程。进程也可能是整个程序或者是部分程序的动态执行。线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行。也可以把它理解为代码运行的上下文。所以线程基本上是轻量级的进程,它负责在单个程序里执行多任务。
相对进程而言,线程是一个更加接近于执行体的概...
阅读(0) 回复(0)
在学习多线程之前,先搞清楚一下几个概念
1,什么是进程,什么是线程,什么是同步?
每个正在系统上运行的程序都是一个进程,每个进程包含一到多个线程。进程也可能是整个程序或者是部分程序的动态执行。线程是一组指令的集合,或者是程序的特殊段,它可以在程序里独立执行。也可以把它理解为代码运行的上下文。所以线程基本上是轻量级的进程,它负责在单个程序里执行多任务。
相对进程而言,线程是一个更加接近于执行体的概...
阅读(30) 回复(0)
&div class=&postbody&&
&span&这两天在看Pthread 资料的时候,无意中看到&a href=&http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_detach.3.html& target=&_blank& rel=&nofollow&&这样&/a&一句话(man pthread_detach):&/span&
&pre&&span&Either &a href=&http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_join.3.html& target=&_blank& rel=&nofollow&&pthread_join(3)&/...
阅读(270) 回复(0)
This tutorial demostrates the use of the boost::asio::strand class to synchronies callback handlers in a multithreaded program .
该教程演示了如何将 boost::asio::strand 类应用于多线程编程中的同步化回调句柄中
The previous four tutorials avoided the issu of handler synchronisation by calling the io_service::run() function from one thread only .
前面的四个教程代码中避免了 通过调用 io_service::run() ...
阅读(120) 回复(0)
盛拓传媒:
北京皓辰网域网络信息技术有限公司. 版权所有
北京市公安局海淀分局网监中心备案编号:
广播电视节目制作经营许可证:编号(京)字第1149号
ITPUB推荐文章解答你所有技术难题您所在的位置: &
QT中实现Thread与GUI主线程连通方法
QT中实现Thread与GUI主线程连通方法
本文以QString为例,注册时首先Q_DECLARE_METATYPE(QString);然后,int id=qRegisterMetaType("QString");加上这两句就注册成功了。先来看内容。
本文介绍的是QT中实现Thread与GUI主线程通,目前只会一种,采用信号槽机制。
通常情况下,信号和槽机制可以同步操作,这就意味着在发射信号的时候,使用直接函数即可以立刻调用连接到一个信号上的多个槽。然而,当连接位于不同线程中的对象时,这一机制就会变得不同步起来,可以通过修改QObject::connect()的第5个可选参数而改变。
connect的第五个参数Qt::QueuedConnection表示槽函数由接受信号的线程所执行,如果不加表示槽函数由发出信号的次线程执行。当传递信号的参数类型不是QT的元类型时要先注册,关于QT的元类型可以参看QT文档
QMetaType这个类里面列举了所有的元类型。
以QString为例,注册时首先Q_DECLARE_METATYPE(QString);然后,int id=qRegisterMetaType&QString&(&QString&);加上这两句就注册成功了。
贴个示例的代码,次线程不断更改一个QString传给GUI主线程,主线程在GUI界面上显示。
mythread.h &#ifndef&MYTHREAD_H &#define&MYTHREAD_H &#include&&class&MyThread&:&public&QThread &{ &Q_OBJECT &public: &MyThread(); &~MyThread(); &protected: &void&run(); &signals: &void&changeText(QString&str); &}; &#endif&//&MYTHREAD_H &widgett.h &#ifndef&WIDGETT_H &#define&WIDGETT_H &#include&/QMainWindow&#include&&ui_widgett.h& &class&WidgetT&:&public&QMainWindow &{ &Q_OBJECT &public: &WidgetT(QWidget&*parent&=&0,&Qt::WFlags&flags&=&0); &~WidgetT(); &private: &Ui::WidgetTClass& &private&slots: &void&labelSetText(QString&qstr); &}; &#endif&//&WIDGETT_H &mythread.cpp &#include&&mythread.h& &MyThread::MyThread() &:&QThread() &{ &} &MyThread::~MyThread() &{ &} &void&MyThread::run(){ &static&int&i=0; &while(true) &{ &++i; &QString&strnum&=&QString::number(i); &emit&changeText(strnum); &&QThread::sleep(1); &} &} &widgett.cpp &#include&&widgett.h& &#include&&mythread.h& &Q_DECLARE_METATYPE(QString); &WidgetT::WidgetT(QWidget&*parent,&Qt::WFlags&flags) &:&QMainWindow(parent,&flags) &{ &ui.setupUi(this); &MyThread&*mythread&=&new&MyT &int&id=qRegisterMetaType(&&); &connect(mythread,SIGNAL(changeText(QString)),this,SLOT(labelSetText(QString)),Qt::QueuedConnection); &mythread-start(); &} &WidgetT::~WidgetT() &{ &} &void&WidgetT::labelSetText(QString&qstr){ &ui.label-setText(qstr); &}&
小结:QT中实现Thread与GUI主线程连通方法的内容介绍完了,在坛子里逛了一圈,解决线程的问题还真不少,最后还是希望本文对你有帮助。【编辑推荐】【责任编辑: TEL:(010)】
关于&&&&&&的更多文章
移动异构计算是相对于同构计算来说的,同构计算就是使用同一个处
现在的天气越来越冷了,感觉跟冬天似的,小编现在在发
在经过数月以及6个测试版本的体验后,iOS 7正式版终于
微软的思路很明确。一个消费者需要拥有几款电子设备?
Visual C++ 6.0是Microsoft公司的Visual Studio开发组件中最强大的编程工具,利用它可以开发出高性能的应用程序。本书由浅入深,
Windows Phone专家
Android开发专家
51CTO旗下网站新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
白手起家, 积分 4, 距离下一级还需 196 积分
论坛徽章:0
请教下各位大虾,我在次线程中要弹出一个消息框:
QMessageBox::critical(0,&Connect Error&,&WriteDout_08 lose!"
结果出来:
Xlib: unexpected async reply (sequence 0x10f53)!
Xlib: sequence lost (0x20f53 & 0x1106a) in reply type 0x0!
X Error: BadImplementation (server does not implement operation) 17
&&Major opcode: 20 (X_GetProperty)
&&Resource id:&&0x86505e0
QFont: It is not safe to use text and fonts outside the GUI thread
QFont: It is not safe to use text and fonts outside the GUI thread
QFont: It is not safe to use text and fonts outside the GUI thread
想问一下,在次线程中是否可以弹出消息框?
如果可以的话,应该如何做?
&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp
白手起家, 积分 122, 距离下一级还需 78 积分
论坛徽章:0
在线程中不能直接弹出的,你可以设置一个singal。slots在主线程中定义,slots的功能就是弹出对话框,singal在线程中定义,emit change()。在主线程中连接即可。
白手起家, 积分 4, 距离下一级还需 196 积分
论坛徽章:0
本来觉得要弹出的消息太多,还期望能在次线程中弹出能简单点,现在看来只能让主线程弹了。郁闷。还是vc方便,想弹就弹。呵呵
北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:
广播电视节目制作经营许可证(京) 字第1234号
中国互联网协会会员&&联系我们:
感谢所有关心和支持过ChinaUnix的朋友们
转载本站内容请注明原作者名及出处博客访问: 270442
博文数量: 138
博客积分: 455
博客等级: 一等列兵
技术积分: 747
注册时间:
APP发帖 享双倍积分
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: LINUX
本文介绍的是QT中实现Thread与GUI主线程通,目前只会一种,采用信号槽机制。
通常情况下,信号和槽机制可以同步操作,这就意味着在发射信号的时候,使用直接函数即可以立刻调用连接到一个信号上的多个槽。然而,当连接位于不同线程中的对象时,这一机制就会变得不同步起来,可以通过修改QObject::connect()的第5个可选参数而改变。
connect的第五个参数Qt::QueuedConnection表示槽函数由接受信号的线程所执行,如果不加表示槽函数由发出信号的次线程执行。当传递信号的参数类型不是QT的元类型时要先注册,关于QT的元类型可以参看QT文档
QMetaType这个类里面列举了所有的元类型。
以QString为例,注册时首先Q_DECLARE_METATYPE(QString);然后,int id=qRegisterMetaType("QString");加上这两句就注册成功了。
贴个示例的代码,次线程不断更改一个QString传给GUI主线程,主线程在GUI界面上显示。
1.mythread.h &
2.#ifndef&MYTHREAD_H &
3.#define&MYTHREAD_H &
4.#include&&
5.class&MyThread&:&public&QThread &
7.Q_OBJECT &
8.public: &
9.MyThread(); &
10.~MyThread(); &
11.protected: &
12.void&run(); &
13.signals: &
14.void&changeText(QString&str); &
16.#endif&//&MYTHREAD_H &
17.widgett.h &
18.#ifndef&WIDGETT_H &
19.#define&WIDGETT_H &
20.#include&<QtGui/QMainWindow>&
21.#include&"ui_widgett.h" &
22.class&WidgetT&:&public&QMainWindow &
24.Q_OBJECT &
25.public: &
26.WidgetT(QWidget&*parent&=&0,&Qt::WFlags&flags&=&0); &
27.~WidgetT(); &
28.private: &
29.Ui::WidgetTClass& &
30.private&slots: &
31.void&labelSetText(QString&qstr); &
33.#endif&//&WIDGETT_H &
34.mythread.cpp &
35.#include&"mythread.h" &
36.MyThread::MyThread() &
37.:&QThread() &
40.MyThread::~MyThread() &
43.void&MyThread::run(){ &
44.static&int&i=0; &
45.while(true) &
48.QString&strnum&=&QString::number(i); &
49.emit&changeText(strnum); &
51.QThread::sleep(1); &
54.widgett.cpp &
55.#include&"widgett.h" &
56.#include&"mythread.h" &
57.Q_DECLARE_METATYPE(QString); &
58.WidgetT::WidgetT(QWidget&*parent,&Qt::WFlags&flags) &
59.:&QMainWindow(parent,&flags) &
61.ui.setupUi(this); &
62.MyThread&*mythread&=&new&MyT &
63.int&id=qRegisterMetaType(""); &
64.connect(mythread,SIGNAL(changeText(QString)),this,SLOT(labelSetText(QString)),Qt::QueuedConnection); &
65.mythread->start(); &
67.WidgetT::~WidgetT() &
70.void&WidgetT::labelSetText(QString&qstr){ &
71.ui.label->setText(qstr); &
小结:QT中实现Thread与GUI主线程连通方法的内容介绍完了,最后还是希望本文对你有帮助。
附:&&&使用自定义的信号和槽,需要注意以下几点: &&&&&&&&&&& 1、类的声明和实现分别放在.h和.cpp文件中;&&&&&&&&&&&&&2、类声明中包含Q_OBJECT宏; &&&&&&&&&&& 3、信号只要声明不要设计其的实现函数; &&&&&&&&&&& 4、发射信号用emit关键字;&&&&&&&&&&&&&5、自定义槽的实现与普通成员函数的实现一样。
阅读(1788) | 评论(0) | 转发(1) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。Update GUI from another thread
Results 1 to 10 of 10
Linear Mode
Update GUI from another thread
Hi Everyone!
I'm struggling with the following problem for quite some time. My application runs heavy mathematical equations based on input files. The actual work is done within a separate thread. This way the GUI remains responsive. Everything works great, but I just can't figure out how to update the GUI from the separate thread. While processing the equations I want the GUI to show which equation is currently processed.
I just recently started using QT. This because I need to develop a application that runs on both Windows and Mac. The concept of slots is hard to me. I have developed many applications using Xcode and Cocoa. In this environment I can create complicated multithreading applications (that continuously update the GUI) without any problem. My C++ knowledge is average.
It drives me crazy. I spend to much time figuring this out. And still no result.
To the point:
Who can create an sample project for me?
The sample must contain:
- GUI with one text-box / button
- Clicking the button starts a new thread
- The new thread executes a loop 5 times.
- The loop simulates a heavy process (for example by using 1-second sleep)
- Every time the loop starts the GUI is updated by appending a line to the textbox.
I'm willing to pay for your time. I definitely need some help here. Please keep the sample as simple as possible. This way i can understand how it works.
(A little desperate)
Kind regards,
Re: Update GUI from another thread
How does your thread look like? Add in *.h file
Q_SIGNALS:void currentEquitation&#40;&#41;;
To copy to clipboard, switch view to plain text mode&
In the cpp, where you calculate a new equation simple do
Q_EMIT currentEquitation&#40;&Equitation foo&&#41;;
To copy to clipboard, switch view to plain text mode&
And then in your GUI class where you start your thread (assuming it is called thread and you have a QLabel called label):
::connect&#40;thread, SIGNAL&#40;currentEquitation&#40;&#41;&#41;, label, SLOT&#40;setText&#40;&#41;&#41;&#41;;
To copy to clipboard, switch view to plain text mode&
That's all.
Advanced user
Re: Update GUI from another thread
Hope this simple example will help you.
Attached Files
(2.7 KB, 217 views)
?scar Llarch i Galán
The following user says thank you to ^NyAw^ for this useful post:
&(14th July 2010)
Re: Update GUI from another thread
You don't have to subclass QThread, you can simply use existing classes for compilation and use them &directly&:
#include &QtGui&&&class calc : public &#123;
public Q_SLOTS:
void doCalc&#40;&#41;
for &#40;int i = 1; i & 25; ++i&#41;
Q_EMIT message&#40;::number&#40;i&#41;&#41;;
t = ::currentTime&#40;&#41;;
t = t.addSecs&#40;2&#41;;
while &#40;t & ::currentTime&#40;&#41;&#41; &#123;&#125;
Q_SIGNALS:
void message&#40;&#41;;&#125;;&&int main&#40;int argv, char** args&#41;&#123;
app&#40;argv, args&#41;;&
* b = new &#40;&start&, &w&#41;;
* l = new &#40;&w&#41;;
* le = new &#40;&w&#41;;
* la = new &#40;&#41;;
la-&addWidget&#40;l&#41;;
la-&addWidget&#40;b&#41;;
la-&addWidget&#40;le&#41;;
w.setLayout&#40;la&#41;;
w.show&#40;&#41;;&
c.moveToThread&#40;&t&#41;;
t.start&#40;&#41;;&
// Connections
::connect&#40;&c, SIGNAL&#40;message&#40;&#41;&#41;, l, SLOT&#40;setText&#40;&#41;&#41;&#41;;
::connect&#40;b, SIGNAL&#40;clicked&#40;&#41;&#41;, &c, SLOT&#40;doCalc&#40;&#41;&#41;&#41;;&
return app.exec&#40;&#41;;&#125;&#include &main.moc&
To copy to clipboard, switch view to plain text mode&
The following user says thank you to Lykurg for this useful post:
&(14th July 2010)
Re: Update GUI from another thread
Originally Posted by Anne
I'm willing to pay for your time.
If you want to offer a job, even if it is a small one, please use your &Jobs& forum.
Nevertheless, if you found the answers usefull, nobody will stop you if you spend some money to QtCentre.org or KDE e.V. or or or...
Re: Update GUI from another thread
Dear Lykurg and ^NyAw^,
Wow, is it just that simple?!
QT really amazes me.
You really helped me out here, everything is perfectly clear right now. I was thinking too complicated. In Xcode / Cocoa it's actually much harder to do exactly this. If you ever have any Xcode / Cocoa related question, please contact me!
Generally I never ask for help. I want to figure out things like these myself. In the long run this is the best way to learn a new language in my opinion. But in this case I was completely stuck.
I did not really intend to offer a job, was just asking for an example. Please give me some info on how to donate to qtcentre.org (can't find the PayPal address). I love communities like these and always support them.
All your help is greatly appreciated,
Kind regards,
Re: Update GUI from another thread
Originally Posted by Anne
Please give me some info on how to donate to qtcentre.org (can't find the PayPal address). I love communities like these and always support them.
Qt Centre paypal account is foundation at qtcentre.org.
Your biological and technological distinctiveness will be added to our own. Resistance is futile.
Please ask Qt related questions on the forum and not using private messages or visitor messages.
Re: Update GUI from another thread
Thanks for the address!
@^NyAw^ and Others
I tried to open your sample project, but the folder does not contain any .pro files.
The only files I see are .h, .cpp, .ui and .qrc.
How can I open such an project directly with QT? The .qrc is not recognized?
(Of cours I now just directly used the .cpp and .h files, but was just curious)
Re: Update GUI from another thread
You can run &qmake -project& inside that folder. Then Qt is generating a pro file for you.
Advanced user
Re: Update GUI from another thread
Originally Posted by Anne
@^NyAw^ and Others
I tried to open your sample project, but the folder does not contain any .pro files.
The only files I see are .h, .cpp, .ui and .qrc.
How can I open such an project directly with QT? The .qrc is not recognized?
(Of cours I now just directly used the .cpp and .h files, but was just curious)
I just don't added the pro file because I'm using Visual Studio on Windows and I'm not using the pro file, but there is an option to create the pro file that then I can attach.
Just use &qmake -project& as Lykurg said to create the &pro& file and then call &make& to compile the project.
Originally Posted by Anne
Generally I never ask for help
It's a good idea to learn by yourself, but sometimes the examples don't show what you are expecting or you are not able really how something works. Then, ask the forum! (but first of all try to find similar posts on it).
?scar Llarch i Galán
By lanmanck in forum Qt Programming
By jan in forum Qt Programming
By method in forum Qt Programming
By kikapu in forum Newbie
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.

我要回帖

更多关于 rt thread gui 的文章

 

随机推荐