Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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++ Qt-QVector和模型视图-what';从listView获取自定义类的最佳方法是什么?_C++_Qt_Listview_Vector_Model - Fatal编程技术网

C++ Qt-QVector和模型视图-what';从listView获取自定义类的最佳方法是什么?

C++ Qt-QVector和模型视图-what';从listView获取自定义类的最佳方法是什么?,c++,qt,listview,vector,model,C++,Qt,Listview,Vector,Model,我有一个名为Foo的自定义类,我将该类的实例存储在一个向量中: class Foo{ public: QString name; int second_property; } //... QVector<Foo> foos = {test1, test2, test3}; 现在,在我单击UI中的项目后,我希望能够访问整个Foo类及其属性和方法,因此我使用循环查找我的对象: void MainWindow::on_l

我有一个名为
Foo
的自定义类,我将该类的实例存储在一个向量中:

    class Foo{
    public:
       QString name;
       int second_property;
    }
    //...
    QVector<Foo> foos = {test1, test2, test3};
现在,在我单击UI中的项目后,我希望能够访问整个
Foo
类及其属性和方法,因此我使用循环查找我的对象:

void MainWindow::on_listView_clicked(const QModelIndex &index)
{
    foreach(Foo foo, foos){
        if(foo.name==index.data().toString()){
           qDebug()<<"You found the object! Second property: " + foo.second_property);
        }
    }
}
我仍然使用循环使用
QVector foos
name
属性填充此模型:

    myOwnModel* myModel = new myOwnModel();
    for(int row=0; row<foos.size(); row++){
        myModel->insertRows(row,1);
        QModelIndex index = myModel->index(row,0);
        myModel->setData(index,foos[row].name);
    }
    ui->view->setModel(myModel);
myOwnModel*myModel=newmyownmodel();
for(int行=0;rowinsertRows(行,1);
QModelIndex index=myModel->index(行,0);
myModel->setData(索引,foos[row].name);
}
ui->view->setModel(myModel);
然后我仍然使用循环来获得所需对象的第二个属性:

    void MainWindow::on_view_clicked(const QModelIndex &index)
    {
        for(int i=0;i<foos.size();i++){
            if(index.data().toString()==foos[i].name){
                qDebug() << "You found " << foos[i].name + " and the second property is " << foos[i].second_property;
            }
        }
    }
void主窗口::在视图上单击(常量QModelIndex和索引)
{
对于(int i=0;i,使用QStandardItemModel几乎总是一个坏主意。您在QVector和Model中复制数据。保持数据一致变得非常困难


更好的方法是改用。

我使用QabStretchListModel并编辑了我的帖子来显示我的结果。你能看一下吗?@FilthCasual这取决于你从哪里获取数据。通常你从文件或数据库等获取数据,但不是从你刚刚创建的向量获取数据。尽管你可以将数据存储在向量中(或任何其他容器)在您的模型中。在
main窗口::在_视图上单击
,您不需要迭代模型。您应该使用
myOwnModel::data()
来获取所需的数据。
 class myOwnModel : public QAbstractListModel
{
    Q_OBJECT
public:
    myOwnModel(const QStringList &strings={""}, QObject *parent = nullptr);


    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role) const override;

    Qt::ItemFlags flags(const QModelIndex &index) const;
    bool setData(const QModelIndex &index, const QVariant &value,
    int role = Qt::EditRole);
    bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());

private:
     QStringList stringList;
};
    myOwnModel* myModel = new myOwnModel();
    for(int row=0; row<foos.size(); row++){
        myModel->insertRows(row,1);
        QModelIndex index = myModel->index(row,0);
        myModel->setData(index,foos[row].name);
    }
    ui->view->setModel(myModel);
    void MainWindow::on_view_clicked(const QModelIndex &index)
    {
        for(int i=0;i<foos.size();i++){
            if(index.data().toString()==foos[i].name){
                qDebug() << "You found " << foos[i].name + " and the second property is " << foos[i].second_property;
            }
        }
    }