PDA

View Full Version : QListWidget signal problem



rishid
18th January 2008, 19:27
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.



for (int i = 0; i < 20; i++) {
QListWidgetItem * item = new QListWidgetItem("123456789", ui.listWidget);
item->setSizeHint(QSize(200,75));
NameEntry * ne = new NameEntry("John", "NU", QPixmap(":/fb/images/1604859.jpg"), "123456789" );
//connect(ui.listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(changeDate()));
ui.listWidget->setItemWidget(item, ne);
connect(ui.listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(changeDate()));
}
//... snip

void changeDate()
{
ui.lblDate->setText("Working...");
}

jpn
18th January 2008, 19:39
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.

rishid
18th January 2008, 19:54
Even if I change the code to just this it still is not working.



for (int i = 0; i < 10; i++) {
QListWidgetItem * item = new QListWidgetItem("123456789");
ui.listWidget->addItem ( item );
if (connect( ui.listWidget, SIGNAL( itemClicked (QListWidgetItem *) ), SLOT( changeDate() ) ) )
ui.lblDate->setText("connect is good");
}

//.. snip
void changeDate()
{
ui.lblDate->setText("It works");
}

jpn
18th January 2008, 20:19
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?

rishid
18th January 2008, 20:33
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.

jpn
18th January 2008, 21:30
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.

rishid
18th January 2008, 21:43
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.