简体   繁体   中英

C++ Qt Do something when an QListWidgetItem from a QListWidget is clicked or selected

QListWidgetItem* lwi = new QListWidgetItem(text.c_str());
lw->addItem(lwi);
QObject::connect(lwi, &QListWidgetItem::isSelected, &lwi, []() {
    exit(0);
});

I want to do something like this, where if an item from the QListWidget is selected or clicked the program will just exit. But this is not the correct syntax and I have no idea how to make it right. Any help?

QListWidgetItem::isSelected is not a Qt signal.

Most likely, your QListWidgetItem will be embedded in a QListWidget . QListWidget provides several signals which may fit your needs. Eg:

QListWidgetItem* lwi = new QListWidgetItem(text.c_str());
lw->addItem(lwi);

// Assuming lw is a QListWidget*
QObject::connect(lw, &QListWidget::currentItemChanged,
    [lwi](QListWidgetItem* current, QListWidgetItem* /*previous*/)
    {
        if (current == lwi) std::exit(0);
    });

Please also note you don't need to pass the receiver address when connecting a signal to a lambda (or a functor, in general).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM