Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 使用QItemSelection或QModelIndex从QListWidget获取项_Qt_Qt4 - Fatal编程技术网

Qt 使用QItemSelection或QModelIndex从QListWidget获取项

Qt 使用QItemSelection或QModelIndex从QListWidget获取项,qt,qt4,Qt,Qt4,我的对话框中有一个回调函数,用于侦听QListWidget中的项目选择: ... QListWidget* listWidget; ... MyDialog::handleSelectionChanged(const QItemSelection& selection) { if (selection.indexes().isEmpty()) { std::cout << "NOTHING SELECTED" << std::endl;

我的对话框中有一个回调函数,用于侦听QListWidget中的项目选择:

...
QListWidget* listWidget;
...

MyDialog::handleSelectionChanged(const QItemSelection& selection) {
    if (selection.indexes().isEmpty()) {
        std::cout << "NOTHING SELECTED" << std::endl;
        // TODO: how to get the actual QListWidgetItem here!?
    }
    else {
       bool selected = LoadedFilesListWidget->selectionModel()->isSelected(selection.indexes().first());
       std::cout << "ITEM CHANGE: " << (selected ? "SELECTED" : "UNSELECTED") << std::endl;
       // TODO: how to get the actual QListWidgetItem here!?
    }
}
。。。
QListWidget*listWidget;
...
MyDialog::handleSelectionChanged(常量QItemSelection&selection){
if(selection.index().isEmpty()){
std::cout isSelected(selection.indexes().first());

std::cout要从选择中获取
QListWidgetItem
,可以执行以下操作:

MyDialog::handleSelectionChanged(const QItemSelection& selection)
{
    [..]

    QModelIndexList indexes = selection.indexes();
    foreach(const QModelIndex &index, indexes) {
        QListWidgetItem *item = LoadedFilesListWidget->item(index.row());
        // ...
    }
}

要从选择中获取
QListWidgetItem
,可以执行以下操作:

MyDialog::handleSelectionChanged(const QItemSelection& selection)
{
    [..]

    QModelIndexList indexes = selection.indexes();
    foreach(const QModelIndex &index, indexes) {
        QListWidgetItem *item = LoadedFilesListWidget->item(index.row());
        // ...
    }
}

感谢您的快速响应!感谢您的快速响应!