Results 1 to 7 of 7

Thread: QListWidget signal problem

  1. #1
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QListWidget signal problem

    Hello,

    I am trying to create a signal on a QListWidget on item click. For some reason the signal does not seem to be working. Code is below. I think it may have to do with my setItemWidget call, not sure if this blocks signals for the QListWidgetItem.

    Edit: I just checked and connect is returning false, not sure what this problem is. And this is with the "ui.listWidget->setItemWidget(item, ne);" line commented out. So that is not causing the problem.

    Thanks for the help.

    Qt Code:
    1. for (int i = 0; i < 20; i++) {
    2. QListWidgetItem * item = new QListWidgetItem("123456789", ui.listWidget);
    3. item->setSizeHint(QSize(200,75));
    4. NameEntry * ne = new NameEntry("John", "NU", QPixmap(":/fb/images/1604859.jpg"), "123456789" );
    5. //connect(ui.listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(changeDate()));
    6. ui.listWidget->setItemWidget(item, ne);
    7. connect(ui.listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(changeDate()));
    8. }
    9. //... snip
    10.  
    11. void changeDate()
    12. {
    13. ui.lblDate->setText("Working...");
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rishid; 18th January 2008 at 18:35.

  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: QListWidget signal problem

    Hi,

    First of all, you should connect the signal only once. Secondly, items won't get clicked at all because item widgets are placed on top of them.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget signal problem

    Even if I change the code to just this it still is not working.

    Qt Code:
    1. for (int i = 0; i < 10; i++) {
    2. QListWidgetItem * item = new QListWidgetItem("123456789");
    3. ui.listWidget->addItem ( item );
    4. if (connect( ui.listWidget, SIGNAL( itemClicked (QListWidgetItem *) ), SLOT( changeDate() ) ) )
    5. ui.lblDate->setText("connect is good");
    6. }
    7.  
    8. //.. snip
    9. void changeDate()
    10. {
    11. ui.lblDate->setText("It works");
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: QListWidget signal problem

    You're still connecting the signal 10 times. Do you want changeDate() to get called 10 times when any item is clicked? Does ui.lblDate contain text "connect is good"? Notice that QObject::connect() prints a detailed warning to debug output when the connection fails. Is changeDate() actually a slot of "this"? Is it declared as a slot? Does the class declaration contain required Q_OBJECT macro?
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    rishid (18th January 2008)

  6. #5
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget signal problem

    I moved the connect outside the for loop, dind't realize it dind't have to be called each time I added an item, I was thinking it was item specific.

    But anyways, I also realized the changeDate() method was not declared a slot. I was using some code as an example and did not catch this.

    Now as you stated above, using the setItemWidget will break the itemClicked signal which it does. So the only other way to recreate this signal/slot mechanism is to use an event on the widget itself?

    Thanks for the help.
    Last edited by rishid; 18th January 2008 at 19:55.

  7. #6
    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: QListWidget signal problem

    You can always make the widget emit similar signal. But you shouldn't flood the view full of item widgets. The whole point of item views is to get rid of expensive widgets and replace them with light items. Just add the widgets to a layout and place it inside a QScrollArea if you really need to use widgets.
    J-P Nurmi

  8. #7
    Join Date
    Jan 2008
    Posts
    27
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget signal problem

    The problem I had with using widgets, layout box and a scroll area is that I would not be able to dynamically add and remove widgets from the layout box without recreating the entire thing each time. Problem was when I added a widget to the layout box, the rest of the widgets would become smaller. This aspect fixable somehow? If so then it would be perfect.

    Thanks for all your help and time.

Similar Threads

  1. QLineEdit - lostFocus signal problem
    By Enygma in forum Qt Programming
    Replies: 6
    Last Post: 17th June 2010, 20:52
  2. QListWidget SIGNAL Problem
    By skuda in forum Qt Programming
    Replies: 19
    Last Post: 28th October 2009, 14:42
  3. QListWidget Problem
    By pmabie in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2007, 06:52
  4. Problem emitting signal from a static function
    By Valheru in forum Qt Programming
    Replies: 21
    Last Post: 12th June 2007, 14:48
  5. Replies: 3
    Last Post: 15th April 2007, 19: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.