//threadTest.h
#ifndef THREADTEST_H
#define THREADTEST_H
#include <qthread.h>
#include "test.h"
class MyThread : public QThread
{
protected :
virtual void run();
};
MyThread myThread;
void Test::newSlot()
{
myThread.start();
}
#endif
////////////////////////////////
//threadTest.cpp
#include <QDialog>
void MyThread::run()
{
for(int i=0;i <100;i++)
{
printf("new value i is:%d",i);
//QMessageBox::information(this,tr("info"),tr("load dll OK!"));
//QMessageBox::information(this, tr("Empty Search "),"click Find.");
//Test m;
//m.show();
qDebug("mhf");
}
/*
QDialog *dialog = new QDialog(0,”popup”,FALSE);
dialog->setCaption(“A QDialog Window”);
dialog->setMinimumSize(200,80);
dialog->setMaximumSize(200,80);
dialog->show();
*/
}
/////////////////////////////////////////////////////////////
//test.cpp
#include "test.h"
#include <qvariant.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qtextedit.h>
#include <qlayout.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
#include <QVBoxLayout>
/*
* Constructs a Test as a child of 'parent', with the
* name 'name' and widget flags set to 'f'.
*/
Test::Test( QWidget* parent ): QWidget( parent=0 )
{
this->setWindowTitle("w");
this->resize(250, 50);
textEdit1 = new QTextEdit( "textEdit1" );
textEdit1->setGeometry( QRect( 10, 60, 570, 291 ) );
lineEdit1 = new QLineEdit( "lineEdit1" );
lineEdit1->setGeometry( QRect( 10, 360, 571, 31 ) );
pushButton2 = new QPushButton( "pushButton2" );
pushButton2->setGeometry( QRect( 380, 400, 201, 41 ) );
pushButton1 = new QPushButton( "pushButton1" );
pushButton1->setGeometry( QRect( 10, 10, 261, 41 ) );
//languageChange();
resize( QSize(600, 480).expandedTo(minimumSizeHint()) );
// signals and slots connections
connect( pushButton1, SIGNAL( clicked() ), this, SLOT( newSlot() ) );
connect( pushButton2, SIGNAL( clicked() ), this, SLOT( languageChange() ) );
//add
QVBoxLayout *layout=new QVBoxLayout;
layout->addWidget(textEdit1);
layout->addWidget(lineEdit1);
layout->addWidget(pushButton1);
layout->addWidget(pushButton2);
this->setLayout(layout);
this->resize(350, 200);
}
/*
* Destroys the object and frees any allocated resources
*/
Test::~Test()
{
// no need to delete child widgets, Qt does it all for us
}
/*
* Sets the strings of the subwidgets using the current
* language.
*/
void Test::languageChange()
{
this->setWindowTitle("m");
//printf("new value i is");
pushButton2->setText( tr( "pushButton2" ) );
pushButton1->setText( tr( "pushButton1" ) );
}
///////////////////////////////////////
//test.h
#ifndef TEST_H
#define TEST_H
#include <qvariant.h>
#include <qwidget.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLineEdit;
class QPushButton;
class QTextEdit;
class Test : public QWidget
{
Q_OBJECT
public:
Test( QWidget* parent = 0 );
~Test();
QTextEdit* textEdit1;
QLineEdit* lineEdit1;
QPushButton* pushButton2;
QPushButton* pushButton1;
public slots:
virtual void newSlot();
protected:
protected slots:
virtual void languageChange();
};
#endif // TEST_H
//////////////////////////////////////
main.cpp
#include <qapplication.h>
#include "test.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Test w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
/////////////
调试运行
gdb *.exe
run step点击按钮就可以看到输出调试信息
相关推荐
使用QThread,创建2个QObject对象,两个对象movetoThread,退出。
本篇文章将详细解析标题为“Qt多线程的一个简单例子”的示例项目,通过分析描述中的“用QT实现的一个简单多线程例子”,我们将深入理解Qt如何使用QThread进行多线程操作。 首先,我们需要了解Qt中的QThread类。...
总结来说,QT线程QThread的推荐用法包括使用`QtConcurrent::run()`来执行一次性任务,以及通过`moveToThread()`方法将QObject实例移到新线程中长期运行。这些方法简化了多线程编程,提高了程序效率,并确保了与主线...
第一个例子“调试---1.tar.gz”可能是一个用于调试多线程应用程序的工具,帮助开发者跟踪线程间的交互和同步问题。调试多线程程序是一项挑战,因为它涉及到线程间的复杂关系和同步原语,如信号和槽、mutexes、...
这两个类可能都有自己的run()函数,这是Qt线程执行的主要入口点。每个线程在访问共享变量之前,都会尝试获取QMutex的锁。获取到锁的线程可以安全地访问变量,而其他线程会等待直到锁被释放。 下面是一个简单的示例...
Qt是一个跨平台的C++图形用户界面库,它提供了丰富的功能来支持多线程编程。本文将深入探讨在Qt中实现多线程的实例,并解释相关知识点。 首先,我们要理解Qt中的线程模型。Qt提供两种主要的线程类:QThread和Qt ...
QT4线程同步的小例子是针对QT框架下多线程编程的一个示例,它为初学者提供了一个深入了解线程同步机制的平台。在多线程编程中,线程同步是确保多个线程间数据安全、避免竞态条件和死锁的关键技术。QT库提供了丰富的...
首先,要创建一个新的线程,你需要继承QThread类并重写`run()`函数。`run()`函数是新线程执行的核心,当调用QThread对象的`start()`方法时,这个函数会被执行。例如: ```cpp class MyThread : public QThread { Q...
在这个例子中,我们可能会看到一个自定义的线程类继承自QThread,然后在线程类中重写run()函数来执行我们需要的计算或任务。同时,为了避免直接操作UI,我们通常会使用信号和槽机制,将线程中的结果通过信号发送到...
Qt库,一个跨平台的应用程序开发框架,提供了丰富的API来支持线程和进程间的通信。本实例将深入探讨Qt如何实现线程间通信以及线程与进程间通信,并提供简单易懂的代码示例。 一、Qt线程间通信 Qt通过信号和槽机制...
Qt库,一个流行的跨平台应用开发框架,提供了QThread类来支持线程操作。本篇将深入探讨"QThread练习例子"中涉及的知识点,帮助你更好地理解和运用Qt的多线程功能。 QThread是Qt中的一个关键类,它为C++程序员提供了...
在编程领域,尤其是在GUI(图形用户界面)应用开发中,多线程技术是不可或缺的。Qt框架提供了一套强大的线程支持,使得开发者能够在...通过实践和学习提供的“Qt线程间通信例子”,你可以更深入地理解和应用这些概念。
QThread提供了一个相对简单的方式来管理和控制线程,使得我们可以将耗时的操作放在后台线程中执行。 在QT4中,QThread被设计用来承载QObject对象,允许对象在自己的线程中运行。这可以通过继承QThread类并重写`run...
这个例子是一个使用Qt的TCP服务器,通过多线程技术来处理客户端连接,确保在规定时间内(3秒)完成数据交换。 首先,我们要理解Qt中的网络模块,它提供了丰富的类来处理TCP和UDP通信。在这个例子中,`QTcpServer`类...
QThread是Qt中的线程类,它提供了一个用户友好的接口,用于创建和管理线程。QThread拥有自己的事件循环,这意味着我们可以在子线程中使用Qt的事件驱动模型。通常,我们不会直接继承QThread,而是创建一个类并重写run...
Qt库,作为一个跨平台的应用程序开发框架,提供了强大的多线程支持。本篇我们将深入探讨“Qt多线程经典例子”,通过学习实例,理解如何在Qt环境中有效利用多线程。 1. **Qt中的线程** Qt为开发者提供了QThread类,...
创建一个新的线程时,通常会继承QThread类并重写run()方法,这个方法将在线程的上下文中运行。此外,我们还可以使用start()方法启动线程,而quit()和wait()方法则用于结束和等待线程的完成。 在QT4版本中,多线程的...
在本文中,我们将深入探讨如何在Visual Studio 2015 (VS2015) 集成环境中,利用QT5库创建GUI程序,并实现主窗体与子线程QThread之间的通信。QT5是一个功能强大的跨平台应用程序开发框架,而QThread是其用于处理多...
首先,`QThread`是QT中的一个核心类,它为创建和管理线程提供了一种面向对象的方法。与标准C++的`std::thread`不同,`QThread`允许我们在一个新的执行上下文中运行对象的方法,而不是直接运行可重入的函数。这种设计...
QT线程实例是关于Qt框架中线程应用的一个实践示例。Qt是一个强大的跨平台应用程序开发框架,由C++编写,广泛应用于桌面、移动和嵌入式设备的GUI设计。在多核处理器和高并发需求的现代软件开发中,线程技术显得尤为...