Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用代理模型_C++_Qt - Fatal编程技术网

C++ 使用代理模型

C++ 使用代理模型,c++,qt,C++,Qt,我通过子类化QabStretcProxyModel创建了代理模型,并将其作为模型连接到视图。我还为此代理模型设置了源模型。不幸的是,有些地方出了问题,因为我的listView上没有显示任何内容(当我将模型作为要查看的模型提供时,它工作得很好,但当我提供此代理模型时,它就不工作了)。以下是我的代码中的一些片段: #ifndef FILES_PROXY_MODEL_H #define FILES_PROXY_MODEL_H #include <QAbstractProxyModel> #

我通过子类化QabStretcProxyModel创建了代理模型,并将其作为模型连接到视图。我还为此代理模型设置了源模型。不幸的是,有些地方出了问题,因为我的listView上没有显示任何内容(当我将模型作为要查看的模型提供时,它工作得很好,但当我提供此代理模型时,它就不工作了)。以下是我的代码中的一些片段:

#ifndef FILES_PROXY_MODEL_H
#define FILES_PROXY_MODEL_H
#include <QAbstractProxyModel>
#include "File_List_Model.h"
class File_Proxy_Model: public QAbstractProxyModel
{
public:
    explicit File_Proxy_Model(File_List_Model* source_model)
{
    setSourceModel(source_model);
}

    virtual QModelIndex mapFromSource(const QModelIndex & sourceIndex) const
    {
        return index(sourceIndex.row(),sourceIndex.column());
    }

    virtual QModelIndex mapToSource(const QModelIndex & proxyIndex) const
    {

            return index(proxyIndex.row(),proxyIndex.column());

    }



    virtual int columnCount(const QModelIndex & parent = QModelIndex()) const
    {
        return sourceModel()->columnCount();
    }
    virtual int rowCount(const QModelIndex & parent = QModelIndex()) const
    {
        return sourceModel()->rowCount();
    }

    virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
    {
        return createIndex(row,column);
    }
    virtual QModelIndex parent(const QModelIndex & index) const
    {
        return QModelIndex();
    }

};

#endif // FILES_PROXY_MODEL_H

//and this is a dialog class:  
Line_Counter::Line_Counter(QWidget *parent) :
    QDialog(parent), model_(new File_List_Model(this)),
                     proxy_model_(new File_Proxy_Model(model_)),
                     sel_model_(new QItemSelectionModel(proxy_model_,this))
{
    setupUi(this);

    setup_mvc_();

}

void Line_Counter::setup_mvc_()
{
    listView->setModel(proxy_model_);
    listView->setSelectionModel(sel_model_);
}
\ifndef文件\u代理\u模型\u H
#定义文件\u代理\u模型\u H
#包括
#包括“文件\u列表\u Model.h”
类文件\u代理\u模型:公共QAbstractProxyModel
{
公众:
显式文件\代理\模型(文件\列表\模型*源\模型)
{
设置源模型(源模型);
}
虚拟QModelIndex mapFromSource(常量QModelIndex和sourceIndex)常量
{
返回索引(sourceIndex.row(),sourceIndex.column());
}
虚拟QModelIndex映射源(常量QModelIndex&proxyIndex)常量
{
返回索引(proxyIndex.row(),proxyIndex.column());
}
虚拟int列计数(常量QModelIndex&parent=QModelIndex())常量
{
返回sourceModel()->columnCount();
}
虚拟整数行数(常量QModelIndex&parent=QModelIndex())常量
{
返回sourceModel()->rowCount();
}
虚拟QModelIndex索引(int行,int列,常量QModelIndex&parent=QModelIndex())常量
{
返回createIndex(行、列);
}
虚拟QModelIndex父对象(常量QModelIndex&index)常量
{
返回QModelIndex();
}
};
#endif//FILES\u PROXY\u MODEL\H
//这是一个对话类:
行计数器::行计数器(QWidget*父项):
QDialog(父级)、model(新文件列表)和model(this)),
代理模型(新文件代理模型(模型)),
选择模型(新QItemSelectionModel(代理模型,本))
{
setupUi(本);
安装程序(mvc);
}
无效行\计数器::设置\ mvc \()
{
listView->setModel(代理模型);
listView->setSelectionModel(选择模型);
}

mapToSource
应从源模型返回索引:

virtual QModelIndex mapToSource(const QModelIndex & proxyIndex) const
{
    return sourceModel()->index(proxyIndex.row(),proxyIndex.column());
}
我使用该代码进行了测试:

#include <QtGui>
#include "file_proxy_model.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QDir dir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
    File_List_Model model(dir.entryList());

    File_Proxy_Model proxy(&model);

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

    return a.exec();
}

// In "File_List_Model.h"
class File_List_Model : public QStringListModel
{
public:
    explicit File_List_Model(const QStringList & list, QObject *parent = 0)
        : QStringListModel(list, parent)
    {
    }
};
#包括
#包括“文件\代理\模型.h”
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
QDir目录(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
文件列表模型(dir.entryList());
文件\代理\模型代理(&Model);
QListView列表视图;
setModel(&proxy);
show();
返回a.exec();
}
//在“文件\u列表\u Model.h”中
类文件列表模型:公共QStringListModel
{
公众:
显式文件列表模型(常量QStringList&List,QObject*parent=0)
:QStringListModel(列表,父级)
{
}
};

@smallB:即使没有这个修复,listview似乎也会显示这些项目(但它们都是禁用的/灰色的)。所以问题可能出在你的源模型上。你是说你已经将代理模型插入到源模型中,并且让它们工作了吗?如果是的话,你能把这个代码贴出来让我看看吗?谢谢