c++ - Proxy 的模型函数没有被调用

标签 c++ qt

我已将源模型连接到我的代理模型,并将我的代理模型作为模型连接到 View :

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),model_(new Model(this)),proxy_(new Proxy(this))
{
    setupUi(this);
    proxy_->setSourceModel(model_);

    listView->setModel(proxy_);

}

在代理类中我有一个 fnc:

int Proxy::rowCount(const QModelIndex&) const
{
    static int a = 0;
    qDebug() << "Proxy::rowCount sourceModel()->rowCount() " << a++ << ": "<< sourceModel()->rowCount();
    return sourceModel()->rowCount();
}

但是当我通过模型的 fnc 添加要查看的内容时不会调用它:

bool Model::set_data(int data)
{
    beginInsertRows(QModelIndex(),0,data_.size());
    data_.append(data);
    static int a = 0;
    qDebug() << "Model::set_data data_ " << a++ << ":" << data_;
    endInsertRows();
    emit dataChanged(createIndex(0,0),createIndex(data_.size(),0));
    return true;
}

上述功能是通过 SIGNAL SLOT 连接对话框上的按钮连接的:
QObject::connect(pushButton, SIGNAL(clicked()), Dialog, SLOT(insert()));对话框中的插入内容如下所示:

 bool Dialog::insert()
 {
     static int a = 0;
     return model_->set_data(a++);

 }

但是尽管如此, View 并没有显示任何内容。另一方面,如果我作为模型连接到 View ,我的模型类 obj 而不是代理,一切正常。
有人知道这里出了什么问题吗?
编辑:: 测试模型后:

ASSERT failure in QList<T>::at: "index out of range", file c:\QtSDK\Desktop\Qt\4.7.4\mingw\include/QtCore/qlist.h, line 456

仅代理测试后:

D:\...\tst_mpv.exe exited with code -1073741819  

我的主要 fnc 外观:

#include <QApplication>
#include "Dialog.h"
#include "Model.h"
#include "Proxy.h"
#include "modeltest.h"
int main(int c,char**v)
{
    QApplication app(c,v);
    /*Model* m = new Model;
    new ModelTest(m);*/
    Proxy* p = new Proxy;
    new ModelTest(p);
    /*Dialog d;
    d.show();*/
    return app.exec();
}

这是我的模型和代理类:http://pastebin.com/DiAAkiNY

最佳答案

这里是代理模型的完整示例,使用(如文档中所推荐的)QSortFilterProxyModel

QSortFilterProxyModel 构建是最简单的方法,因为所有棘手的部分都已完成。

测试.cpp

#include <QtGui>

#include "proxy.h"

int main(int argc, char** argv)
{
        QApplication app(argc, argv);

        QStringList list;
        list << "ant" << "bear" << "cat" << "dog";

        QStringListModel mdl(list);

        QListView viewRaw;
        viewRaw.setModel(&mdl);
        viewRaw.show();

        Proxy proxy;
        proxy.setSourceModel(&mdl);

        QListView viewPrx;
        viewPrx.setModel(&proxy);
        viewPrx.show();

        return app.exec();
}

proxy.h

#ifndef _PROXY_H_
#define _PROXY_H_

#include <QtGui>

class Proxy : public QSortFilterProxyModel
{
        public:
                virtual QVariant data(const QModelIndex& proxyIndex, int role = Qt::DisplayRole) const;
};

#endif

proxy.cpp

#include "proxy.h"

QVariant Proxy::data(const QModelIndex& proxyIndex, int role) const
{
        QVariant d = QSortFilterProxyModel::data(proxyIndex, role);
        if (proxyIndex.isValid() && role == Qt::DisplayRole)
                return QVariant(QString("[[%1]]").arg(d.toString()));
        return d;
}

test.pro

QT += core gui
SOURCES=test.cpp proxy.cpp
HEADERS=proxy.h

关于c++ - Proxy 的模型函数没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8322883/

相关文章:

c++ - qt 5.4/clang:体系结构 x86_64 的 undefined symbol (std::istream::gcount())

QtCreator语义问题警告代码永远不会被执行

c++ - 如何使用 execv 生成后台程序

c++ - 修复 TinyWireS 库的 I2C 地址问题

c++ - 在多个平台和架构上部署?

c++ - 从文件中读取输入

c++ boost::serialization 为类设置固定的 class_id

qt - 如何有效地对 QByteArray 进行分区?

qt - 检查 ui 元素的值是否已更改

qt - 为什么 QWebSocketServer 在客户端连接尝试时发送 TCP [FIN] 消息