PDA

View Full Version : QListWidget QObject sender



Archa4
19th May 2011, 14:58
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?

Santosh Reddy
19th May 2011, 15:44
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"

Archa4
20th May 2011, 07:40
TaskItem is a QWidget, that I'm setting on the QListWidget using

list->setItemWidget(list->item(0), new TaskItem);
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

deleteTaskItem();
I can catch it in the class where My list is by using:

connect(item, SIGNAL(deleteTaskItem()), this, SLOT(deleteTaskItem()));
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:

void TaskMain::deleteTaskItem()
{
list->removeItemWidget(sender());
}
But I need to give as a parameter QListWidgetItem*, and not a QObject*

ChrisW67
20th May 2011, 08:10
TaskItem is a QWidget, that I'm setting on the QListWidget using

list->setItemWidget(list->item(0), new TaskItem);
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:


TaskItem *ti = new TaskItem(this);
connect(ti, SIGNAL(stuffHappened()), somethingElse, SLOT(interestedInStuff()));
list->setItemWidget(list->item(0), ti);

FelixB
20th May 2011, 08:23
But I need to give as a parameter QListWidgetItem*, and not a QObject*

you can use qobject_cast...


if(QListWidgetItem* item = qobject_cast<QListWidgetItem*>(sender()))
{
// do something with item
}

Archa4
20th May 2011, 08:40
Thanks, and I managed to figure this on my own just secs before this posts :)


void TaskMain::deleteTaskItem()
{
item = new TaskItem;
TaskItem *item2 = new TaskItem;
item = qobject_cast<TaskItem*>(sender());
for(int i=0; i<list->count(); i++)
{
item2 = qobject_cast<TaskItem*>(list->itemWidget(list->item(i)));
if (item2==item)
delete list->item(i);
}
count--;
if (list->count()==0)
addItem();
}

FelixB
20th May 2011, 09:07
why do you create new objects on the heap? where do you delete them?


item = new TaskItem;
TaskItem *item2 = new TaskItem;

Archa4
20th May 2011, 09:10
Ok, then how should I solve this?

FelixB
20th May 2011, 09:33
I can't tell you how to solve "this" because I don't see any reason why you create these objects.