c++ - Qt 5.15 - 带有 QAbstractTableModel 的 QSortFilterProxyModel 在 dataChanged 信号上崩溃
问题描述
我已经实现了一个自定义 QAbstractTableModel 并且我已经通过 QAbstractItemModelTester 运行它并且我的模型中没有更多问题。但是,我现在正在尝试通过 QSortFilterProxyModel 实现排序,但我似乎根本无法得到任何工作。
void RDMSensorModels::UpdateDevice(ArtNet::ArtRdmDevice* rdmDev, const RDM::RDMProcessor::RDMDeviceModel& model, int pid) {
if (s_RequiredPIDs.contains(pid)) {
for (int i = 0; i < m_RDMDevices.size(); i++) {
if (m_RDMDevices[i] == rdmDev) {
emit dataChanged(createIndex(i, 0), createIndex(i, columnCount() - 1));
return;
}
}
}
}
这是发出模型 dataChanged 信号的函数,我认为这里没有问题,但是在发出此信号后,程序在 QSortFilterProxyModels 内部 dataChanged 处理程序中崩溃
因为我还不能在我的问题中嵌入图片,所以这里有一个链接到调试器在 QSortFilterProxyModel 中中断的位置
最奇怪的是,无论我将什么传递给 dataChanged 信号,QSortFilterProxyModel 中的 proxy_columns 始终为空。
如果有任何帮助,这是我的 QSortFilterProxyModel 实现,它基本上完全是空的。
class RDMSensorSortFilterProxyModel final : public QSortFilterProxyModel {
enum SortValue {
MANUFACTUER_MODEL,
UNIVERSE_DMXADDRESS,
};
public:
RDMSensorSortFilterProxyModel(RDMSensorModels *sourceModel, QObject *parent = nullptr) : QSortFilterProxyModel(parent) {
setSourceModel(sourceModel);
}
int SortIndex();
void SetSortIndex(int value);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
private:
SortValue m_SortValue = MANUFACTUER_MODEL;
};
int RDMSensorSortFilterProxyModel::SortIndex() { return m_SortValue; }
void RDMSensorSortFilterProxyModel::SetSortIndex(int value) {
m_SortValue = static_cast<SortValue>(value);
invalidate();
}
bool RDMSensorSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const { return true; }
bool RDMSensorSortFilterProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const {
auto leftDeviceManufacturer = sourceModel()->data(left, RDMSensorModels::Roles::DeviceManufacturerRole).toString();
auto rightDeviceManufacturer = sourceModel()->data(right, RDMSensorModels::Roles::DeviceManufacturerRole).toString();
auto same = QString::compare(leftDeviceManufacturer, rightDeviceManufacturer, Qt::CaseInsensitive) == 0;
return same;
}
这是我的 QAbstractTableModel 重新实现的函数
QVariant RDMSensorModels::headerData(int section, Qt::Orientation orientation, int role) const {
if (section < 1)
return QString("Device");
else
return QString("Sensor %1").arg(section);
}
int RDMSensorModels::rowCount(const QModelIndex& parent) const {
if (parent.isValid())
return 0;
return m_RDMDevices.count();
}
int RDMSensorModels::columnCount(const QModelIndex& parent) const {
if (parent.isValid())
return 0;
return m_ColumnCount;
}
QVariant RDMSensorModels::data(const QModelIndex& index, int role) const {
if (!index.isValid())
return {};
int deviceIndex = index.row();
switch (role) {
case SensorGraphReadingsRole: {
auto& readings = m_RDMDevices[deviceIndex]->Sensors()[index.column() - 1]->LastReadings();
auto maxElement = f_SensorMaxReading(index.row(), index.column() - 1);
auto minElement = f_SensorMinReading(index.row(), index.column() - 1);
QVariantList values;
for (int i = 0; i < readings.size(); i++) {
values.push_back(Utils::Math::map(readings[i], maxElement, minElement, 0, 1));
}
return values;
}
case SensorMinReadingRole: return f_SensorMinReading(deviceIndex, index.column() - 1);
case SensorMaxReadingRole: return f_SensorMaxReading(deviceIndex, index.column() - 1);
case DeviceUIDRole: return f_DeviceUIDString(deviceIndex);
case DeviceUniverseRole: return f_DeviceUniverseString(deviceIndex);
case DeviceLabelRole: return f_DeviceLabelString(deviceIndex);
case DeviceManufacturerRole: return f_DeviceManufacturerString(deviceIndex);
case DeviceModelRole: return f_DeviceModelString(deviceIndex);
case SensorRangeMaxValueRole: return f_SensorRangeMaxValueString(deviceIndex, index.column() - 1);
case SensorRangeMinValueRole: return f_SensorRangeMinValueString(deviceIndex, index.column() - 1);
case SensorCurrentValueRole: return f_SensorCurrentValueString(deviceIndex, index.column() - 1);
case SensorNameRole: return f_SensorNameString(deviceIndex, index.column() - 1);
case SensorCurrentValueNormalizedRole: return f_SensorCurrentValueNormalized(deviceIndex, index.column() - 1);
case SensorMinNormalValueNormalizedRole: return f_SensorMinNormalValueNormalized(deviceIndex, index.column() - 1);
case SensorMaxNormalValueNormalizedRole: return f_SensorMaxNormalValueNormalized(deviceIndex, index.column() - 1);
case SensorValidRole: {
auto sensorCount = f_DeviceSensorCount(deviceIndex);
return sensorCount && (index.column() <= sensorCount);
}
default: return {};
}
}
QHash<int, QByteArray> RDMSensorModels::roleNames() const { return s_RoleNames; }
任何帮助将不胜感激!
解决方案
事实证明,试图在更小的范围内复制这个问题让我的大脑神经元足够活跃,我发现了这个问题。我的模型列数可以更改并且确实会更改,但是,我没有编写任何通知列数更改 beginRemoveColumns 和 endRemoveColumns 以及 beginInsertColumns 和 endInsertColumns 的内容。我像这样在我的代码中实现了这些
void RDMSensorModels::UpdateColumnCount() {
int sensorCount = 1;
for (auto device : m_RDMDevices) {
int deviceSensorCount = device->Sensors().size();
if (deviceSensorCount + 1 > sensorCount)
sensorCount = deviceSensorCount + 1; // +1 for device column
}
if (m_ColumnCount != sensorCount) {
if (m_ColumnCount < sensorCount) {
beginInsertColumns(QModelIndex(), m_ColumnCount, sensorCount - 1);
m_ColumnCount = sensorCount;
endInsertColumns();
} else {
beginRemoveColumns(QModelIndex(), sensorCount, m_ColumnCount - 1);
m_ColumnCount = sensorCount;
endRemoveColumns();
}
}
}
并且代理模型现在按预期工作。希望这可以帮助其他遇到 QSortFilterProxyModel 问题的人。
有趣的是,QAbstractItemModelTester 没有发现这个问题,因为我的模型会根据当前找到的设备的最大传感器数量来更改列数,因此我预计会出现这种问题。
推荐阅读
- c++ - 多客户端 websocket 应用程序问题
- webpack - 带有部分的 webpack5 html 模板不适用于“npm run dev”
- firebase - 如何从我的存储库中获取数据,我无法获取我存储的数据
- python - 为什么这些位反转实现以如此不同的速度运行?
- asp.net-core - 如何在 .net6 中使用 WebApplicationFactory(没有可说的入口点)
- java - JavaFX Robot.getScreenCapture 导致场景之前没有更新
- unity3d - 如何在 Unity 中恢复丢失的场景
- python - 查找凸多边形外部垂直于其边缘的点
- c# - 如何在连接上使用代理?
- google-bigquery - 我可以有效地对 BigQuery 中的日期分区表进行 GROUP BY