5 Attachment(s)
Cannot drag children of QTreeView when first column is hidden
Hi,
I've implemented a tree model on top of a QSqlTableModel via a proxy model (QSqlTableModel doesn't work well with parent/child relationships). It works great, except when I hide the first column of the view (id column). I want to hide the id column because I don't want it in the view but once I do, dragging children does not work. Any thoughts? I haven't been able to make any progress on this. code attached and below. Thanks!
sqltreemodel.h
Code:
#define TREEMODEL_H
#include <QSqlTableModel>
{
Q_OBJECT
public:
sqlTreeModel
(QObject *parent
=nullptr
);
virtual Qt::DropActions supportedDragActions() const { return Qt::MoveAction; }
virtual Qt::DropActions supportedDropActions() const { return Qt::MoveAction; }
};
#endif // TREEMODEL_H
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlError>
class sqlTreeModel;
class proxyTreeModel;
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= nullptr
);
private:
sqlTreeModel *plidModel;
proxyTreeModel *proxyPlidModel;
QHash<QString,QPair<int,int> > playlistVideoTracker;
void setupModels();
};
Re: Cannot drag children of QTreeView when first column is hidden
Children in built-in Qt models are attached to the first column of the model thus when you hide it, the relationship breaks. A possible solution would be to put a proxy model on top of the sql model to reorder columns so that the id is not the first column. Or you can just remove the id column from the model using a proxy (or your existing QSqlTableModel subclass).
Re: Cannot drag children of QTreeView when first column is hidden
Thank you Grandmaster Wysota that did it!
I did implement a custom model (inherited QAbstractItemModel) and create my own tree item class to manage the parent/child relationship. Is the first column therefore used by the view or delegate?
Re: Cannot drag children of QTreeView when first column is hidden
I'm not sure I understand your question. If the column is visible then it is "used" by the view and the delegate. If it is hidden by the model itself, the view and the delegate know nothing about it.