使用 TableView 作为 ListView-delegate
Using TableView as ListView-delegate
我有一个 model
,其中包含几个 QStandardItemModels
。现在我想创建一个视图,为每个 QStandardItemModel
显示一个 TableView
。
我的想法是有一个 ListView
有一个 TableView
作为代表,像这样:
ListView {
id: myListView
anchors {
fill: parent
margins: 5
}
model: testModel
delegate: CompareDelegate { }
}
在CompareDelegate.qml
中:
Item {
id: base
width: 500
height: 300
TableView {
anchors.fill: parent
}
}
如何让委托中的 TableView
在 ListView
的模型中使用适当的 QStandardItemModel
?
我希望这个问题有点清楚。
提前致谢。
有趣的用例...我仍然不确定这是否是个好主意,想知道是否有更好的方法,但我想不出它不好的原因,所以我试了一下好奇心:
main.cpp
#include <QApplication>
#include <QtQml>
#include <QtWidgets>
class IndividualModel : public QStandardItemModel
{
public:
IndividualModel(QObject* parent = 0) :
QStandardItemModel(4, 2, parent)
{
for (int row = 0; row < 4; ++row) {
QStandardItem *item0 = new QStandardItem;
item0->setData(QString("row %1, column 0").arg(row), TitleRole);
setItem(row, 0, item0);
QStandardItem *item1 = new QStandardItem;
item1->setData(QString("row %1, column 1").arg(row), AuthorRole);
setItem(row, 1, item1);
}
}
enum {
TitleRole = Qt::UserRole,
AuthorRole
};
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE
{
QHash<int, QByteArray> names;
names[TitleRole] = "title";
names[AuthorRole] = "author";
return names;
}
};
class CompositeModel : public QAbstractItemModel
{
public:
CompositeModel()
{
for (int i = 0; i < 3; ++i) {
QStandardItemModel *model = new IndividualModel(this);
mModels.append(model);
}
}
enum {
ModelRole = Qt::UserRole
};
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE
{
if (!index.isValid())
return QVariant();
if (role != ModelRole)
return QVariant();
return QVariant::fromValue(mModels.at(index.row()));
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
QModelIndex parent(const QModelIndex &) const Q_DECL_OVERRIDE
{
return QModelIndex();
}
int rowCount(const QModelIndex & = QModelIndex()) const Q_DECL_OVERRIDE
{
return mModels.size();
}
int columnCount(const QModelIndex & = QModelIndex()) const Q_DECL_OVERRIDE
{
return 1;
}
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE
{
QHash<int, QByteArray> names;
names[ModelRole] = "individualModel";
return names;
}
private:
Q_DISABLE_COPY(CompositeModel)
QVector<QStandardItemModel*> mModels;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
CompositeModel compositeModel;
engine.rootContext()->setContextProperty("compositeModel", QVariant::fromValue(&compositeModel));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.0
Window {
visible: true
width: 300
height: 600
ListView {
anchors.fill: parent
model: compositeModel
delegate: TableView {
model: individualModel
TableViewColumn {
role: "title"
title: "Title"
width: 100
}
TableViewColumn {
role: "author"
title: "Author"
width: 200
}
}
}
}
这个有效,但是...它在退出时崩溃。 :)
我在使用 Creator 调试时无法重现它,所以我只能在命令行上使用 gdb
手动获取堆栈跟踪,我不太熟悉(一直在 IDE 中使用调试器 :))。崩溃发生在 QML 的某个地方,同时访问了一些 属性。但是,我在这上面花了太多时间,没有 post 答案,我认为它仍然有用。也许好心人会发现问题并编辑我的 post。 :)
我有一个 model
,其中包含几个 QStandardItemModels
。现在我想创建一个视图,为每个 QStandardItemModel
显示一个 TableView
。
我的想法是有一个 ListView
有一个 TableView
作为代表,像这样:
ListView {
id: myListView
anchors {
fill: parent
margins: 5
}
model: testModel
delegate: CompareDelegate { }
}
在CompareDelegate.qml
中:
Item {
id: base
width: 500
height: 300
TableView {
anchors.fill: parent
}
}
如何让委托中的 TableView
在 ListView
的模型中使用适当的 QStandardItemModel
?
我希望这个问题有点清楚。
提前致谢。
有趣的用例...我仍然不确定这是否是个好主意,想知道是否有更好的方法,但我想不出它不好的原因,所以我试了一下好奇心:
main.cpp
#include <QApplication>
#include <QtQml>
#include <QtWidgets>
class IndividualModel : public QStandardItemModel
{
public:
IndividualModel(QObject* parent = 0) :
QStandardItemModel(4, 2, parent)
{
for (int row = 0; row < 4; ++row) {
QStandardItem *item0 = new QStandardItem;
item0->setData(QString("row %1, column 0").arg(row), TitleRole);
setItem(row, 0, item0);
QStandardItem *item1 = new QStandardItem;
item1->setData(QString("row %1, column 1").arg(row), AuthorRole);
setItem(row, 1, item1);
}
}
enum {
TitleRole = Qt::UserRole,
AuthorRole
};
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE
{
QHash<int, QByteArray> names;
names[TitleRole] = "title";
names[AuthorRole] = "author";
return names;
}
};
class CompositeModel : public QAbstractItemModel
{
public:
CompositeModel()
{
for (int i = 0; i < 3; ++i) {
QStandardItemModel *model = new IndividualModel(this);
mModels.append(model);
}
}
enum {
ModelRole = Qt::UserRole
};
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE
{
if (!index.isValid())
return QVariant();
if (role != ModelRole)
return QVariant();
return QVariant::fromValue(mModels.at(index.row()));
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
QModelIndex parent(const QModelIndex &) const Q_DECL_OVERRIDE
{
return QModelIndex();
}
int rowCount(const QModelIndex & = QModelIndex()) const Q_DECL_OVERRIDE
{
return mModels.size();
}
int columnCount(const QModelIndex & = QModelIndex()) const Q_DECL_OVERRIDE
{
return 1;
}
QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE
{
QHash<int, QByteArray> names;
names[ModelRole] = "individualModel";
return names;
}
private:
Q_DISABLE_COPY(CompositeModel)
QVector<QStandardItemModel*> mModels;
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
CompositeModel compositeModel;
engine.rootContext()->setContextProperty("compositeModel", QVariant::fromValue(&compositeModel));
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
main.qml
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.0
Window {
visible: true
width: 300
height: 600
ListView {
anchors.fill: parent
model: compositeModel
delegate: TableView {
model: individualModel
TableViewColumn {
role: "title"
title: "Title"
width: 100
}
TableViewColumn {
role: "author"
title: "Author"
width: 200
}
}
}
}
这个有效,但是...它在退出时崩溃。 :)
我在使用 Creator 调试时无法重现它,所以我只能在命令行上使用 gdb
手动获取堆栈跟踪,我不太熟悉(一直在 IDE 中使用调试器 :))。崩溃发生在 QML 的某个地方,同时访问了一些 属性。但是,我在这上面花了太多时间,没有 post 答案,我认为它仍然有用。也许好心人会发现问题并编辑我的 post。 :)