Results 1 to 11 of 11

Thread: signal of QListWidget doesn't work

  1. #1
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question signal of QListWidget doesn't work

    Hello,

    First of all forgive me my bad English and please keep in mind I'm a complete newbie to C++. I did some coding for a while in 'Gambas', that's like Visual Basic but for Linux.

    Up to now it goes fine with Qt but I do have one problem I can't solve ...
    The GUI of my first program is made with designer and has amongst several buttons and lineEdits also a ListWidget.

    This ListWidget does not emit signals, all other widgets do, they work fine.

    Here's some code I use to emit the signal:
    connect(listWidget, SIGNAL(itemActivated(QListWidgetItem * item)), this, SLOT(enableDeleteButton()));

    Signal itemClicked() doesn't work neither....

    As the function enableDeleteButton() says it will enable a button I can use to remove items from the ListWidget.

    Anyone an idea what I'm doing wrong?

    Thanks.

    Vito

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: signal of QListWidget doesn't work

    You must not put parameter names to the connect statement. It should be:
    Qt Code:
    1. connect(listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(enableDeleteButton()));
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: signal of QListWidget doesn't work

    Quote Originally Posted by jpn View Post
    You must not put parameter names to the connect statement. It should be:
    Qt Code:
    1. connect(listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(enableDeleteButton()));
    To copy to clipboard, switch view to plain text mode 
    Yes, this was the solution, thanks ... but how do I know what item is selected for deletion if I can't put it in a parameter? Or are there other ways?

    Thank you for your help.

    Vito

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: signal of QListWidget doesn't work

    you can use
    Qt Code:
    1. QListWidgetItem * QListWidget::currentItem () const
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: signal of QListWidget doesn't work

    Quote Originally Posted by vito49 View Post
    Yes, this was the solution, thanks ... but how do I know what item is selected for deletion if I can't put it in a parameter? Or are there other ways?

    Thank you for your help.

    Vito
    you can use the following syntax:
    Qt Code:
    1. connect(listWidget, SIGNAL(itemActivated(QListWidgetItem *)), this, SLOT(enableDeleteButton(QListWidgetItem *)));
    To copy to clipboard, switch view to plain text mode 

    This way, in the enableDeleteButton() function, u can access the current item from the argument.

    An alternate solution is to have the button you want to enable accessible in the enableDeleteButton() function and use currentItem () to get the current item.
    To make the button accesible, declare the button in the class itself (as a private variable), then you'll be able to access it in the enableDeleteButton() function.

  6. #6
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: signal of QListWidget doesn't work

    Quote Originally Posted by montylee View Post
    you can use the following syntax:
    Qt Code:
    1. connect(listWidget, SIGNAL(itemActivated(QListWidgetItem *)), this, SLOT(enableDeleteButton(QListWidgetItem *)));
    To copy to clipboard, switch view to plain text mode 

    This way, in the enableDeleteButton() function, u can access the current item from the argument.

    An alternate solution is to have the button you want to enable accessible in the enableDeleteButton() function and use currentItem () to get the current item.
    To make the button accesible, declare the button in the class itself (as a private variable), then you'll be able to access it in the enableDeleteButton() function.
    I can't figure it out .. well, I'm new to C++ and Qt

    I changed a few things ...

    Qt Code:
    1. connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(delete_Item(QListWidgetItem*)));
    2.  
    3. void myQtApp::delete_Item(QListWidgetItem* item)
    4. {
    5. QMessageBox msgDelete;
    6. msgDelete.setText("item is going to be removed");
    7. msgDelete.setStandardButtons(QMessageBox::Yes);
    8. int msg=msgDelete.exec();
    9.  
    10. listWidget->removeItemWidget(item);
    11. }
    To copy to clipboard, switch view to plain text mode 

    But the item is not removed ... probably I'm using the wrong code..
    I read that QListWidget is read-only by default, I can't find out if that's true or how to change that if that's the case.

    Don't mind the MessageBox in the function, I still have to implement a second button so I can choose to delete or to leave the item. The delete button I previously used is no longer valid.

    Thanks for your help.
    Last edited by jpn; 30th September 2008 at 16:39. Reason: missing [code] tags
    Vito
    from Flanders/Belgium

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: signal of QListWidget doesn't work

    Item widgets are widgets laid on top of items. QListWidget::removeItemWidget() removes an item widget, but leaves the actual item intact. Just delete the item to remove it from the list.
    J-P Nurmi

  8. #8
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Re: signal of QListWidget doesn't work

    Quote Originally Posted by jpn View Post
    Just delete the item to remove it from the list.
    That's my problem ... I can't find any member of QListWidget that removes/deletes an item. Unless I don't understand the English text of course ... sorry for that.
    Vito
    from Flanders/Belgium

  9. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: signal of QListWidget doesn't work

    Use the C++ delete operator:
    Qt Code:
    1. delete item;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  10. #10
    Join Date
    Sep 2008
    Location
    Flanders/Belgium
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Re: signal of QListWidget doesn't work

    Quote Originally Posted by jpn View Post
    Use the C++ delete operator:
    Qt Code:
    1. delete item;
    To copy to clipboard, switch view to plain text mode 
    Yep, that was it ... as easy as that.
    Well I do have to learn a lot, but I'm a good student (I think)

    Many thanks for your help.
    Vito
    from Flanders/Belgium

  11. #11
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: signal of QListWidget doesn't work

    Before delete, you can delete the item by using the following API:

    Qt Code:
    1. QListWidgetItem *QListWidget::takeItem ( int row )
    To copy to clipboard, switch view to plain text mode 

    This API actually removes the item from the list widget and returns the item. After calling this API, you should call delete on the item which was returned by the above API.

Similar Threads

  1. QListWidget SIGNAL Problem
    By skuda in forum Qt Programming
    Replies: 19
    Last Post: 28th October 2009, 14:42
  2. QListWidget clicked signal
    By asieriko in forum Qt Programming
    Replies: 12
    Last Post: 10th August 2007, 15:37
  3. Replies: 3
    Last Post: 15th April 2007, 19:16
  4. Replies: 13
    Last Post: 15th December 2006, 11:52

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.