QTimer只能与以QThread开头的线程一起使用 [英] QTimer can only be used with threads started with QThread

查看:182
本文介绍了QTimer只能与以QThread开头的线程一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个有趣的问题....我正在尝试编写的程序因该错误而崩溃:

So I have an interesting problem....a program I am (trying) to write is crashing with this error:

QObject::startTimer: QTimer can only be used with threads started with QThread 

让我感到困惑的是我的程序是单线程的.该类的目标是将POST数据发送到我在服务器上拥有的php页面.一旦它尝试发送POST,我就会收到该消息.这是我的代码.

The thing that baffles me is my program is single threaded. The goal of the class in question is to send POST data to a php page I have on my server. As soon as it tries to send the POST, I get that message. Here is my code.

#ifndef TRANSMISSIONS_H
#define TRANSMISSIONS_H
#include "name_spawn.h"
#include <QNetworkReply>
#include <QObject>
#include <QNetworkConfigurationManager>

class Transmissions : public QObject
{
    Q_OBJECT
public:
    Transmissions();
    void Send(GeneratedData);
public slots:
    void serviceRequestFinished(QNetworkReply*);
signals:
    void configurationAdded(const QNetworkConfiguration);
    void configurationChanged(const QNetworkConfiguration);
    void configurationRemoved(const QNetworkConfiguration);
    void onlineStateChanged(bool);
    void updateCompleted();
};

#endif // TRANSMISSIONS_H

还有

#include "transmissions.h"
#include "name_spawn.h"
#include <QHttp>
#include <QUrl>
#include <QString>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <iostream>
#include <QNetworkAccessManager>
#include <QNetworkConfigurationManager>
#include <QObject>

using namespace std;

Transmissions::Transmissions()
{
}

void Transmissions::Send(GeneratedData User)
{
    cerr<<"transmitting"<<endl;
    QUrl serviceUrl = QUrl("http://192.168.1.138/postTest.php");
    QByteArray postData;
    QString username="user="+User.Email()+"&";
    QString Passwd="password="+User.pass();
    postData.append(username);
    postData.append(Passwd);

    QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
    connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(serviceRequestFinished(QNetworkReply*)));
    networkManager->post(QNetworkRequest(serviceUrl), postData);
}

void Transmissions::serviceRequestFinished(QNetworkReply *reply)
{
    //this will do other things once post is working
    QString data = reply->readAll();
    cerr <<"Data is "<< data.toStdString()<<endl;

}

我认为我想做的事情很简单,但这使我无休止地尝试使其工作.我在文档中没有看到有关需要线程的QNetworkAccessManager的任何信息.我承认我不太了解Qt,所以任何帮助(或指向完整POST示例的链接)将不胜感激.

I think what I am trying to do is fairly simple, but it is frustrating me to no end trying to get it to work. I didn't see anything in the documentation about QNetworkAccessManager requiring threads. I admit I don't know Qt that well, so any help (or links to complete POST examples) would be very appreciated.

推荐答案

要使用QTimer,您需要有一个事件循环. QNAM显然使用计时器来定期检查网络回复.

To use a QTimer you need to have an event loop. QNAM obviously uses a timer to periodically check for the network reply.

您需要使用QCoreApplication::exec()启动应用程序事件循环,然后再调用post之类的QNAM方法.

You need to start the application event loop with QCoreApplication::exec() and then call QNAM methods like post after that.

我认为您可以在exec之前致电post,但是您可能会遇到此错误.

I think you can call post before exec but you may come across this bug.

此外,请注意,直到Qt 4.7 QNAM都没有使用线程,而是使用4.8

Also, note that up to Qt 4.7 QNAM did not use threading but with 4.8 this is changing.

这篇关于QTimer只能与以QThread开头的线程一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆