Results 1 to 15 of 15

Thread: QGraphicsView not being updated

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QGraphicsView not being updated

    Hi! I've been noticing this strange behavior and I really can't understand what could it be related to.
    I have a worker thread which loads some images in a loop and sends a signal in each iteration. This signal is connected to a slot in the GUI thread which simply shows these images in a QGraphicsView.

    This structure has always been working very good, but now I tried it on another system (both are Windows CE), and it seems its not working properly. When I start the application, the images coming from the thread are simply not rendered in the QGraphicsView. They are loaded correctly in the background, but not rendered. After I click anywhere, those which are already loaded appears suddenly, and the others begin to appear correctly as soon as those are loaded.

    After this process has finished, everything is OK. I can even start this thread again, and the images are correctly shown as expected, as soon as they are loaded. The problem is only immediately after the application is loaded.

    Any idea why this could be happening. As I've recently been taught, I thought it could be a problem of events not processed, but I tried placing QApplication:rocessEvents() after the update method without any success...
    Any other idea?

    Thanks for any advice!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not being updated

    Can we see the code? Remember you can only call QApplication::processEvents() from the main thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    This is the run method of my thread:

    Qt Code:
    1. for (int i = 0; i < filesList->count(); i++) {
    2. // Load the QImage...
    3. QImage image = imageReader.read();
    4. emit this->imageReady(image);
    5. }
    To copy to clipboard, switch view to plain text mode 

    it simply loads one after the other all the images listed. The signal is connected to a slot which calls a method which sets the image in a QGraphicsItem subclass which uses it in its paint method this way:

    Qt Code:
    1. void Class::setImageLoaded(QImage image) {
    2. // Store the image in the QGraphicsItem
    3. this->currentImage = image;
    4. // Force repainting of this object.
    5. this->update(this->boundingRect());
    6. }
    To copy to clipboard, switch view to plain text mode 

    and the paint method draws the image with drawImage of this->currentImage.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not being updated

    How does the connect statement look like? What is "Class", what is its superclass?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    The connect statement is this:

    Qt Code:
    1. connect(loader, SIGNAL(imageReady(QImage)), this, SLOT(imageGenerated(QImage)));
    To copy to clipboard, switch view to plain text mode 

    imageGenerated then calls Class::setImageLoaded(QImage image) that I reported above.
    Class is a subclass I made which is defined this way:

    Qt Code:
    1. class IconViewItem : public QObject, public QGraphicsItem {
    2. Q_OBJECT
    3. Q_PROPERTY(QPointF pos READ pos WRITE setPos)
    4. Q_INTERFACES(QGraphicsItem)
    5. public:
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not being updated

    Please provide more than one or two lines of code. It is important to see the context of a particular call. If you use some variable in a code snippet, please explain what it is, we're not trueseers.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    Sure, the methods I already posted are more or less like those are in my code. The only difference is that imageReady and the slot imageGenerated accept two parameters instead of one. I didn't report the implementation of imageGenerated which is this:

    Qt Code:
    1. void Class2::imageGenerated(QImage image, QString string) {
    2. Class* item = itemsMap.value(string);
    3. item->setImageLoaded(image);
    4. }
    To copy to clipboard, switch view to plain text mode 

    where Class2 is a subclass of QGraphicsScene and itemsMap is a QMap which maps strings to objects of class Class. Sorry I made a mistake in the other post, in fact IconViewItem was Class.
    This method I reported calls the method setImageLoaded of class Class that I reported above (it was complete, it only lacked another assignation of image, no more lines).

    Of course the connect statement was:

    Qt Code:
    1. connect(loader, SIGNAL(imageReady(QImage, QString)), this, SLOT(imageGenerated(QImage, QString)));
    To copy to clipboard, switch view to plain text mode 

    The code I reported for the worker thread was complete, I only removed some lines where I set the size I want for imageReader before loading (I report in case it's relevant):

    Qt Code:
    1. QImageReader imageReader(dir->absoluteFilePath(filesList->at(i)));
    2. if (imageReader.size().height() >= imageReader.size().width())
    3. imageReader.setScaledSize(QSize(imageReader.size().width()*64/imageReader.size().height(), 64));
    4. else imageReader.setScaledSize(QSize(64, imageReader.size().height()*64/imageReader.size().width()));
    To copy to clipboard, switch view to plain text mode 

    where dir and filesList are local pointers to QDir and QStringList respectively.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not being updated

    So "loader" is an instance of a subclass of QThread?

    What does the program do while the thread is working? Are you able to resize windows of the application, scroll the view, etc.?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    Sorry, forgot to specify loader. loader is a subclass of QThread whose run() method I already reported.
    The program works correctly except immediately after it is loaded. Subsequent loaders work correctly. I create it this way:

    loader = new ThumbnailLoader(dir, filesList);

    where dir and filesList are those I reported.
    And I start it in a method this way:

    Qt Code:
    1. delete loader->dir;
    2. delete loader->filesList;
    3. loader->dir = dir;
    4. loader->filesList = imagesList;
    5. loader->start(QThread::LowestPriority);
    To copy to clipboard, switch view to plain text mode 

    The reason why I delete and then reassign is that this method is called many times when the user requests (parameters in the constructor were absolutely unuseful). It works correctly every time except the first which is right after the program loads. After the first call, while the loader works, I can do whatever I want, like scrolling the QGraphicsView, stopping it, etc... The first time instead, it works, but the QGraphicsView is simply not showing anything until I click anywhere on the application. After I click, the images already processed are suddenly rendered and the QGraphicsView starts to show them as soon as they are loaded (which is the expected behavior, and the behavior the program has in some systems, always Windows CE). I hope I've been able to make the behavior clear.

    EDIT: I forgot to mention that the above portion of code is placed in a method which is called, the first time (which is the only case in which sometimes it fails to update correctly), in the constructor of a subclass of a QGraphicsScene (the same which has to render the images).

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not being updated

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    This is a compilable example showing the issue I'm talking about. It doesn't happen always, but quite often. Tried this on Windows Vista and Windows CE.

    Under Windows Vista seems to work always correctly. Under Windows CE, sometimes it shows the images as soon as they are loaded, sometimes it is necessary to click in some place before the application shows the images.

    In this example I didn't add an update explicitly, in my previous application there was an explicit call to the update method, but it seems this is not needed, at least not under Windows Vista.

    If you need to change the path where the images are located and the filter to test it, change whatever you need in workerthread.cpp.

    Thanks for any advice!
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    Isn't anyone able to reproduce this issue?

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView not being updated

    You can't manipulate QPixmaps in worker threads, neither can they be transfered across threads (use QImage instead). Also remember the WorkerThread object doesn't live in the thread it represents. See if it all helps.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    You're absolutely right. My bad. I will correct this mistake. I used QPixmaps instead of using QImages. But I did this only in the example. In my code I originally used QImages. Are you able to reproduce this situation anyway?
    Thanks for your answers!

  15. #15
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QGraphicsView not being updated

    I made some more investigation on this, and found out that the method in my GUI thread that should have painted the images on the view was not executed until the user clicked. I therefore changed the type of connection to Qt:irectConnection. Unfortunately, now the method is called immediately, but still the QGraphicsView is not updated anyway until the user clicks.
    Any other idea?
    Thanks!

Similar Threads

  1. Layout not updated when sizeHint changes
    By ArmanS in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2010, 14:31
  2. QTextEdit not being updated
    By doggrant in forum Qt Programming
    Replies: 3
    Last Post: 2nd September 2009, 17:25
  3. elements not updated
    By danesh in forum Qt Programming
    Replies: 2
    Last Post: 2nd April 2009, 08:35
  4. HaiQ updated for Qt 4.4
    By magland in forum Qt-based Software
    Replies: 8
    Last Post: 19th September 2008, 11:42
  5. QHash only getting updated not populated
    By quickNitin in forum Newbie
    Replies: 2
    Last Post: 13th July 2006, 11:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.