Extracting sub model from a another model? - c++

The following code is from a Qt demo. This is a model for QTreeView.
TreeItem class below represent each node in tree, it can have child nodes.
class TreeItem
{
public:
explicit TreeItem(const QList<QVariant> &data, TreeItem *parentItem = 0);
~TreeItem();
void appendChild(TreeItem *child);
TreeItem *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
TreeItem *parentItem();
private:
QList<TreeItem*> m_childItems;
QList<QVariant> m_itemData;
TreeItem *m_parentItem;
};
TreeModel class below is the main model. It only contains one root node which contain all other child nodes.
class TreeModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit TreeModel(const QString &data, QObject *parent = 0);
~TreeModel();
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QModelIndex parent(const QModelIndex &index) const Q_DECL_OVERRIDE;
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
private:
void setupModelData(const QStringList &lines, TreeItem *parent);
TreeItem *rootItem;
};
I am using QML and I can get this model displayed in tree however I also want to display in a ListView.
For the ListView, I only want to display one tier at one time (1st childrens). When user click on any of the item, it should clear and show that item children. How do I do that?
My Qml code is below. It displays all first tier children which is great but I need to show children when user clicks on an item. My thought is need extract the sub model and point to it, but how?
Item {
width: parent.width
height: parent.height
ListView {
//anchors.top: myImage.bottom
anchors.fill: parent
id: list
spacing: 4
model: treeModel
delegate: listDelegate
}
Component {
id: listDelegate
Rectangle
{
id: delegateRect
height: image.height
width: 500
Image {
id: image
source: "qrc:/icons/resources/element.ico"
}
Text {
id: t
anchors.left: image.right
anchors.leftMargin: 20
anchors.centerIn: delegateRect
text: title + "/" + summary
//text: display
}
MouseArea {
anchors.fill: parent
onClicked: {
list.currentIndex = index
console.log("Item clicked, index = " + index)
// I think I should change model here to sub model but how do I do it?
//list.model = treeModel.data(index)
}
}
}
}
}

You should have a look at the QAbstractProxyModel and derived classes in the Qt documentation http://doc.qt.io/qt-5/qabstractproxymodel.html.
A proxy model is used to map model indexes (if you want to modify the layout, or do sorting/filtering on data) and do data processing (modify the data returned from the source models data method).
What you need to do is to add a property to select the new root (the item that should be the root item of the extracted model) and implement the two methods mapFromSource(const QModelIndex &) and mapToSource(const QModelIndex &). These methods map the model index given to the view (and only valid in your proxy model) to a model index that is valid for the source model based on the currently set root property (and vice versa).
In addition you should also reimplement the roleNames() method to forward the rolenames defined by the source model to be able to access the data from inside QML.

Related

QML QT backend and GUI design - doesn't update the object

I have simple object with collection which is created and managed under C++ code and I want to let user view it and modify it from GUI (QML - presentation of the collection and add/remove commands), but lifetime and business logic should be managed by backend (C++)
class QtModel : public QAbstractListModel {
Q_OBJECT
public:
explicit QtModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index,
int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
void test();
private:
std::vector<std::shared_ptr<Data>> collection_;
};
where test method push a new element:
void QtModel::test(){
collection_.push_back(std::make_shared<Data>("test"));
}
I tried follow this way:
https://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html
And qml code just takes the current state of the object. The further modifications are ignored:
application = std::make_unique<QGuiApplication>((int &)argc, argv);
engine = std::make_shared<QQmlApplicationEngine>();
qt_model_.test(); // 1 element, GUI shows 1 element
engine->rootContext()->setContextProperty("myGlobalObject", &qt_model_);
// those are ignored
qt_model_.test(); // 2 elements, GUI shows 1 element
qt_model_.test(); // 3 elements, GUI shows 1 element
qt_model_.test(); // 4 elements, GUI shows 1 element
application->exec();
For presentation I am using GridLayout like this:
GridLayout {
anchors.fill: parent
flow: width > height ? GridLayout.LeftToRight : GridLayout.TopToBottom
Repeater {
model: myGlobalObject
delegate : Rectangle {
Layout.fillWidth: true
Layout.fillHeight: true
color: Style.appLightBackgroundColor
Label {
anchors.centerIn: parent
text: model.name
color: Style.fontDarkColor
}
}
}
}
So the object in QML is not updated, while the modifications are taken place after its registration in C++
You are not sending RowsInserted signals from the test function, so QML cannot know when to update. Please adjust like so:
void QtModel::test(){
beginInsertRows({}, collection_.size(), collection_.size() + 1);
collection_.push_back(std::make_shared<Data>("test"));
endInsertRows();
}

Wrapping a QStringListModel in a QAbstractItemModel renders a blank list

I want to start off making my own models for Qt list views, and I thought that I would start by wrapping a QStringListModel in my own QAbstractItemModel, then render it in a list view. However, it only renders a blank white square, instead of the list I expect. I don't really know what could be happening, given that all I'm doing is delegating all calls to the QStringListModel. Perhaps there's some aspects of the QStringListModel that are called by the QListView that are not mandated by the QAbstractItemModel pure virtual methods? Or maybe it's related to storage of the QStringList somehow?
My attempt is below. The header:
class DelegatingItemModel: public QAbstractItemModel {
public:
DelegatingItemModel();
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
private:
QAbstractItemModel* innerModel;
};
This is the implementation:
#include "delegating_item_model.hh"
DelegatingItemModel::DelegatingItemModel() {
QStringList available = {"foo", "bar", "baz"};
this->innerModel = new QStringListModel(available);
}
QVariant DelegatingItemModel::data(const QModelIndex &index, int role) const {
return innerModel->data(index, role);
}
int DelegatingItemModel::columnCount(const QModelIndex &parent) const {
return innerModel->columnCount(parent);
}
int DelegatingItemModel::rowCount(const QModelIndex &parent) const {
return innerModel->rowCount(parent);
}
QModelIndex DelegatingItemModel::parent(const QModelIndex &index) const {
return innerModel->parent(index);
}
QModelIndex DelegatingItemModel::index(int row, int column, const QModelIndex &parent) const {
return innerModel->index(row, column, parent);
}
And this is the entry point:
int main(int argc, char** argv) {
qDebug() << "Starting up";
QApplication app(argc, argv);
QMainWindow mainWindow;
QListView* listView = new QListView;
DelegatingItemModel* theModel = new DelegatingItemModel;
listView->setModel(theModel);
mainWindow.setCentralWidget(listView);
mainWindow.show();
return app.exec();
}
Your view will get data from the model only if the given index is linked to its model. If you print a trace in the data() method, you will see that it's never called.
So, you cannot return a new index created by your inner list model because it will be linked to the list and not your own model. For example:
QModelIndex DelegatingItemModel::index(int row, int column, const QModelIndex &parent) const {
//return innerModel->index(row, column, parent);
if (parent.isValid()) // It's a list. Not a tree
return QModelIndex();
return createIndex(row, column); // Create a index for your own model.
}
To be full compliant, you should convert the index in data():
QVariant DelegatingItemModel::data(const QModelIndex &index, int role) const {
QModelIndex const innerIndex(innerModel->index(index.row(), index.column()));
return innerModel->data(innerIndex, role);
}

Qml Listview items disappear when scrolling

I have got the following scrollview with listview inside:
ScrollView{
anchors.fill: parent
ListView{
id: lvCommitsBranch
model: git.getCommitsBranch();
clip: true
delegate: Rectangle {
height: 100
width: parent.width
Text {
anchors.left: parent.left
font.bold: true
text:model.author
id:txtName
}
Text{
anchors.left: parent.left
anchors.top:txtName.bottom
font.pixelSize: 10
text:model.email
id: txtEmail
}
Text {
anchors.left: parent.left
anchors.top:txtEmail.bottom
text: model.message + ' ' + model.hash
id: txtMsg
}
MouseArea{
anchors.fill: parent
onClicked: {
lvCommitsBranch.currentIndex = index;
console.log('Msg: ' + model.message);
console.log('Hash: ' + model.hash);
}
acceptedButtons: Qt.LeftButton | Qt.RightButton
}
}
}
}
The issue is that when I scroll some items disappear (each time randomly and sometimes I have to scroll fast but not always).
When I click on the items that have not disappeared, I get undefined on all the model's properties. When Mousearea's onclick is triggered it prints the following:
qml: Msg: undefined
qml: Hash: undefined
I get the model info from a method (QAbstractListModel) that is returned from my git custom component.
This is my QAbstractListModel:
header:
class CommitsBranch : public QAbstractListModel
{
Q_OBJECT
public:
enum Roles {
AuthorRole,
EMailRole,
MsgRole,
DateRole,
HashRole
};
explicit CommitsBranch(QObject *parent = 0);
CommitsBranch(Repository *repo);
public:
virtual int rowCount(const QModelIndex &parent) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
protected:
// return the roles mapping to be used by QML
virtual QHash<int, QByteArray> roleNames() const override;
private:
QList<Commit> m_data;
QHash<int, QByteArray> m_roleNames;
};
Cpp:
CommitsBranch::CommitsBranch(QObject *parent)
: QAbstractListModel(parent)
{
}
CommitsBranch::CommitsBranch(Repository *repo)
{
m_roleNames[AuthorRole] = "author";
m_roleNames[EMailRole] = "email";
m_roleNames[MsgRole] = "message";
m_roleNames[DateRole] = "date";
m_roleNames[HashRole] = "hash";
/*
here we append the m_data (QList) Items using libgit2 methods
*/
}
int CommitsBranch::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_data.count();
}
QVariant CommitsBranch::data(const QModelIndex &index, int role) const
{
// this function returns the required data
}
QHash<int, QByteArray> CommitsBranch::roleNames() const
{
return m_roleNames;
}
And git is just a class that inherits from QObject and it has the following method:
Q_INVOKABLE QObject* getCommitsBranch();
QObject *Git::getCommitsBranch()
{
CommitsBranch* files = new CommitsBranch(repo.data());
return files;
}
I get the same behavior without the scrollview.
EDIT:
If I take a repository with a lot of commits (more lines to the listview), even increasing the cacheBuffer won't help, if I scroll a bit fast all the items will disappear.
The problem here is that, by default, if you return a QObject* it will transfer the ownership to QML.
http://doc.qt.io/qt-5/qtqml-cppintegration-data.html#data-ownership
The exception to this rule is when a QObject is returned from an
explicit C++ method call: in this case, the QML engine assumes
ownership of the object, unless the ownership of the object has
explicitly been set to remain with C++ by invoking
QQmlEngine::setObjectOwnership() with QQmlEngine::CppOwnership
specified.
You have to set the returned QObject* ownership manually, so it doesn't get destroyed by the QML engine :
QObject *Git::getCommitsBranch()
{
CommitsBranch* files = new CommitsBranch(repo.data());
QQmlEngine::setObjectOwnership(files, QQmlEngine::CppOwnership)
return files;
}
Note that you will have a memory leak as your CommitsBranch object will never be deleted. But at least your QML items should not disappear anymore !
EDIT: As suggested you can do something like this to avoid the memory leak :
// CommitsBranch Constructor
CommitsBranch::CommitsBranch(Repository *repo, QObject *parent) :
QAbstractListModel(parent) { /*stuff*/ }
QObject *Git::getCommitsBranch()
{
// Setting ownership is not necessary if you pass the parent to the QAbstractListModel
CommitsBranch* commits = new CommitsBranch(repo.data(), this);
return files;
}

Bind CheckBox checked-state in TableView to custom model attribute

I've got a QML-application containing a TableView with two columns. One of them is a CheckBox. Now I created a model derived from QAbstractTableModel. Reading data for the ordinary text-column already works but how do I sync the checked-property for my CheckBox with the model?
Currently I can't even set it checked via model. You find the relevant code below.
tablemodel.cpp
TableModel::TableModel(QObject *parent) :
QAbstractTableModel(parent)
{
list.append("test1");
list.append("test2");
}
int TableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return list.count();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant TableModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= list.count())
return QVariant();
if (role == NameRole)
return list[index.row()]
else if (role== EnabledRole){
//list is not QList<QString>, its a custom class saving a String and a boolean for the checked state
return list[index.row()].isEnabled();
}
else {
return QVariant();
}
}
QHash<int, QByteArray> TableModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[EnabledRole] = "enabled";
return roles;
}
tablemodel.hpp
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Roles {
NameRole = Qt::UserRole + 1,
EnabledRole
};
explicit TableModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex & parent = QModelIndex()) const;
Q_INVOKABLE QVariant data (const QModelIndex & index, int role) const;
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<QString> list;
main.qml
TableView {
id: Table
model: TableModel
TableViewColumn {
role: "name"
}
TableViewColumn {
role: "enabled"
delegate: CheckBox {
//how to get the right state from the model
//checked: ??
}
}
}
main.cpp
QQmlApplicationEngine engine;
QQmlContext * context = new QQmlContext(engine.rootContext());
TableModel tableModel;
context->setContextProperty("tableModel",&tableModel);
QQmlComponent component(&engine, QUrl("qrc:///main.qml"));
QQuickWindow * window = qobject_cast<QQuickWindow*>(component.create(context));
window->show();
You can emit signal from qml, when clicked on checkbox; connect this signal to your model slot and do something
main.qml
TableView {
id: table
objectName: "myTable"
signal checkedChanged(int index, bool cheked)
TableViewColumn {
role: "enabled"
delegate: CheckBox {
onClicked: table.checkedChanged(styleData.row, checked);
}
}
main.cpp
QQuickItem *obj = engine.rootObjects().at(0)->findChild<QQuickItem*>(QStringLiteral("myTable"));
QObject::connect(obj,SIGNAL(checkedChanged(int,bool)),tableModel,SLOT(mySlot(int,bool)));

Data is not displayed in TableView

I try to build simple TableView with model from C++. My table has as many rows and columns, as return rowCount and columnCount method. This means, model is 'connected' with view, but in each cell does not display the message: 'Some data'
here is my code:
class PlaylistModel : public QAbstractTableModel
{
Q_OBJECT
public:
PlaylistModel(QObject *parent=0): QAbstractTableModel(parent), rows(0){}
int rowCount(const QModelIndex & /*parent*/) const
{
return 5;
}
int columnCount(const QModelIndex & /*parent*/) const
{
return 3;
}
QModelIndex index(int row, int column, const QModelIndex &parent) const {
return createIndex(row, column);
}
QModelIndex parent(const QModelIndex &child) const {
return child.parent();
}
QVariant data(const QModelIndex &index, int role) const{
if (role == Qt::DisplayRole)
{
return QString("Some data");
}
return QVariant();
}
(...)
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
PlaylistModel plModel(0);
engine.rootContext()->setContextProperty("myModel", &plModel);
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
and qml
TableView {
id: trackList
width: 100; height: 100
model: myModel
TableViewColumn { role: "id"; title: "Id"; width: 30 }
TableViewColumn { role: "name"; title: "Name"; width: 100}
TableViewColumn { role: "duration"; title: "Duration"; width: 20 }
}
Where I make a mistake?
Short answer
Because your QML is not asking for Qt::DisplayRole. Change your TableVievColumn to
TableViewColumn { role: "display"; title: "xxx"; width: 20 }
The QML is now asking for Qt::DisplayRole, and "Some data" is shown in this column.
Long answer
QML is asking for three user-defined roles: "id", "name", and "duration". However, the three roles are not bulit-in roles. Therefore you need to implement the three roles in your model class.
First, you should provide a set of roles to the model. The model returns data to views using QAbstractItemModel::data function. The type of role is int, we can write an enum in the model class:
class PlaylistModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum MyTableRoles
{
IdRole = Qt::UserRole + 1,
NameRole,
DurationRole
}
//...
};
Now, in the data function, returns the corresponding value any time the view is asking:
QVariant PlaylistModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()){return QVariant();}
switch(role)
{
case IdRole:
return GetIdFromMyTable(index);
case NameRole:
return GetNameFromMyTable(index);
case DurationRole:
return GetDurationFromMyTable(index);
}
//...
return QVariant();
}
Next, provide a string-to-int mapping for each role. The role in the model is in type of int, however in the QML the role is type of string. (see the role property in TableViewColumn.) Therefore we should provide a string-to-int mapping for each role so the QML can correctly asking for the required data. The mapping should be provided in QAbstractItemModel::roleNames():
QHash<int, QByteArray> PlaylistModel::roleNames() const
{
QHash<int, QByteArray> roleNameMap;
roleNameMap[IdRole] = "id";
roleNameMap[NameRole] = "name";
roleNameMap[DurationRole] = "duration";
return roleNameMap;
}
Finally your table in QML now can display the things you want.
Some references
When subclassing QAbstractTableModel,
you must implement rowCount(), columnCount(), and data(). Default implementations of the index() and parent() functions are provided by QAbstractTableModel.
When using C++ models in QML,
The roles of a QAbstractItemModel subclass can be exposed to QML by reimplementing QAbstractItemModel::roleNames().
And if you do not reimplement the roleNames function, you can use only the default roles declared in QAbstractItemModel::roleNames. And this is the reason why the short answer works.
You did not implemented index method.
According to documentation,
When subclassing QAbstractItemModel, at the very least you must
implement index(), parent(), rowCount(), columnCount(), and data().
These functions are used in all read-only models, and form the basis
of editable models.
in other words, You have to implement your own low-level item management, AbstractItemModel will not do it for you. You should create indexes with createIndex and destroy them when nesessary etc.
If you don't want to play these games and just want to implement your own quick&dirty model, consider subclassing QStandardItemModel.