Results 1 to 9 of 9

Thread: QListWidget QObject sender

  1. #1
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidget QObject sender

    I have a list, that has multiple widgets in it (QListWidget with TaskItem's in it). The Task Item can produce a certain signal. How can i find out which one send it? I know I have to use QObject::sender, but I had no luck trying to connect(..., ..., ..., ...) with it... Can anyone, please, answer - what should i do?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListWidget QObject sender

    So, what is exactly is your problem
    1. You are not able to make connect() work ?
    or
    2. You are not able to know which QListWidgetItem has sent the signal ?

    byt the way what you mean by "TaskItem"

  3. #3
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget QObject sender

    TaskItem is a QWidget, that I'm setting on the QListWidget using
    Qt Code:
    1. list->setItemWidget(list->item(0), new TaskItem);
    To copy to clipboard, switch view to plain text mode 
    I don't know HOW to write the connect BECAUSE I don't know, how to point out which list item sent the signal...


    Added after 16 minutes:


    I think I am a little bit closer to the truth:
    When the pushButton in taskitem get's clicked, it emits a signal
    Qt Code:
    1. deleteTaskItem();
    To copy to clipboard, switch view to plain text mode 
    I can catch it in the class where My list is by using:
    Qt Code:
    1. connect(item, SIGNAL(deleteTaskItem()), this, SLOT(deleteTaskItem()));
    To copy to clipboard, switch view to plain text mode 
    Then in the slot I have to use something like QObject* QObject::sender();
    But I can't figure out what to do with it...


    Added after 11 minutes:


    I tried to Write in the slot something like this:
    Qt Code:
    1. void TaskMain::deleteTaskItem()
    2. {
    3. list->removeItemWidget(sender());
    4. }
    To copy to clipboard, switch view to plain text mode 
    But I need to give as a parameter QListWidgetItem*, and not a QObject*
    Last edited by Archa4; 20th May 2011 at 07:40.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QListWidget QObject sender

    Quote Originally Posted by Archa4 View Post
    TaskItem is a QWidget, that I'm setting on the QListWidget using
    Qt Code:
    1. list->setItemWidget(list->item(0), new TaskItem);
    To copy to clipboard, switch view to plain text mode 
    I don't know HOW to write the connect BECAUSE I don't know, how to point out which list item sent the signal...
    If you need a pointer to the TaskItem in order to connect it then why don't you just make that explicit. What is wrong with:
    Qt Code:
    1. TaskItem *ti = new TaskItem(this);
    2. connect(ti, SIGNAL(stuffHappened()), somethingElse, SLOT(interestedInStuff()));
    3. list->setItemWidget(list->item(0), ti);
    To copy to clipboard, switch view to plain text mode 

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

    Archa4 (20th May 2011)

  6. #5
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget QObject sender

    Quote Originally Posted by Archa4 View Post
    But I need to give as a parameter QListWidgetItem*, and not a QObject*
    you can use qobject_cast...

    Qt Code:
    1. if(QListWidgetItem* item = qobject_cast<QListWidgetItem*>(sender()))
    2. {
    3. // do something with item
    4. }
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to FelixB for this useful post:

    Archa4 (20th May 2011)

  8. #6
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget QObject sender

    Thanks, and I managed to figure this on my own just secs before this posts

    Qt Code:
    1. void TaskMain::deleteTaskItem()
    2. {
    3. item = new TaskItem;
    4. TaskItem *item2 = new TaskItem;
    5. item = qobject_cast<TaskItem*>(sender());
    6. for(int i=0; i<list->count(); i++)
    7. {
    8. item2 = qobject_cast<TaskItem*>(list->itemWidget(list->item(i)));
    9. if (item2==item)
    10. delete list->item(i);
    11. }
    12. count--;
    13. if (list->count()==0)
    14. addItem();
    15. }
    To copy to clipboard, switch view to plain text mode 

  9. #7
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget QObject sender

    why do you create new objects on the heap? where do you delete them?

    Qt Code:
    1. item = new TaskItem;
    2. TaskItem *item2 = new TaskItem
    To copy to clipboard, switch view to plain text mode 
    ;

  10. #8
    Join Date
    Feb 2011
    Location
    Latvia
    Posts
    139
    Thanks
    24
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget QObject sender

    Ok, then how should I solve this?

  11. #9
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidget QObject sender

    I can't tell you how to solve "this" because I don't see any reason why you create these objects.

Similar Threads

  1. QObject::sender() in a Q_PRIVATE_SLOT
    By vfernandez in forum Qt Programming
    Replies: 13
    Last Post: 17th June 2010, 19:57
  2. problem in sender()
    By wagmare in forum Qt Programming
    Replies: 13
    Last Post: 15th July 2009, 10:04
  3. [solved] Which object type is QObject::sender()?
    By ricardo in forum Qt Programming
    Replies: 6
    Last Post: 8th May 2009, 21:03
  4. tip on using QObject::sender()
    By drhex in forum Qt Programming
    Replies: 1
    Last Post: 3rd April 2008, 20:01
  5. How to add an QObject to QListWidget
    By steg90 in forum Newbie
    Replies: 7
    Last Post: 14th May 2007, 15:53

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.