-1

我需要存储一些表类型的数据,例如 aQTableWidget但没有 GUI。类似于以下代码的内容:

QMap<QString, QString, int, QString, int>

有没有办法在 Qt 中实现这一点?我的 Qt 版本是 5.3。

4

1 回答 1

1

You seem to be unclear on a few concepts.

A map (also known in some languages as a dictionary) is an associative array. It associates a key to a value, that's about it, there are no "fields" involved whatsoever, just a key and a value.

There is no data type in Qt to model a database table. For such tasks you usually directly use SQL, Qt supports SQL with various different database drivers.

If you don't want to use a database but instead want to have "native" C++ types, you can simply create an object with all the desired fields:

struct Entry {
    QString s1, s2, s3;
    int i1, i2;
};

And then put that into whatever container you want.

QList<Entry> entryList;
QVector<Entry> entryVec;
QSet<Entry> entrySet;

You can wrap the container in a QAbstractListModel, implement the key functions and roles and have that model be used for a table widget or a QML view.

于 2015-04-15T17:53:15.657 回答