QListView项目具有复选框选择行为 [英] QListView item with checkbox selection behavior

查看:1192
本文介绍了QListView项目具有复选框选择行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将复选框项添加到列表视图.

I'm adding checkbox items to a list view.

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

Then when I change the check box indicator, the item row is not selected. And when I'm selection an item in the list, the check box indicator won't change.

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

The checkbox indicator should be selected/deselected on item selection row, and checkbox indicator selection should set the item row selected.

列表视图初始化:

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);

有什么建议吗?

推荐答案

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

Solved this issue by connection two signals

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

这是我的代码:

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);
}

这篇关于QListView项目具有复选框选择行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆