当前位置: 技术问答>linux和unix
新手求教QT4 Qthread线程问题!求帮忙看看code谢谢!
来源: 互联网 发布时间:2016-05-29
本文导语: 我是新手,第一次接触QT4,想用Qt4写一个简单的Qthread小程序,只要能实现点一下按钮就自动循环改变label里面的文字就行了,可我弄了半天,网上查了半天还是不会,请大家帮我看看我哪里错了,谢谢。 ui很简单,一...
我是新手,第一次接触QT4,想用Qt4写一个简单的Qthread小程序,只要能实现点一下按钮就自动循环改变label里面的文字就行了,可我弄了半天,网上查了半天还是不会,请大家帮我看看我哪里错了,谢谢。
ui很简单,一个label两个按钮,一个按钮按了就循环5次改变label里面的文字,另一个按了退出
用uic自动生成的.h文件
mainform.h
然后是main.cpp
TCPServer.h
TCPServer.cpp
谢谢大家帮我看看哪里错了,编译没有问题,就是运行没反应,有的时候一点按钮就退出了。
ui很简单,一个label两个按钮,一个按钮按了就循环5次改变label里面的文字,另一个按了退出
用uic自动生成的.h文件
mainform.h
/********************************************************************************
** Form generated from reading ui file 'mainform.ui'
**
** Created: Wed Apr 15 03:00:33 2009
** by: Qt User Interface Compiler version 4.4.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/
#ifndef MAINFORM_H
#define MAINFORM_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
QT_BEGIN_NAMESPACE
class Ui_mainForm
{
public:
QLabel *label;
QWidget *widget;
QHBoxLayout *horizontalLayout;
QSpacerItem *horizontalSpacer;
QPushButton *pushButton_1;
QSpacerItem *horizontalSpacer_2;
QPushButton *pushButton_2;
QSpacerItem *horizontalSpacer_3;
void setupUi(QDialog *mainForm)
{
if (mainForm->objectName().isEmpty())
mainForm->setObjectName(QString::fromUtf8("mainForm"));
mainForm->setWindowModality(Qt::NonModal);
mainForm->resize(436, 236);
mainForm->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
label = new QLabel(mainForm);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(40, 20, 341, 151));
widget = new QWidget(mainForm);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(9, 195, 418, 32));
horizontalLayout = new QHBoxLayout(widget);
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer);
pushButton_1 = new QPushButton(widget);
pushButton_1->setObjectName(QString::fromUtf8("pushButton_1"));
horizontalLayout->addWidget(pushButton_1);
horizontalSpacer_2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_2);
pushButton_2 = new QPushButton(widget);
pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
horizontalLayout->addWidget(pushButton_2);
horizontalSpacer_3 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout->addItem(horizontalSpacer_3);
retranslateUi(mainForm);
QObject::connect(pushButton_2, SIGNAL(clicked()), mainForm, SLOT(close()));
QMetaObject::connectSlotsByName(mainForm);
} // setupUi
void retranslateUi(QDialog *mainForm)
{
mainForm->setWindowTitle(QApplication::translate("mainForm", "TCP Server", 0, QApplication::UnicodeUTF8));
label->setText(QString());
pushButton_1->setText(QApplication::translate("mainForm", "Run", 0, QApplication::UnicodeUTF8));
pushButton_2->setText(QApplication::translate("mainForm", "Exit", 0, QApplication::UnicodeUTF8));
Q_UNUSED(mainForm);
} // retranslateUi
};
namespace Ui {
class mainForm: public Ui_mainForm {};
} // namespace Ui
QT_END_NAMESPACE
#endif // MAINFORM_H
然后是main.cpp
#include
#include "TCPServer.h"
int main(int argc, char **argv)
{
QApplication app(argc, argv);
TCPServer *tcpserver= new TCPServer;
return tcpserver->exec();
}
TCPServer.h
#ifndef DLG_H
#define DLG_H
#include
#include
#include
class TCPServer : public QDialog
{
Q_OBJECT
public:
TCPServer();
public slots:
void runtcp();
private:
Ui::mainForm ui;
};
class MyThread : public QThread
{
public:
virtual void run();
private:
Ui::mainForm ui;
};
#endif
TCPServer.cpp
#include "TCPServer.h"
TCPServer::TCPServer()
{
ui.setupUi(this);
QObject::connect(ui.pushButton_1, SIGNAL(clicked()), this, SLOT(runtcp()));
QObject::connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(close()));
}
void TCPServer::runtcp()
{
MyThread a;
a.start();
}
void MyThread::run()
{
QString strResult;
for( int count = 0; count setText(strResult);
}
}
谢谢大家帮我看看哪里错了,编译没有问题,就是运行没反应,有的时候一点按钮就退出了。
|
晕,你自己好好看看,你用的是什么?QThread是这个类啊,你用的是TCPServer.我还以为你要写套接字呢.
QThread很好用啊,C_GUI_Programming_with_Qt_4.这本书(有文档),有用法个,例子代码啊,你入门QT看这本书就可以了.
|
main函数怎么回事?
试试
试试
int main(int argc, char **argv)
{
QApplication app(argc, argv);
TCPServer *tcpserver= new TCPServer;
////////////////////
tcpserver->show();
//////////////////////
return app.exec();
}
|
图形界面接触不多,帮你顶一下,楼下来解决吧...