Discussion:
How to start object in the thread?
(too old to reply)
i***@gmail.com
2014-06-08 10:10:25 UTC
Permalink
Hi,

for example I have Object with slot start.

And I create that object with the next code:

QCoreApplication app( argc, argv );

Object o;
QThread t;
o.moveToThread( &t );
t.start();

QTimer::singleShot( 0, &o, SLOT( start() ) );

app.exec();

But this code doesn’t invoke slot start() after launching.

And there is my question: how to invoke SLOT start() of Object on the different thread and app start-up?

Thanks.
Roland Winklmeier
2014-06-08 09:43:30 UTC
Permalink
Post by i***@gmail.com
QCoreApplication app( argc, argv );
Object o;
QThread t;
o.moveToThread( &t );
t.start();
QTimer::singleShot( 0, &o, SLOT( start() ) );
Hi Igor,

why are you using a QTimer? As far as I know, you could invoke the slot
directly.

QMetaObject::invokeMethod(&o, "start", Qt::QueuedConnection);

This will place an event in the threads event loop. This works in
several places in my project. Maybe there is a better solution.
In principle you can also omit Qt::QueuedConnection.

QMetaObject::invokeMethod(&o, "start");

Default is Qt::AutoConnection, which detects o is running in a different
thread. But I usually pass QueuedConnection explicitly.

Roland
i***@gmail.com
2014-06-08 10:42:39 UTC
Permalink
I’ve added Launcher that works on the main thread. His job is just emit start() signal.
My Object on the different thread connects to this start() signal.

Now Launcher emits start() signal. All is ok. But Object doesn’t receive it... Why?

From: ***@gmail.com
Sent: Sunday, June 08, 2014 12:10 PM
To: ***@qt-project.org
Subject: How to start object in the thread?

Hi,

for example I have Object with slot start.

And I create that object with the next code:

QCoreApplication app( argc, argv );

Object o;
QThread t;
o.moveToThread( &t );
t.start();

QTimer::singleShot( 0, &o, SLOT( start() ) );

app.exec();

But this code doesn’t invoke slot start() after launching.

And there is my question: how to invoke SLOT start() of Object on the different thread and app start-up?

Thanks.
K. Frank
2014-06-08 14:05:35 UTC
Permalink
Hello Igor!
I’ve added Launcher that works on the main thread. His job is just emit
start() signal.
My Object on the different thread connects to this start() signal.
Now Launcher emits start() signal. All is ok. But Object doesn’t receive
it... Why?
From: igor
Sent: Sunday, June 08, 2014 12:10 PM
...
Hi,
for example I have Object with slot start.
QCoreApplication app( argc, argv );
Object o;
QThread t;
o.moveToThread( &t );
t.start();
QTimer::singleShot( 0, &o, SLOT( start() ) );
app.exec();
But this code doesn’t invoke slot start() after launching.
Your code looks right to me. (I'm not saying it is right -- I haven't
tried it and it's easy for me to overlook errors that ought to be
obvious.)
And there is my question: how to invoke SLOT start() of Object on the
different thread and app start-up?
Could you show the code for Object? Or if Object actually
does something, could you show the code for a stripped-down
version that demonstrates the same issue?

Some trivial questions, but just to be sure: Does Object derive
from QObject? If so, how? How do you know that Object::start()
isn't being called? How is your code instrumented?
Thanks.
Good luck!


K. Frank
Thiago Macieira
2014-06-08 17:28:13 UTC
Permalink
I’ve added Launcher that works on the main thread. His job is just emit
start() signal. My Object on the different thread connects to this start()
signal.
Now Launcher emits start() signal. All is ok. But Object doesn’t receive
it... Why?
Check that:
a) the signal was really emitted
b) the receiver was not deleted
c) the connection was successful (your earlier code had SLOT(start()), not a
signal)
d) the event loop is running on the target thread
e) there are no warnings printed to stderr or standard debug
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
i***@gmail.com
2014-06-08 22:10:33 UTC
Permalink
Thanks guys for your replies.

I've found the problem. The problem was in Object's event() method...

But now I have another problem.

In my test app I have custom event that I post with
QCoreApplication::postEvent().

All seems ok, events posted and received but with another type that I
expect.

static const int changeColorEventType = QEvent::registerEventType();

class ChangeColorEvent
: public QEvent
{
public:
ChangeColorEvent()
: QEvent( static_cast< QEvent::Type >( changeColorEventType ) )
{
}
};

Is this code right?

In my Object's event() I receive events with type 65533 when
changeColorEventType is 65535...

-----Original Message-----
From: Thiago Macieira
Sent: Sunday, June 08, 2014 7:28 PM
To: ***@qt-project.org
Subject: Re: [Interest] How to start object in the thread?
I’ve added Launcher that works on the main thread. His job is just emit
start() signal. My Object on the different thread connects to this start()
signal.
Now Launcher emits start() signal. All is ok. But Object doesn’t receive
it... Why?
Check that:
a) the signal was really emitted
b) the receiver was not deleted
c) the connection was successful (your earlier code had SLOT(start()), not a
signal)
d) the event loop is running on the target thread
e) there are no warnings printed to stderr or standard debug
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Thiago Macieira
2014-06-08 22:24:26 UTC
Permalink
Post by i***@gmail.com
Thanks guys for your replies.
I've found the problem. The problem was in Object's event() method...
But now I have another problem.
In my test app I have custom event that I post with
QCoreApplication::postEvent().
All seems ok, events posted and received but with another type that I
expect.
static const int changeColorEventType = QEvent::registerEventType();
class ChangeColorEvent
: public QEvent
{
ChangeColorEvent()
: QEvent( static_cast< QEvent::Type >( changeColorEventType ) )
{
}
};
Is this code right?
Yes.
Post by i***@gmail.com
In my Object's event() I receive events with type 65533 when
changeColorEventType is 65535...
That's another event, not yours. Don't handle it, just pass to QObject's
handler.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
i***@gmail.com
2014-06-08 23:37:24 UTC
Permalink
Actually Thiago it's my event. It's looks like a bug.

When I changed

static const int changeColorEventType = QEvent::registerEventType();

to

static const int changeColorEventType = 2000;

all works fine.

P.S. In my test app I have only one custom event...

-----Original Message-----
From: Thiago Macieira
Sent: Monday, June 09, 2014 12:24 AM
To: ***@qt-project.org
Subject: Re: [Interest] How to start object in the thread?
Post by i***@gmail.com
Thanks guys for your replies.
I've found the problem. The problem was in Object's event() method...
But now I have another problem.
In my test app I have custom event that I post with
QCoreApplication::postEvent().
All seems ok, events posted and received but with another type that I
expect.
static const int changeColorEventType = QEvent::registerEventType();
class ChangeColorEvent
: public QEvent
{
ChangeColorEvent()
: QEvent( static_cast< QEvent::Type >( changeColorEventType ) )
{
}
};
Is this code right?
Yes.
Post by i***@gmail.com
In my Object's event() I receive events with type 65533 when
changeColorEventType is 65535...
That's another event, not yours. Don't handle it, just pass to QObject's
handler.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
i***@gmail.com
2014-06-09 05:59:45 UTC
Permalink
Sorry... It's not a bug... It's my mistake...

-----Original Message-----
From: ***@gmail.com
Sent: Monday, June 09, 2014 1:37 AM
To: Thiago Macieira ; ***@qt-project.org
Subject: Re: [Interest] How to start object in the thread?

Actually Thiago it's my event. It's looks like a bug.

When I changed

static const int changeColorEventType = QEvent::registerEventType();

to

static const int changeColorEventType = 2000;

all works fine.

P.S. In my test app I have only one custom event...

-----Original Message-----
From: Thiago Macieira
Sent: Monday, June 09, 2014 12:24 AM
To: ***@qt-project.org
Subject: Re: [Interest] How to start object in the thread?
Post by i***@gmail.com
Thanks guys for your replies.
I've found the problem. The problem was in Object's event() method...
But now I have another problem.
In my test app I have custom event that I post with
QCoreApplication::postEvent().
All seems ok, events posted and received but with another type that I
expect.
static const int changeColorEventType = QEvent::registerEventType();
class ChangeColorEvent
: public QEvent
{
ChangeColorEvent()
: QEvent( static_cast< QEvent::Type >( changeColorEventType ) )
{
}
};
Is this code right?
Yes.
Post by i***@gmail.com
In my Object's event() I receive events with type 65533 when
changeColorEventType is 65535...
That's another event, not yours. Don't handle it, just pass to QObject's
handler.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Continue reading on narkive:
Loading...