QAbstractTableModel在数据更改后检索自定义对象 [英] QAbstractTableModel retrieve custom object on data changed

查看:268
本文介绍了QAbstractTableModel在数据更改后检索自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近再次拿起Qt,并开始刷新我的记忆。
为表创建自定义数据模型非常容易。

I have recently picked up Qt again, and started refreshing my memory. Creating a custom data model for a table was easy enough.

现在,我正在尝试检索选定的数据。
请注意,我使用了自定义数据对象。

Now I am trying to retrieve the selected data. Take note that I use custom data objects.

自定义模型的示例:

平台.h

class Platform
{
public:
    Platform();
    Platform(QString name);
    QString getName();
    void setName(QString name);
private:
    QString m_name;
};

用于测试目的的非常简单的数据结构。
然后我实现了一个QAbstractTableModel,Data()方法如下:

Very simple data structure for testing purposes. I then implemented a QAbstractTableModel, the Data() method looks like this:

platformmodel.cpp

platformmodel.cpp

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

    if (index.row() >= m_platforms.size() || index.row() < 0)
        return QVariant();

    if (role == Qt::DisplayRole) {
        Platform platform = m_platforms.at(index.row());
        qDebug() << platform.getName();
        return platform.getName();
    }
    return QVariant();
}

我从这段代码中了解到,对于可选项目,一个字符串总是返回,而不是平台对象。

What I understand from this code is, that for the selectable items, a String is always returned, instead of a platform object.

对于显示,它工作正常,我在视图中看到了实际的对象。
现在,我想从模型中选择实际的对象,而不仅仅是QString。

For displaying, this works fine, I see the actual objects in the view. Now I want to select the actual object from the model, and not just a QString.

所以方法体类似于:

void MainWindow::selectionChangedSlot(const QItemSelection &, const QItemSelection &)
{
    //get the text of the selected item
    const QModelIndex index = ui->lvPlatforms->selectionModel()->currentIndex();
    Platform selectedPlatform = index.data();//This returns a QVariant and will fail at compile time, but I want to achieve something along this line.
    setWindowTitle(selectedPlatform.getName());
}

P.s也许我正在尝试搜索错误的东西,可以找到使用自定义对象的示例,但是没有人谈论检索选定的项目。

P.s. Maybe I am trying to search on the wrong thing, I can find examples that use custom objects, but none talk about retrieving the selected item.

必须有一个更好的选择然后取回字符串,然后循环通过平台列表并将名称与所选项目进行比较。如果我的列表很大,则必须循环通过每个项目并进行字符串比较不是很有效。

There has to be a better way then retreiving the string, then looping trough the list of platforms and comparing the name to the selected item.. If i have a big list, having to loop trough each item and do string comparison is not very efficient.

我希望我的问题很清楚。如果缺少重要的内容,请告诉我,以便我可以编辑示例。

I hope my problem is clear enough. If something important lacks, let me know so I can edit my example.

我尝试了Q_DECLARE_METATYPE(平台);

I tried Q_DECLARE_METATYPE(Platform);

是的,它可以将其存储在QVariant中,
的问题是,对于显示,字符串始终是预期的,还是9/10倍。
到目前为止,似乎既不能同时显示文本,又不能从选择模型中获得完整的平台对象(我可以单独完成这两个操作。..非常没用..)

And yes it works, it makes it possible to store it in a QVariant, the problem is, since for displaying, a String is always expected, or 9/10 times anyway. So far it seems impossible to have both text display AND get the full platform object from the selection model(i can do both individually.. pretty useless..)

推荐答案

您可以使用宏Q_DECLARE_METATYPE创建与QVariant兼容的自定义类型。
如果您将类声明为元类型,则可以将其存储在QVariant中,并使用强制转换将其提取。

You can create custom type compatible with QVariant using the macro Q_DECLARE_METATYPE. If you declare your class as a metatype, you can store it in a QVariant and extract it with a cast.

以下示例显示了如何创建可以使用QVariant显示自定义类的数据的自定义委托:

Here an example that show how to create a custom delegate which can display data from a custom class using QVariant :

class Data {
private:
    QString name;
    int value;
public:
    Data() : name(""), value(-1){}
    Data( QString n, int v ) : name(n), value(v){}
    QString text() {
        return QString( "Test %1 - %2" ).arg( name ).arg( value );
    }
};

Q_DECLARE_METATYPE( Data )

class Delegate : public QStyledItemDelegate {
protected:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        Data d = index.data().value<Data>();
        painter->drawText( option.rect, d.text() );
    }
};


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

    QVariant var0, var1, var2;
    var0.setValue(Data( "Item A", 0 ));
    var1.setValue(Data( "Item B", 1 ));
    var2.setValue(Data( "Item C", 2 ));

    QListView *view = new QListView();
    QStandardItemModel model(3, 1);

    model.setData( model.index( 0, 0 ), var0 );
    model.setData( model.index( 1, 0 ), var1 );
    model.setData( model.index( 2, 0 ), var2 );
    view->setModel( &model );
    view->show();
    view->setItemDelegate( new Delegate() );
    return app.exec();
}

这篇关于QAbstractTableModel在数据更改后检索自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆