Results 1 to 15 of 15

Thread: Stupid list widget won't clear

  1. #1
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Stupid list widget won't clear

    When I say MyListWidget->clear() it crashes if there's an item selected (if it's fresh and an item hasn't been clicked yet it works).

    When I create a command that goes MyListWidget->takeItem(0) it removes the top item (like it should) and then when there's one item left it crashes when it tries to remove it.

    If I don't select anything in the listwidget at all during the course of the program, the command is able to remove the last item safely.

    What's going on here? I think clear() worked in my program a short time ago, and now it seems to have this strange issue. How do I fix this issue and make the listwidget clearable?
    Last edited by scwizard; 24th March 2007 at 21:11.

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

    Default Re: Stupid list widget won't clear

    Are you by any chance using a Qt version lower than 4.2?

  3. #3
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Stupid list widget won't clear

    Nope, I'm not. Going into the all programs menu I see Qt 4.2.2 (open source edition) by Trolltech.

  4. #4
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: Stupid list widget won't clear

    Could you give us some code? Could it be that you delete your item in the QListWidget somewhere else illegally?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Stupid list widget won't clear

    Could you post the backtrace?

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

    Default Re: Stupid list widget won't clear

    If the thing happens when you delete the last item only, then I was experiencing a simmilar issue some time ago. Do you try to delete that item from within a slot? If so, what is the slot connected to?

  7. #7
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Stupid list widget won't clear

    Quote Originally Posted by wysota View Post
    If the thing happens when you delete the last item only, then I was experiencing a simmilar issue some time ago. Do you try to delete that item from within a slot? If so, what is the slot connected to?
    I seems we're getting somewhere now.

    The slot is connected to MainWindow. The signal is emitted from a keyboard shortcut.

    The hierarchy goes:
    MainWindow->Sidebar->MyListWidget

    The function is:
    Qt Code:
    1. void MainWindow::NewFile() {
    2. Sidebar->MyListWidget->clear();
    3. // Or a while loop with the following:
    4. // Sidebar->MyListWidget->takeItem(0);
    5. return;
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Stupid list widget won't clear

    Does the application still crash if you implement it as follows:
    Qt Code:
    1. void MainWindow::NewFile(){
    2. QTimer::singleShot(0, Sidebar->MyListWidget, SLOT(clear()));
    3. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Stupid list widget won't clear

    Yes it does

    It doesn't change anything. Still crashes when something has been selected and works when nothing has.

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

    Default Re: Stupid list widget won't clear

    Then it means it's not because of clear() but because of some other signal from the list widget being connected. It probably calls a slot that takes a QListWidgetItem pointer and it doesn't check for a null value and that causes a crash.

    In my situation (Qt bug, AFAIR) the trick above helped. If it didn't help you, the app has to be crashing because of a null pointer in one of the slots. You can try debugging as Jacek suggested, but first I'd check the signals connected.

  11. The following user says thank you to wysota for this useful post:

    scwizard (24th March 2007)

  12. #11
    Join Date
    Feb 2007
    Posts
    29
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Stupid list widget won't clear

    Very good work!

    I have a signal being emitted upon items changing and forgot to take into account that the item could change without the user changing it.

  13. #12
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Stupid list widget won't clear

    I faced Same problem, actual problem in currentRowChanged() signal's slot

    I have taken one slot for currentRowChanged() signal of listwidget there i am trying to access element like following code, if we clear the list that time also that slot will be get called,so memory crash occurs,

    My old code,

    connect(myistWidget,SIGNAL(currentRowChanged(int)) ,this,SLOT(changeLabel(int)));

    void QZugNummerDlgAction::changeLabel(int position)
    {
    QString labelText = myistWidget.at(position);//if we clear list memory crash occurs heremyLabel->setText(labelText);
    }

    New Code, i put the following condition it works properly,

    void QZugNummerDlgAction::changeLabel(int position)
    {
    if(myistWidget->currentRow()!=-1)
    {
    QString labelText = myistWidget.at(position);
    myLabel->setText(labelText);
    }
    }


    Thanks,
    Rajesh.S

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Stupid list widget won't clear

    Quote Originally Posted by rajeshs View Post
    QString labelText = myistWidget.at(position);//if we clear list memory crash occurs here
    This shouldn't even compile, as "myistWidget" is a pointer.

    What is that "at" method? QListWidget doesn't have such method.

  15. #14
    Join Date
    Jun 2007
    Location
    India/Bangalore
    Posts
    156
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Stupid list widget won't clear

    Sorry ,one mistake in my code

    That is not listwidget, there i am taking one corresponding
    Qlist so if we access -1 position from QList crash occurs,

    that line is myqlist->at(position).

    Thanks,
    Rajesh.S

  16. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Stupid list widget won't clear

    Quote Originally Posted by rajeshs View Post
    That is not listwidget, there i am taking one corresponding
    Qlist so if we access -1 position from QList crash occurs,
    OK, that explains everything. Qt docs warn about accessing inexistent items.

Similar Threads

  1. Replies: 8
    Last Post: 1st October 2015, 07:23
  2. Controlling which widget on top layer?
    By JonathanForQT4 in forum Qt Programming
    Replies: 6
    Last Post: 22nd March 2007, 14:27
  3. Replies: 4
    Last Post: 10th March 2007, 18:01
  4. Custom widget in list view
    By fusoin23 in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 14:09
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.