PDA

View Full Version : Cannot drag children of QTreeView when first column is hidden



kazaak
20th August 2018, 00:32
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


#ifndef TREEMODEL_H12947129481294912950
#define TREEMODEL_H

#include <QSqlTableModel>

class sqlTreeModel : public 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

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSqlError>

class QTreeView;
class sqlTreeModel;
class proxyTreeModel;

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = nullptr);

private:
QTreeView *treeView;

sqlTreeModel *plidModel;
proxyTreeModel *proxyPlidModel;
QHash<QString,QPair<int,int> > playlistVideoTracker;

QSqlError initdb();
void setupModels();
};

#endif // MAINWINDOW_H129411294212943129441294512946

wysota
20th August 2018, 09:39
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).

kazaak
20th August 2018, 15:06
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?

wysota
20th August 2018, 21:44
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.