PDA

View Full Version : signal of QListWidget doesn't work



vito49
29th September 2008, 17:39
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

jpn
29th September 2008, 17:48
You must not put parameter names to the connect statement. It should be:


connect(listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(enableDeleteButton()));

vito49
29th September 2008, 20:28
You must not put parameter names to the connect statement. It should be:


connect(listWidget, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(enableDeleteButton()));


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

spirit
29th September 2008, 20:40
you can use


QListWidgetItem * QListWidget::currentItem () const

montylee
30th September 2008, 06:19
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:

connect(listWidget, SIGNAL(itemActivated(QListWidgetItem *)), this, SLOT(enableDeleteButton(QListWidgetItem *)));

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.

vito49
30th September 2008, 16:33
you can use the following syntax:

connect(listWidget, SIGNAL(itemActivated(QListWidgetItem *)), this, SLOT(enableDeleteButton(QListWidgetItem *)));

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 ...



connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(delete_Item(QListWidgetItem*)));

void myQtApp::delete_Item(QListWidgetItem* item)
{
QMessageBox msgDelete;
msgDelete.setText("item is going to be removed");
msgDelete.setStandardButtons(QMessageBox::Yes);
int msg=msgDelete.exec();

listWidget->removeItemWidget(item);
}

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.

jpn
30th September 2008, 17:52
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.

vito49
30th September 2008, 18:11
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.

jpn
30th September 2008, 18:20
Use the C++ delete operator:


delete item;

:)

vito49
30th September 2008, 18:35
Use the C++ delete operator:


delete item;

:)

Yep, that was it ... as easy as that.
Well I do have to learn a lot, but I'm a good student (I think) :D

Many thanks for your help.

montylee
1st October 2008, 05:07
Before delete, you can delete the item by using the following API:


QListWidgetItem *QListWidget::takeItem ( int row )

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.