仅将QThread用于对象 [英] use QThread exclusively for an object

查看:72
本文介绍了仅将QThread用于对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT2

这是解决以下问题的解决方案,该问题专门为对象提供了QThread.

Here is the solution that works for the following problem of giving a QThread exclusively to the object.

我已经改变了解决问题的方法.我不想再关闭MyClass中的QThread,因为以下解决方案看起来更简单,而且外观也不差.

I've changed the approach for the problem. I don't want to close QThread in MyClass anymore, cause the following solution seems easier and not too bad looking.

我的解决方案是此处给出的解决方案的修改:

My solution is modification of the solution given here: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

该解决方案的问题是QObject worker并未真正删除(选中).

The problem with that solution was that QObject worker wasn't really deleted (checked it).

QThread *myThread = new QThread();
SCIntermediary* myObj = new MyClass();
myObj->moveToThread(myThread);
connect(myThread, SIGNAL(started()), myObj, SLOT(setup()));
connect(myObj, SIGNAL(destroyed()), myThread, SLOT(quit()), Qt::DirectConnection);
connect(myObj, SIGNAL(finished()), myObj, SLOT(deleteLater()));
connect(myThread, SIGNAL(finished()), myThread, SLOT(deleteLater()));
myThread -> start();

// do your work bla bla bla

myObj -> finishIt(); // this function only emits finish() signal of myObj
myThread -> wait();

这是第一个对我有用的解决方案,可完全销毁myObj和myThread,而没有任何错误或其他麻烦.

This is the first solution that worked for me destroying both myObj and myThread without any errors or other troubles at all.

ENDOF EDIT

我正在尝试创建一个类,它将在客户端和服务器之间做一些事情.我希望它具有自己的线程.所以我要做的是:

I'm trying to create a class will do some stuff between my client and my server. I'd like it to have it's own thread. So what I did is:

class Myclass : public QObject {
    Q_OBJECT
public:
    Myclass();
    ~Myclass();

private:
    QThread *my_thread;
    QTcpSocket *sock;
}

这是我编写构造函数的方式:

Here is how I coded my constructor:

Myclass::Myclass(){
    my_thread = new QThread();
    my_thread -> start();
    moveToThread(my_thread);

    sock = new QTcpSocket(this);
    sock -> connectToHost("host", port);
}

这不起作用.它没有用,因为TcpSocket的代码未在当前父对象所在的同一线程中执行.因此,我决定要做的是创建用于设置的插槽和信号,并在我的构造函数中发出它.这是我的代码现在的样子.

This didn't work. It didn't work, because the code for TcpSocket wasn't executed in the same thread the parent object currently is. So what I decided to do is to create slot and signal for setup and emit it in my constructor. Here is how my code looks right now.

class Myclass : public QObject {
    Q_OBJECT
public:
    Myclass();
    ~Myclass();

public slots:
    void setup();

signals:
    void do_setup();

private:
    QThread *my_thread;
    QTcpSocket *sock;
}

其中一些实现

Myclass::Myclass(){
    my_thread = new QThread();
    my_thread -> start();
    moveToThread(my_thread);

    connect(this, SIGNAL(do_setup()), this, SLOT(setup()));
    emit do_setup();
}

void Myclass::setup(){
    sock = new QTcpSocket();
    sock -> connectToHost("host", port);
}

现在它可以正常工作了,可悲的是,它看起来很糟糕!这太糟糕了,我不知道如何使它看起来更好,或者首先应该如何完成这种模式.您会建议我做什么以达到相同的效果?

Now it works and here comes the sad part - it looks terrible! It's awful and I have no idea how to make it look better or how such pattern should be done in the first place. What would you advice me to do to get the same effect?

此外-我不知道如何为此编写好的析构函数-如何顺利删除QThread和所有类对象.

Additionally - I have no idea how to write good destructor for this - how to delete QThread smoothly and all the class objects..

编辑

就目前而言,我认为解决方案已经不错-剩下的唯一问题是如何为此编写析构函数.我不知道如何在这里完成.

For now I believe the solution is quite alright - the only problem left is how to write a destructor for that. I have no idea how it can be done here.

推荐答案

对于析构函数,请查看Maya Posch示例: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ 由于您的线程不是MyClass的子级,也许您可​​以在析构函数中作为最后一件事在线程上调用finish(),并将MyClass的destroy()信号连接到线程的deleteLater上?

For destructor, check out Maya Posch example: http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ Since your thread is not child of MyClass, perhaps you can call finish() on thread as last thing in destructor and connect destroyed() signal of MyClass to deleteLater for thread?

这篇关于仅将QThread用于对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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