multithreadingqt

Passing arguments to a run() method of QThread


I've subclassed my Qthread so I can implement my code in run() method. I have to pass it some parameters,

I tried it like this, so what's wrong in here?

class QMyThread :
    public QThread
{
public:
    QMyThread();
    ~QMyThread(void);
    virtual void start(FILE *data, int sock, int bits);

protected:
    virtual void run(FILE *data, int sock, int bits);
};

run method;

void QMyThread::run(FILE *data, int sock, int bits)
{
//do stuff
}

start the thread:

QMyThread *thread;
thread->start(datafile, sockint, bitsint);

first it says the thread might not be initialized and then it crashes in the start() method with SIGSEGV error. Anyone can help me?


Solution

  • You shouldn't be subclassing the QThread class as this is no longer the recommended way of using QThread.

    For more information http://qt-project.org/doc/qt-4.8/qthread.html

    To answer your question, couldn't you just make those parameters members of your class and assign their values through setters or its contructor?