Search code examples
c++qtqt5qt-creatorqlistwidget

Click action on QListWidget


I created a List widget and added some indexes (or items) on it with Designer (Shutdown). Now I need to do something when I click on this item (index). But I can't understand how to do it.

connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(itemClicked(QListWidgetItem*)));

void MainWindow::on_listWidget_clicked(const QModelIndex &index)
{
    if(ui->listWidget->indexWidget(index) == QAbstractItemView::Shutdown) {
        command = "shutdown /s /t 7200";
    }
}

I get error 'Shutdown' is not a member of 'QListWidgetItem'

The row

void MainWindow::on_listWidget_clicked(const QModelIndex &index)

was created automatically when I added the click action.

Screenshot of my List


Solution

  • You must use the slot called itemClicked:

    And then filter by the text:

    void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
    {
        if(item->text()=="Shutdown"){
            command = "shutdown /s /t 7200";
        }
    }