[Qt]双击事件导致的问题

有如下代码

cpp 复制代码
#include "mymodel.h"
#include <QDebug>

myModel::myModel(QObject *parent) : QAbstractTableModel(parent)
{
    status << Qt::Unchecked << Qt::Unchecked << Qt::Unchecked;
}

int myModel::rowCount(const QModelIndex &parent)const
{
    Q_UNUSED(parent);
    return 3;
}

int myModel::columnCount(const QModelIndex &parent)const
{
    Q_UNUSED(parent);
    return 2;
}

void myModel::setHeader(const QStringList &list)
{
    m_header = list;
}

QVariant myModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role == Qt::DisplayRole)
    {
        if(orientation == Qt::Horizontal)
        {
            return m_header.at(section);
        }
    }
    return QVariant();
}

QVariant myModel::data(const QModelIndex &index, int role)const
{
    if(role == Qt::DisplayRole)
    {
        if(index.column() == 0)
        {
            return QStringLiteral("%1号同学").arg(index.row() + 1);
        }
        if(index.column() == 1)
        {
            return QStringLiteral("1000%1").arg(index.row() + 1);
        }
    }
    if(role == Qt::CheckStateRole)
    {
        if(index.column() == 0)
        {
            return status.at(index.row());
        }
        return QVariant();
    }
    return QVariant();
}

Qt::ItemFlags myModel::flags(const QModelIndex &index) const
{
    if(!index.isValid())
    {
        return QAbstractItemModel::flags(index);
    }
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    if(index.column() == 0)
    {
        flags |= Qt::ItemIsUserCheckable;
    }
    return flags;
}

bool myModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid())
    {
        return false;
    }

    if(role == Qt::CheckStateRole)
    {
        if(index.column() == 0)
        {
            status[index.row()] = value.toInt();
            emit dataChanged(index, index);
            return true;
        }
    }
    return false;
}

效果如下

额,效果不是很明显。但是当我快速点击鼠标左键时,发现checkbox的状态并没有立即修改,这是因为快速点击鼠标左键时,被识别成了双击事件,修改思路如下,创建一个类继承于QStyledItemDelegate,然后重写 bool QStyledItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) 这个方法,当遇到 双击事件时,单独处理即可。代码如下

cpp 复制代码
#include "MyStyledItemDelegate.h"
#include <QEvent>

MyStyledItemDelegate::MyStyledItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{

}

bool MyStyledItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if(event->type() == QEvent::MouseButtonDblClick && index.column() == 0){
        model->setData(index, index.data(Qt::CheckStateRole).toInt() == Qt::Checked ? Qt::Unchecked : Qt::Checked ,Qt::CheckStateRole);
        return true;
    }else{
        return QStyledItemDelegate::editorEvent(event, model, option, index);
    }
}

修正后效果如图

相关推荐
Sarvartha1 小时前
final 关键字
java·开发语言
程序员Sunday1 小时前
JavaScript 事件循环面试题,宏任务与微任务怎么执行|Sunday面试指南
开发语言·javascript·面试·校招·事件循环·程序员sunday
Quz2 小时前
QML 动画选择:GIF与Lottie 的区别,以及 Qt 6.8 与 6.11 的 Lottie 实现差异
qt
zhangzeyuaaa2 小时前
Ruby 多线程、GVL(GIL)与 Mutex 完全指南
开发语言·前端·ruby
谢亮_vipxieliang3 小时前
用 PHP 构建轻量级 REST API:从路由到鉴权的完整实践
开发语言·后端·php
思无邪664 小时前
用 AI 做 JS 逆向:从抓包到复现的完整方法论
开发语言·javascript·人工智能
H.莓飛4 小时前
【数据结构】二叉树_OJ题
linux·开发语言·数据结构·算法
零基础1235 小时前
Ubuntu 常用命令汇总
linux·运维·开发语言
外收内放5 小时前
Python基础语法练习题(57-58)
开发语言·python
时间的拾荒人5 小时前
Qt 界面美化实战:QSS 样式表
开发语言·qt·面试