PDA

View Full Version : Good way to make a standard tree model for develop CLI and GUI application



madawg
12th November 2018, 11:14
I used QStandardModelItemModel for treeview show in GUI.
But I can't using QStandardModelItemModel for develop CLI app.
I want to create subclass of QAbstractItemModel for both TreeView and Model of CLI app.
I saw simple tree model (http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-treemodel-cpp.html), but It is still difficult to modify for tree model, example: edit model, set, get property of item,....
Where or How to help me make StandardTreeModel which I can use to devboth GUI app and CLI app

ChrisW67
13th November 2018, 01:06
But I can't using QStandardModelItemModel for develop CLI app.

Why not?


#include <QCoreApplication>
#include <QStandardItemModel>
#include <QDebug>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
qDebug() << "No GUI here";

QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model.setItem(row, column, item);
}
}

for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QModelIndex index = model.index(row, column);
qDebug() << index.data();
}
}

return 0;
}

madawg
13th November 2018, 01:45
because I want to develop CLI app
in .pro file, I set
QT-= GUI
Because QStandardModelItemModel is a GUI class

ChrisW67
13th November 2018, 04:42
Yes, QStandardItemModel requires the QtGui library but it does not require you to use a GUI. You can have a usable command-line only application and use the standard model. It may have issues running in an environment without X though... haven't tried.

You state you want a QAbstractItemModel interface. That interface is in QtCore so you can do this without QtGui. Take a look at Editable Tree Model Example (http://doc.qt.io/qt-5/qtwidgets-itemviews-editabletreemodel-example.html). It is not trivial to fully implement a model, but the example goes through a working internal data structure and extensions for editing.