[Interest] Which thread implementation is best to trigger external library via timers

Hugo Drumond Jacob hugo at jacob.eng.br
Thu Apr 10 21:25:37 CEST 2014


Why not using an wrapper, a cache to hold IP, port and others worker's
members and repassing worker signals... Something like this:

class NetworkWorker : public QObject
{
    Q_OBJECT

public:
    NetworkWorker::NetworkWorker()
    {
        QTimer *timer = new QTimer(this);
        connect(timer, &QTimer::timeout, m_library, &Library::DoTask());
        timer->start(50);
    }

    QString getIP() const;
    int getPort() const;
    QString getServerName() const;
    QStringList getKnownServers() const;

public slots:

    void setIP(const QString &ip);
    void setPort(int port);

signals:
    void ipChanged(const QString& ip);
    void portChanged(int port);

private:
    Network *m_network;
};


class NetworkWrapper : public QObject
{
    Q_OBJECT

public:
    NetworkWrapper(QObject* parent = 0)
    : QObject(parent),
      m_worker(new NetworkWorker),
      m_thread(new QThread(this))
    {
        //Repass worker signals
        connect(m_worker, SIGNAL(ipChanged(const QString&)), this,
SIGNAL(ipChanged(const QString&)));
        connect(m_worker, SIGNAL(portChanged(int)), this,
SIGNAL(portChanged(int)));

        m_worker->moveToThread(m_thread);
        m_thread->start();
    }

    ~NetworkWrapper()
    {
        m_thread->quit();
        m_thread->wait();

        delete m_worker;
    }

    QString getIP() const { return m_cache.ip; }
    int getPort() const { return m_cache.port; }

public slots:

    void setIP(const QString &ip)
    {
        m_cache.ip = ip;
        QMetaObject::invokeMethod(m_worker, "setIP", Qt::QueuedConnection,
Q_ARG(const QString &, m_cache.ip));
    }

    void setPort(int port)
    {
        m_cache.port = port;
        QMetaObject::invokeMethod(m_worker, "setPort",
Qt::QueuedConnection, Q_ARG(int, m_cache.port));
    }

signals:
    void ipChanged(const QString& ip);
    void portChanged(int port);

private:
    NetworkWorker* m_worker;
    QThread* m_thread;

    struct Cache
    {
        QString ip;
        int port;
    } m_cache;

}

Hugo Drumond Jacob


2014-04-10 15:11 GMT-03:00 Thiago Macieira <thiago.macieira at intel.com>:

> Em qui 10 abr 2014, às 12:17:15, K. Frank escreveu:
> > But Thiago -- You, of all people! -- How's he then going to get his
> > queued-event slot calls to run in the worker thread unless he calls
> > ThreadClass::moveToThread (this) ?!!?
>
> I didn't read the entire email...
>
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140410/2193026e/attachment.html>


More information about the Interest mailing list