c++ - QListView项目具有复选框选择行为

标签 c++ qt qlistview qstandarditem

我正在将复选框项添加到列表 View 。

然后,当我更改复选框指示器时,未选择项目行。
当我在列表中选择一个项目时,复选框指示符将不会更改。

应该在项目选择行上选择/取消选中复选框指示器,并且应该通过选择复选框指示器来设置选定的项目行。

列表 View 初始化:

QListView *poListView = new QListView(this);

// Create list view item model
QStandardItemModel*  poModel =
          new QStandardItemModel(poListView);

QStandardItem *poListItem = new QStandardItem;

// Checkable item
poListItem->setCheckable( true );

// Uncheck the item
poListItem->setCheckState(Qt::Unchecked);

// Save checke state
poListItem->setData(Qt::Unchecked, Qt::CheckStateRole);

poModel->setItem(0, poListItem);

poListView->setModel(poModel);

有什么建议吗?

最佳答案

通过连接两个信号解决了这个问题

  • 已注册的模型项目已更改信号,用于处理复选框指示符更改。
  • 已注册的查看项已激活信号,用于更改复选框指示符状态

  • 这是我的代码:
    void MyClass:Init() 
    {
        m_poListView = new QListView(this);
    
        // Set single selection mode
        m_poListView->setSelectionMode(
                   QAbstractItemView::SingleSelection);
    
        // Create list view item model
        QStandardItemModel*  poModel =
                  new QStandardItemModel(m_poListView);
    
        QStandardItem * poListItem =
                  new QStandardItem;
    
        // Checkable item
        poListItem->setCheckable( true );
    
        // Save checke state
        poListItem->setData(Qt::Unchecked, Qt::CheckStateRole);
    
        poModel->setItem(0, poListItem);
    
        m_poListView->setModel(poModel);
    
         // Register model item  changed signal
           connect(poModel, SIGNAL(itemChanged(QStandardItem*)),
           this,            SLOT  (SlotItemChanged(QStandardItem*)));
    
        // Resister view item acticated
         connect( m_poListView , SIGNAL(activated(const QModelIndex & )),
                     this,       SLOT(SlotListItemActivated(const QModelIndex & )))
    
    }
    

    插槽限制:
    void MyClass::SlotItemChanged(QStandardItem *poItem)
    {
        // Get current index from item
        const QModelIndex oCurrentIndex =
                poItemChanged->model()->indexFromItem(poItem);
    
        // Get list selection model
        QItemSelectionModel *poSelModel =
                m_poListView->selectionModel();
    
        // Set selection
        poSelModel->select(
                    QItemSelection(oCurrentIndex, oCurrentIndex),
                    QItemSelectionModel::Select | QItemSelectionModel::Current);
    }
    
    void MyClass::SlotListItemActivated(const QModelIndex &oIndex)
    {
        Qt::CheckState eCheckState = Qt::Unchecked;
    
        // Get item's check state
        bool bChecked =
                oIndex.data(Qt::CheckStateRole).toBool();
    
        // Item checked ?
        if (bChecked == false) 
            eCheckState = Qt::Checked;
        else 
            eCheckState = Qt::Unchecked;
    
        // Get index model
          //    Note: I used QSortFilterProxyModel in the original code
        QSortFilterProxyModel *poModel = 
            (QSortFilterProxyModel *)oIndex.model();
    
        // Update model data
        poModel->setData(oIndex, eCheckState, Qt::CheckStateRole);
    }
    

    关于c++ - QListView项目具有复选框选择行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31163516/

    相关文章:

    c++ - C++通过别名指针默默地将错误的类型分配给数组元素

    c++ - QT/C++ - 从不同的类访问 MainWindow UI

    python - 如何使项目 View 在 PyQt 中呈现丰富的 (html) 文本?

    qt - QListWidget或QListView与QItemDelegate?

    c++ - 可变参数模板函数的特化

    访问 PHP superglobals 的 PHP 扩展库

    c++ - QtWebkit QWebView setUrl内存泄漏

    Qt - 将 int 转换为文件大小格式(KB、MB 或 GB)的最简单方法?

    string - 从 QListView 获取文本

    c# - 在 Xcode 中创建 C/C++ 库并将其导入到 C# .NET Core 中