Results 1 to 4 of 4

Thread: Cannot drag children of QTreeView when first column is hidden

  1. #1
    Join Date
    Nov 2017
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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

    Qt Code:
    1. #define TREEMODEL_H
    2.  
    3. #include <QSqlTableModel>
    4.  
    5. class sqlTreeModel : public QSqlTableModel
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. sqlTreeModel(QObject *parent=nullptr);
    11.  
    12. virtual Qt::DropActions supportedDragActions() const { return Qt::MoveAction; }
    13. virtual Qt::DropActions supportedDropActions() const { return Qt::MoveAction; }
    14. };
    15.  
    16. #endif // TREEMODEL_H
    To copy to clipboard, switch view to plain text mode 


    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QSqlError>
    6.  
    7. class QTreeView;
    8. class sqlTreeModel;
    9. class proxyTreeModel;
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = nullptr);
    17.  
    18. private:
    19. QTreeView *treeView;
    20.  
    21. sqlTreeModel *plidModel;
    22. proxyTreeModel *proxyPlidModel;
    23. QHash<QString,QPair<int,int> > playlistVideoTracker;
    24.  
    25. QSqlError initdb();
    26. void setupModels();
    27. };
    28.  
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2017
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 7
    Last Post: 4th April 2015, 09:18
  2. Showing a Hidden QTableView Column
    By dchow in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2011, 01:11
  3. Replies: 2
    Last Post: 21st October 2009, 09:13
  4. Model/View one index.column as hidden UserRole
    By doitux in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2008, 13:08
  5. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 09:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.