Results 1 to 5 of 5

Thread: Help! How to implement CHECK & RETURN in signal/slots

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Red face Help! How to implement CHECK & RETURN in signal/slots

    I am using Qt to develop a really big software, but I got some trobule in resolving UI-Frozen-Issue in Qt framework.

    For example, I have a widget named "Picture wall", which lists all the pictures in one directory, with each picture represented by a icon and a label.

    After user click the icon or label, a signal will be issued and the slot on_picture_wall_item_clicked will be launched, then the picture clicked will be loaded to canvas.

    If user click the items in picture wall one by one very fast, then the UI will be frozen, and the pictures clicked by user will be loaded one by one.

    Of course I want the slot to CHECK whether this slot is busy (loading some other picture), if it's busy, the slot must return immediately.

    My problems is that the "CHECK & RETURN" doesn't work at all. See my codes below:

    Qt Code:
    1. void MusePictureWallWidget::on_picture_wall_item_clicked(const QString& fileName)
    2. {
    3. if(!m_mutex.tryLock())
    4. return;
    5.  
    6. if(m_busy)
    7. return;
    8.  
    9. m_busy=true;
    10.  
    11. this->setCursor(Qt::WaitCursor);
    12.  
    13. disconnect(ui.listViewPhotoWall, SIGNAL(pictureDoubleChecked(const QString &)),this, SLOT(on_picture_wall_item_clicked(const QString&)));
    14.  
    15. this->setEnabled(false);
    16. ui.listViewPhotoWall->setEnabled(false);
    17. this->repaint();
    18.  
    19. QDialog dlg;
    20. dlg.setModal(true);
    21. dlg.show();
    22.  
    23. loadPicture(fileName);
    24.  
    25. dlg.hide();
    26.  
    27. m_busy=false;
    28.  
    29. ui.listViewPhotoWall->setEnabled(true);
    30. this->setEnabled(true);
    31.  
    32. connect(ui.listViewPhotoWall, SIGNAL(pictureDoubleChecked(const QString &)),this, SLOT(on_picture_wall_item_clicked(const QString&)), Qt::DirectConnection);
    33.  
    34. this->unsetCursor();
    35.  
    36. m_mutex.unlock();
    37. }
    To copy to clipboard, switch view to plain text mode 

    I tried 5 ways to implement the "CHECK & RETURN" in my slots.

    1, QMutex, I used a mutex to lock and lock the slot, if it's occupied, the slot call should return at once.
    2, An flag m_busy, the slot should then check the flag, if m_busy is true, it should return at once.
    3, When a slot is launched, I disconnected this current signal/slot and reconnect them after the loading is done.
    4, I raise a Modal Dialog when the slot is launched, so that the picture wall will not be clickable until the loading is done and the dialog is hiden.
    5, I disable the picture wall after one slot is launched, and then enable it after loading is done.

    BUT......all these did not work!!! What's wrong with my codes? How can I fix this problem? Help! Help!
    Last edited by apollocheun; 23rd July 2010 at 09:06.

  2. #2
    Join Date
    Jul 2008
    Posts
    416
    Thanks
    9
    Thanked 54 Times in 52 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help! How to implement CHECK & RETURN in signal/slots

    Hi, I think at least the mutex approach should work, but you have to be more specific on how "it does not work". For example, does the tryLock() call fail sometimes?

    On another note, I would expect the program to display the item I clicked last, and not the one it is still working on. So in my opinion it would be better to abort a running loading operation at restart with the newly clicked item.

    Ginsengelf

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help! How to implement CHECK & RETURN in signal/slots

    Quote Originally Posted by Ginsengelf View Post
    Hi, I think at least the mutex approach should work, but you have to be more specific on how "it does not work". For example, does the tryLock() call fail sometimes?

    On another note, I would expect the program to display the item I clicked last, and not the one it is still working on. So in my opinion it would be better to abort a running loading operation at restart with the newly clicked item.

    Ginsengelf
    Thank you Ginsengelf, you are right. I also expect the program to display the last clicked, but terminating a loading process is not easy and not safe sometime.

    I guess, QObject won't issue another signal until the first signal has been proceeded? Am I right?

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

    Default Re: Help! How to implement CHECK & RETURN in signal/slots

    I would suggest to read this article: [wiki]Keeping the GUI Responsive[/wiki]. QtConcurrent seems to be a perfect solution for your problem.

    Your mutex approach is useless because only one thread tries to access the mutex so it will never block on it. It's much better to do the work in another thread and only when the picture is ready, display it on your wall. It then doesn't even matter if the user clicks 1 image or 100, because they will be loaded one after the other and all without blocking the UI. It's just a matter of which one you display.
    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
    Jul 2008
    Posts
    416
    Thanks
    9
    Thanked 54 Times in 52 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Help! How to implement CHECK & RETURN in signal/slots

    Quote Originally Posted by wysota View Post
    Your mutex approach is useless because only one thread tries to access the mutex so it will never block on it.
    You're right of course. At the moment I see threads lurking at every corner...

Similar Threads

  1. QWebpage always return false for signal loadfinished
    By QtVenkat in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2010, 21:50
  2. how to check if a signal in emited or not??
    By sudhansu in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 09:51
  3. How to Implement signal and slots for QAccessibleWidget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2009, 07:57
  4. check box and signal
    By mickey in forum Newbie
    Replies: 7
    Last Post: 10th November 2007, 15:21
  5. Replies: 12
    Last Post: 14th June 2006, 10:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.