You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

重写QAbstractTableModel的setData触发dataChanged队列参数警告疑问

Qt MVC模型触发dataChanged信号时的QVector元类型注册问题

我实现了一个遵循MVC模式的数据模型class NeighborsModel : public QAbstractTableModel,重写setData方法并在方法末尾触发dataChanged信号,运行时出现如下警告:

QObject::connect: Cannot queue arguments of type 'QVector'
(Make sure 'QVector' is registered using qRegisterMetaType().)

我怀疑需要将QVector注册为元类型,以下是相关代码:

setData方法实现

bool NeighborsModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
    ...
    emit dataChanged(index,index);
    return true;
}

dataChanged信号原型

Q_SIGNALS:
    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>());

已尝试的解决方法

我通过在NeighborsModel构造函数中添加以下代码解决了问题,但不确定这是否为最佳实践:

#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 )
    if (QMetaType::type("QVector<int>") == QMetaType::UnknownType)
#else
    if (QMetaType::type("QVector<int>") == 0
        || !QMetaType::isRegistered(QMetaType::type("QVector<int>")))
#endif
    {
        qRegisterMetaType<QVector<int>>("QVector<int>");
    }

内容的提问来源于stack exchange,提问作者fakamakato

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 15:02:40