Results 1 to 4 of 4

Thread: Can QListWidget's clear() be called from a slot connected with one of its signal?

  1. #1
    Join Date
    May 2008
    Location
    Missouri City, TX, USA
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Can QListWidget's clear() be called from a slot connected with one of its signal?

    Consider this code

    Qt Code:
    1. class SomeClass : public QWidget
    2. {
    3. public:
    4. SomeClass();
    5. ...
    6. private:
    7. QListWidget* _listWgt;
    8. };
    9.  
    10. SomeClass::SomeClass(QWidget* parent) : QWidget(parent)
    11. {
    12. _listWgt = new QListWidget(this);
    13. connect(_listWgt, SIGNAL(itemDoubleClicked(QListWidgetItem*), this, SLOT(onItemDoubleClicked(QListWidgetItem*));
    14. }
    15.  
    16. void SomeClass::onItemDoubleClicked(QListWidgetItem* item)
    17. {
    18. QString itemText = item->text();
    19. if(_listWgt){
    20. _listWgt->clear();
    21. }
    22. // Code to repopulate _listWgt with new list of items base on 'itemText'
    23. // follows.
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    My code is crashing in Qt as some timer event occurs on _listWgt that perhaps has information of _listWgt before it entered onItemDoubleClicked() slot, but after this slot is done, _listWgt doesn't reflect that state.

    I'm suspecting clear() as technically, it will try to delete the item on which the doubleClicked signal happened.

    Can anyone agree or confirm my suspicion?

    Thanks.
    Last edited by iunknwn; 30th September 2015 at 18:34.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can QListWidget's clear() be called from a slot connected with one of its signal?

    The cause of the crash could be anywhere in your program. Have you tried this suspicious pattern (calling clear() from a slot connected to itemDoubleClicked) in a minimal example?

    You could also try to turn the connection into a queued one and see if this changes anything:
    Qt Code:
    1. connect(_listWgt, SIGNAL(itemDoubleClicked(QListWidgetItem*), this, SLOT(onItemDoubleClicked(QListWidgetItem*), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 
    That way, the slot is only invoked after control has returned to the event loop.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can QListWidget's clear() be called from a slot connected with one of its signal?

    Well, what does the backtrace of your crash say?
    Does it still crash if you remove the call to clear()?

    Cheers,
    _

  4. #4
    Join Date
    May 2008
    Location
    Missouri City, TX, USA
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can QListWidget's clear() be called from a slot connected with one of its signal?

    Thanks for your response guys!

    The traceback in debugger was not very helpful. The real application is 20+ million lines of code, but I just extracted conceptual code. Didn't really try to create a small test program, but the issue has been resolved.

    There was a 100 ms timer that would scroll the list of items to the far right automatically after the new list of items were populated. Randomly, this feature would cause the crash. See callstack below.

    Qt Code:
    1. STACK ? 6 ? 0x64f55329 QListView::updateGeometries
    2. STACK ? 7 ? 0x64f4d3d7 QListView::indexAt
    3. STACK ? 8 ? 0x64f32568 QAbstractItemView::horizontalScrollbarValueChanged
    4. STACK ? 9 ? 0x6a5a7110 QMetaObject::activate
    5. STACK ? 10 ? 0x6504ab10 QAbstractSlider::valueChanged
    6. STACK ? 11 ? 0x64e0fdb9 QAbstractSlider::setValue
    7. STACK ? 12 ? 0x64e0fb42 QAbstractSlider::setRange
    8. STACK ? 13 ? 0x64f54c74 QListView::updateGeometries
    9. STACK ? 14 ? 0x64f303a1 QAbstractItemView::doItemsLayout
    10. STACK ? 15 ? 0x64f4a2b9 QListView::doItemsLayout
    11. STACK ? 16 ? 0x64f396ee QAbstractItemView::timerEvent
    12. STACK ? 17 ? 0x6a5aa5ec QObject::event
    13. STACK ? 18 ? 0x64b2e913 QWidget::event
    14. STACK ? 19 ? 0x64e4ac97 QFrame::event
    15. STACK ? 20 ? 0x64eba1a4 QAbstractScrollArea::event
    16. STACK ? 21 ? 0x64af1a36 QApplicationPrivate::notify_helper
    To copy to clipboard, switch view to plain text mode 

    Well, just by changing the horizontal scrollbar policy to always visible instead of automatic on the QListWidget solved this issue!

    This is on Qt 4.8.2 on Windows x64 MSVS 2012

Similar Threads

  1. Slot connected to a generic signal
    By Momergil in forum Qt Programming
    Replies: 5
    Last Post: 28th June 2014, 23:42
  2. Replies: 3
    Last Post: 29th September 2013, 12:46
  3. Replies: 11
    Last Post: 2nd February 2013, 15:39
  4. Replies: 2
    Last Post: 26th August 2011, 09:51
  5. Connected QTimer slot not being called
    By Polnareff in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2010, 16:55

Tags for this Thread

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.