I have a QListWidget
in my MainWindow that displays a list of VideoWidgets (a custom QWidget
).
VideoWidget
has a clickable label where on clicking the label it should delete a file and then remove the QListItem
which holds the VideoWidget
from the QListWidget
. Here is my VideoWidget class:
VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent)
{
ClickableLabel *smallRed = new ClickableLabel(this)
//...
QObject::connect(smallRed,SIGNAL(clicked()),this,SLOT(removeVideo()));
}
void VideoWidget::removeVideo(){
//...code to remove a file
QListWidget* list = myParent->getList();
QListWidgetItem* item = list->takeItem(list->currentIndex().row());
myList->removeItemWidget(item);
}
The problem is that clicking the smallRed
label will not select its item in the QListWidget which means that list->currentIndex().row()
will return -1. Clicking anywhere else in the Widget does select the current item. For the code to work I currently have to first click anywhere in the VideoWidget
and then click its ClickableLabel
. Is there any way I can achieve the same effect with one single click on my ClickableLabel
?
From your previous qestion, we suggested use signal and slots. For example:
Slot:
It is not unique way to solve this problem, but it shows all main things, with signals and slots mechanism,
objectName
andsender()
you can achieve all what you need.sender()
return object which send signal, you can cast it, but if you need onlyobjectName
you should not cast.