【问题标题】:Get item from QListWidget using a QItemSelection or QModelIndex使用 QItemSelection 或 QModelIndex 从 QListWidget 获取项目
【发布时间】:2014-06-05 19:41:06
【问题描述】:

我的对话框中有一个回调来监听 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!?
    }
}

正如您在待办事项中看到的,我不知道如何从 QItemSelection 对象中获取关联的 QListWidgetItem。我可以使用listWidget 类变量访问列表小部件。非常感谢任何帮助。

【问题讨论】:

    标签: qt qt4


    【解决方案1】:

    为了从选择中获得QListWidgetItem,您可以执行以下操作:

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

    【讨论】:

    • 感谢您的快速响应!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2021-04-23
    • 2014-08-17
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多