[Qt-interest] QGraphicsScene problem
Patric
userqt at gmail.com
Sat Aug 29 11:53:16 CEST 2009
Another thing I have noticed,
if instead of moveBy() we use for example
textItem = new QGraphicsTextItem();
textItem->setPlainText(QString::number(GlobalVariables::specieOnTurn));
inside run(), this error is thrown :
ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to
objects owned by a different thread. Current thread 8deda50. Receiver '' (of
type 'QTextDocument') was created in thread 3d4988", file
kernel\qcoreapplication.cpp, line 305
So we can move items from other threads, but we can't do.. other things.
Like setting the text of a QGraphicsTextItem.
----- Original Message -----
From: "Patric" <userqt at gmail.com>
To: "Sean Harmer" <sean.harmer at maps-technology.com>;
<qt-interest at trolltech.com>
Sent: Saturday, August 29, 2009 12:31 PM
Subject: Re: [Qt-interest] QGraphicsScene problem
>I have simulated the error with the timer. However, my problem with the
>"doing nothing" items is missing... Here is what I do :
> --------------------
> WorkerThread.h :
> ---------------------
> #ifndef WORKERTHREAD_H
> #define WORKERTHREAD_H
>
>
> #include <QThread>
>
> #include <QGraphicsScene>
>
> #include <QGraphicsItem>
>
> #include <QList>
>
>
> class WorkerThread : QThread
>
> {
>
> public:
>
> WorkerThread(QGraphicsScene *scene = 0);
>
> void startThread();
>
>
> protected:
>
> void run();
>
>
> private :
>
> QGraphicsScene *scene;
>
> };
>
>
> #endif // WORKERTHREAD_H
>
>
>
>
>
> ----------------------
>
> WorkerThread.cpp :
>
> ---------------------
>
>
>
> #include "workerthread.h"
>
>
> WorkerThread::WorkerThread(QGraphicsScene *scene)
>
> {
>
> this->scene = scene;
>
> }
>
>
> void WorkerThread::run()
>
> {
>
> QList<QGraphicsItem*> items = scene->items();
>
> items.at(0)->setPos(100, 250);
>
> items.at(1)->setPos(200, 250);
>
> items.at(2)->setPos(300, 250);
>
>
> forever
>
> {
>
> for(int i = 0;i < items.count();i++)
>
> {
>
> items.at(i)->moveBy(0, -10);
>
> sleep(1);
>
> scene->update(0, 0, 351, 271);
>
> }
>
> }
>
> }
>
>
> void WorkerThread::startThread()
>
> {
>
> start();
>
> }
>
>
>
>
>
> ----------------
>
> MainWindow.cpp
>
> -------------
>
>
> #include "mainwindow.h"
> #include "ui_mainwindow.h"
>
>
>
> MainWindow::MainWindow(QWidget *parent)
>
> : QMainWindow(parent), ui(new Ui::MainWindow)
>
> {
>
> ui->setupUi(this);
>
> this->scene = new QGraphicsScene();
>
> scene->setSceneRect(0, 0, 351, 271);
>
> ui->graphicsView->setScene(scene);
>
> thread = new WorkerThread(scene);
>
> }
>
>
> MainWindow::~MainWindow()
>
> {
>
> delete ui;
>
> }
>
>
> void MainWindow::on_startBtn_clicked()
>
> {
>
> scene->addSimpleText("test1");
>
> scene->addSimpleText("test2");
>
> scene->addSimpleText("test3");
>
> thread->startThread();
>
> }
>
>
>
>
>
> -------------------------------------------------------------
>
> I have QGraphicsView and a button in my .ui .
>
> As you can see I'm modifying the items from another thread. QGraphicsSene
> is not derived from QWidget, therefore it can be modified from worker
> threads. The timer error is rising, though...
>
>
>
>
> Regards,
>
> Patric
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> ----- Original Message -----
> From: "Sean Harmer" <sean.harmer at maps-technology.com>
> To: <qt-interest at trolltech.com>
> Sent: Saturday, August 29, 2009 11:51 AM
> Subject: Re: [Qt-interest] QGraphicsScene problem
>
>
>> Hi,
>>
>> On Saturday 29 Aug 2009 09:40:14 Patric wrote:
>>> I don't have any timers.
>>> But I have found that when the item's moveBy() functions is called, the
>>> warning is given.
>> What thread does your scene and items have affinity with? Main or worker?
>>
>> Personally I would just get your worker thread to emit signals that tell
>> the
>> items where to move to rather than having the worker thread move the
>> items
>> directly which is what it sounds like you are doing form the limited
>> information.
>>
>> This keeps your game logic code in the worker thread but keeps the GUI
>> stuff
>> in the main thread where it has to live.
>>
>> Can you post some example code that reproduces the problem?
>>
>> Have you tried doing this without using threads?
>>
>> Sean
>>
>>>
>>> The scene is in the main thread, the worker thread possesses a pointer
>>> to
>>> the scene. And the worker thread is trying to move the item's in the
>>> scene
>>> around. ----- Original Message -----
>>> From: Scott Aron Bloom
>>> To: qt-interest at trolltech.com
>>> Sent: Saturday, August 29, 2009 11:21 AM
>>> Subject: Re: [Qt-interest] QGraphicsScene problem
>>>
>>>
>>> The timer is owned/constructed in one theread, and is being
>>> started/stoped by the worker thread.
>>>
>>>
>>>
>>> Try calling moveToThread to move the timers into the worker thread.
>>>
>>>
>>>
>>> Scott
>>>
>>>
>>>
>>> From: qt-interest-bounces at trolltech.com
>>> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Patric Sent:
>>> Saturday, August 29, 2009 1:11 AM
>>> To: qt-interest at trolltech.com
>>> Subject: [Qt-interest] QGraphicsScene problem
>>>
>>>
>>>
>>> Hello guys,
>>>
>>> I have QGraphicsScene here with items on it.
>>>
>>>
>>>
>>> I have manual control (i.e. keyboard events) and also auto control
>>> (through a thread which makes all the calculations where these
>>> QPixmapItems
>>> should interact).
>>>
>>>
>>>
>>> When I control them, everything is OK.
>>>
>>> But when I start the auto control, i.e. my thread which simulates the
>>> actions, I'm observing a very strange behaviour. It's a game actually,
>>> it's
>>> turn based.
>>>
>>> So somethimes the objects on the board are just not moving on their
>>> turns.
>>>
>>>
>>>
>>> I have implemented this simulation engine with the technique used in
>>> the
>>> Mandelbrot example.
>>>
>>>
>>>
>>> In my MainWindow class I have instance of the engine thread, and when
>>> some button in the GUI is pressed MainWindow calls a method in the
>>> thread
>>> (directly), which wakes the thread.
>>>
>>> And I'm getting this shit :
>>>
>>>
>>>
>>> QObject::killTimer: timers cannot be stopped from another thread
>>>
>>> QObject::startTimer: timers cannot be started from another thread
>>>
>>> QObject::killTimer: timers cannot be stopped from another thread
>>>
>>> QObject::startTimer: timers cannot be started from another thread
>>>
>>> QObject::killTimer: timers cannot be stopped from another thread
>>>
>>> QObject::startTimer: timers cannot be started from another thread
>>>
>>>
>>>
>>> Any ideas why ?
>>>
>>>
>>>
>>> Regards,
>>>
>>> Patric
>>>
>>>
>>>
>>> p.s.:in the end of the thread's run method there is sleep call also.
>>>
>>>
>>>
>>> ---------------------------------------------------------------------------
>>>---
>>>
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>
>
More information about the Qt-interest-old
mailing list