Results 1 to 2 of 2

Thread: QTreeView and custom model: Problems while selecting items

  1. #1

    Default QTreeView and custom model: Problems while selecting items

    Dear all,

    I have been stuck with this problem for a week now, and I am not able to solve it.

    The situation:
    I am writing a patient manager for an annotation tool. In this manager I have a QTreeview with a custom model derived from QAbstractItemModel. The model contains maximum of two layers. The first layer is a spanned row with information on the patient. The patient can be expanded to show a number of events related to that patient, which is shown as a 5-column row containing information on that event. The data comes from a SQLite database, and is stored in a custom-made tree structure (non QT objects), with three layers (a root, patients and examinations). Rows (both patients and events) can be selected on a single basis (i.e. QAbstractItemView::SingleSelection & QAbstractItemView::SelectRows). Below is how I set up my treeview:
    Qt Code:
    1. // Setting up the tree view
    2. m_tmPatientTreeModel = new QPatientTreeModel(db, this);
    3. ui.m_tvMainTreeView->setModel(m_tmPatientTreeModel);
    4. ui.m_tvMainTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
    5. ui.m_tvMainTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    6. ui.m_tvMainTreeView->setRootIndex(m_tmPatientTreeModel->RootIndex());
    7. ui.m_tvMainTreeView->setAllColumnsShowFocus(true);
    8. ui.m_tvMainTreeView->setColumnWidth(0, 100);
    9. ui.m_tvMainTreeView->setColumnWidth(1, 75);
    10. ui.m_tvMainTreeView->setColumnWidth(2, 30);
    11. ui.m_tvMainTreeView->setColumnWidth(3, 150);
    12. EnsureFirstColumnExpanded();
    To copy to clipboard, switch view to plain text mode 
    db is an object that will contain references to the database, and manages the input and output of the database.
    EnsureFirstColumnExpanded() will go over the patient items and run setFirstColumnSpanned. Everytime I add a new patient, this function is called for all patients, to ensure that the columns are spanned.

    The problem:
    I am unable to select the patient objects (I am able to select the events).

    What I have tried:
    The weirdest thing is that I am either able to select the patient node, but not then only the first column will show up. This I can do when I return 1 at the marked location in the code below.
    The other option is if I return 5 at the marked location, then all five columns are shown, but I am not able to select the patient. If I click on a patient, it does change the current index (i.e. it does fire currentChanged()) but does not fire selectionChanged(). Also, despite setting the behavior to SelectRows, it does not select the whole event row, only the first column. I am at a loss here as to how to fix this.

    Qt Code:
    1. int QPatientTreeModel::columnCount(const QModelIndex & parent) const
    2. {
    3. // If the parent is invalid or is the root index, return 0 because it does not have any columns
    4. if (!parent.isValid())
    5. {
    6. return 5;
    7. }
    8. else if (parent == RootIndex())
    9. {
    10. return 5; // <----------------------- This is the weird location, see text above.
    11. }
    12. else
    13. {
    14. // Determine the patient object by doing a dynamic_cast from the void pointer to the nodebase to the patientNode class,
    15. // in the macro function VOIDTONODETYPE
    16. PatientNode * pat;
    17. VOIDTONODETYPE( parent.internalPointer() , pat , PatientNode );
    18. if (pat)
    19. {
    20. return 1;
    21. }
    22. else
    23. {
    24. return 5;
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions are very very very welcome. I have tried to be as complete as possible, but if you need more code, please let me know.

  2. #2

    Default Re: QTreeView and custom model: Problems while selecting items


Similar Threads

  1. Problems with custom model and rowsInserted() signal
    By frank100 in forum Qt Programming
    Replies: 3
    Last Post: 31st March 2011, 13:53
  2. Custom model for QTreeView with SQLite data source
    By emanuel in forum Qt Programming
    Replies: 0
    Last Post: 19th January 2011, 01:53
  3. QTreeView and custom model from QAbstractItemModel
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 18th January 2010, 17:03
  4. Replies: 1
    Last Post: 24th September 2009, 14:44
  5. Updating QTreeView with custom model
    By supergillis in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 08:01


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.