Here is the class that inherits from QListWidget
:
typedef void(*fnc) (QListWidgetItem* item);
class CustomListWidget : public QListWidget
{
public:
CustomListWidget();
void AddItemList(std::vector<std::string>* list);
void ConnectToOnClickSlot(fnc func);
private:
fnc onClick;
void InvokeOnclickMethod(QListWidgetItem* item);
};
Corresponding .cpp file (part of it):
CustomListWidget::CustomListWidget()
{
QObject::connect(this, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(InvokeOnclickMethod(QListWidgetItem*)));
}
void CustomListWidget::ConnectToOnClickSlot(fnc func)
{
onClick = func;
}
void CustomListWidget::InvokeOnclickMethod(QListWidgetItem* item)
{
onClick(item);
}
I was expecting that InvokeOnclickMethod
to be called when items are clicked, but nothing happens. What might be the problem?
The main part:
auto listView = new CustomListWidget();
listView->ConnectToOnClickSlot(ItemClicked);
void ItemClicked(QListWidgetItem* item)
{
//Do something...
}
You need to add Q_OBJECT
macro for the class, where you want to use signals/slots. IIRC, the "old" connection also requires slots to be declared in <accessibility> slots:
section.
By the way, people have been using the "new" signal/slot connection syntax for about three years now.