[Interest] Never ending thread: blocking GUI
Lincoln Ramsay
a1291762 at gmail.com
Tue Oct 16 15:34:27 CEST 2012
On 16/10/12 11:15 PM, Sensei wrote:
> Once in a while, when the GUI needs it, it will "wake the thread up",
> and run a method of my QThread subclass, in my case, a "find in files"
> or "rename all files", or any other method.
>
> I thought I could simply make run() be an infinite loop, sleeping for a
> long period (one day).
You're doing it wrong.
You want an async QObject-based interface (call in via slot, answer via
signal).
> class ScanFiles : public QObject
> {
> public:
>
> ScanFiles(QObject *parent = 0) : QObject(parent) {};
>
> signals:
> QString searchResult();
> public slots:
>
> // Here I will place the methods that should be run by the thread
> void search(const QStringList &files);
> };
>
Then you just put that object on the thread, connect up your signals and
slots and you're good to go.
QThread *thread = new QThread;
thread->start();
ScanFiles *sf = new ScanFiles;
sf->moveToThread(thread);
connect(..., sf, SLOT(search(QStringList)));
connect(sf, SIGNAL(searchResult()), ...);
--
Link
More information about the Interest
mailing list