c++ 当QDialog中有三个QListView时,QListView刷新缓慢

ukdjmx9f  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(112)

我自定义了从QStringListModel继承的数据模型AnnotationListModel
AnnotationListModel类的目的,是为数据项添加复选框,实现数据项之间的互斥。
现在,我在QDialog中创建了三个QListView和AnnotationListModel对象。模型中的数据项作为复选框添加,并且是独占的。
我可以在QListView中选中复选框,但选中的状态无法快速显示。
如何使QListView快速刷新?

class AnnotationListModel :public QStringListModel
{
    Q_OBJECT
public:
    AnnotationListModel(AnnotationType annType, const QStringList& stringList, QObject* parent = nullptr);
    ~AnnotationListModel() {};

protected:
    Qt::ItemFlags flags(const QModelIndex& index) const override;
    QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
    bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;

signals:
    void sendAnnotation(const AnnotationType& annoType, const QString& annotation);

private:
    AnnotationType m_annoType;
    QStringList m_stringList;
    QMap<QString, bool> m_stringList_map; //<annotationName,checked>
};

AnnotationListModel::AnnotationListModel(AnnotationType annType, const QStringList& stringList, QObject* parent)
    :QStringListModel(parent), m_stringList(stringList), m_annoType(annType)
{
    setStringList(m_stringList);
    for (auto& value : m_stringList)
    {
        m_stringList_map[value] = false;
    }
}

Qt::ItemFlags AnnotationListModel::flags(const QModelIndex& index) const
{
    if (!index.isValid())
    {
        return Qt::ItemIsEnabled;
    }
    return Qt::ItemIsUserCheckable | QStringListModel::flags(index);
}

QVariant AnnotationListModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
    {
        return QVariant();
    }

    if (role == Qt::CheckStateRole)
    {
        if (m_stringList_map[m_stringList.at(index.row())] == true)
        {
            return Qt::Checked;
        }
        else
        {
            return Qt::Unchecked;
        }
    }
    else if (role == Qt::DisplayRole)
    {
        return m_stringList.at(index.row());
    }

    return QStringListModel::data(index, role);
}

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

    if (role == Qt::CheckStateRole)
    {
        if (value == Qt::Checked)
        {
            for (int i{ 0 }; i < m_stringList.size(); ++i)
            {
                if (i != index.row())
                {
                    m_stringList_map[m_stringList.at(i)] = false;
                }
                else
                {
                    m_stringList_map[m_stringList.at(i)] = true;
                    sendAnnotation(m_annoType, m_stringList.at(i));
                }
            }
        }
        else if (value == Qt::Unchecked)
        {
            m_stringList_map[m_stringList.at(index.row())] = false;
        }
    }

    return QStringListModel::setData(index, value, role);
}

字符串
创建三个AnnotationListModel对象:

switch (itr_kind.key())
{
case AnnotationType::CounCode:
    m_counCodeModel = new AnnotationListModel(AnnotationType::CounCode, l_annoStrList);
    m_uiDialog->listView_country->setModel(m_counCodeModel);
    connect(m_counCodeModel, &AnnotationListModel::sendAnnotation, this, &AnnotationDialog::sendAnnotation);
    break;

case AnnotationType::DriSide:
    m_driSideModel = new AnnotationListModel(AnnotationType::DriSide, l_annoStrList);
    m_uiDialog->listView_driving->setModel(m_driSideModel);
    connect(m_driSideModel, &AnnotationListModel::sendAnnotation, this, &AnnotationDialog::sendAnnotation);
    break;

case AnnotationType::RoadType:
    m_roadTypeModel = new AnnotationListModel(AnnotationType::RoadType, l_annoStrList);
    m_uiDialog->listView_road->setModel(m_roadTypeModel);
    connect(m_roadTypeModel, &AnnotationListModel::sendAnnotation, this, &AnnotationDialog::sendAnnotation);
}


对话框通过mainwindow打开。


的数据

niknxzdl

niknxzdl1#

在我看来,你犯了四个错误,但最重要的是前两个:

  • 将功能添加到现有模型(在您的情况下为QStringListModel)的正确方法是使用代理模型(QAbstractProxyModel的子类)。它工作得很好,但更容易实现,并且更灵活,因为它可以与任何模型类型一起使用。
  • 当你想让你的复选框是独占的时,你只需要记住选中的那个复选框,而不需要有一个完整的QMap<...>容器。

我相信这就是代码中大部分计算时间被浪费的地方,特别是在调试模式下测试对话框时(容器有很多很多只在调试中完成的检查,这使得它们的使用速度比发布时慢得多)。
至少,不将检查过的索引保存为QModelIndex是正确的。

  • 您忘记从setData方法发出dataChanged信号。
  • 您忘记从setData方法返回false/true。准确地说,确实返回了一个布尔值,但仅由QStringListModel::setData(...)返回,这不是您想要做的。

下面的AnnotationProxyModel为你想要实现的任何模型实现了排他性的复选框。它派生QIdentityProxyModel,并通过QPersistentModelIndex记住选中的索引(与QModelIndex不同,它可以保存)。
我让你检查你是否真的需要保持Q_OBJECT宏和你的信号(S)在下面的定义,如果是的,添加他们回来自己。

#include <QtCore/QIdentityProxyModel>
class AnnotationProxyModel : public QIdentityProxyModel {
public:
    AnnotationProxyModel(QObject* parent = nullptr);
    QVariant data(const QModelIndex& index, int role) const override;
    Qt::ItemFlags flags(const QModelIndex& index) const override;
    bool setData(const QModelIndex& index, const QVariant& value, int role) override;
private:
    QPersistentModelIndex checkedIndex;
};

AnnotationProxyModel::AnnotationProxyModel(QObject* parent) : 
    QIdentityProxyModel(parent),
    checkedIndex()
{
}

QVariant AnnotationProxyModel::data(const QModelIndex& index, int role) const
{
    switch (role) {
    case Qt::CheckStateRole: return QVariant((checkedIndex == index) ? Qt::Checked : Qt::Unchecked);
    default: return QIdentityProxyModel::data(index, role);
    }
}

Qt::ItemFlags AnnotationProxyModel::flags(const QModelIndex& index) const
{
    return QIdentityProxyModel::flags(index) | Qt::ItemIsUserCheckable;
}

bool AnnotationProxyModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
    switch (role) {
    case Qt::CheckStateRole: {
        if (checkedIndex == index) {
            if (value.value<Qt::CheckState>() != Qt::Unchecked) // Trying to check the already checked index -> return false
                return false;
            else {
                checkedIndex = QPersistentModelIndex(); // Unchecking the already checked index -> return true;
                emit dataChanged(index, index, { Qt::CheckStateRole });
                return true;
            }
        }
        else {
            if (value.value<Qt::CheckState>() == Qt::Unchecked) // Trying to uncheck an index that is already unchecked -> return false
                return false;
            else {
                QModelIndex uncheckedIndex = checkedIndex;
                checkedIndex = QPersistentModelIndex(index);
                emit dataChanged(uncheckedIndex, uncheckedIndex, { Qt::CheckStateRole });
                emit dataChanged(index, index, { Qt::CheckStateRole });
                return true;
            }
        }
    }
    default: return QIdentityProxyModel::setData(index, value, role);
    }
}

字符串
与任何其他代理模型一样,它是通过使用QStringListModel调用setSourceModel(...)来使用的,即以这种方式:

AnnotationProxyModel* checkModel = new AnnotationProxyModel(view);
checkModel->setSourceModel(countryModel);
view->setModel(checkModel);

相关问题