PDA

View Full Version : QTreeView and custom model: Problems while selecting items



gaohan
15th October 2012, 10:34
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:


// Setting up the tree view
m_tmPatientTreeModel = new QPatientTreeModel(db, this);
ui.m_tvMainTreeView->setModel(m_tmPatientTreeModel);
ui.m_tvMainTreeView->setSelectionMode(QAbstractItemView::SingleSelectio n);
ui.m_tvMainTreeView->setSelectionBehavior(QAbstractItemView::SelectRows );
ui.m_tvMainTreeView->setRootIndex(m_tmPatientTreeModel->RootIndex());
ui.m_tvMainTreeView->setAllColumnsShowFocus(true);
ui.m_tvMainTreeView->setColumnWidth(0, 100);
ui.m_tvMainTreeView->setColumnWidth(1, 75);
ui.m_tvMainTreeView->setColumnWidth(2, 30);
ui.m_tvMainTreeView->setColumnWidth(3, 150);
EnsureFirstColumnExpanded();

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.



int QPatientTreeModel::columnCount(const QModelIndex & parent) const
{
// If the parent is invalid or is the root index, return 0 because it does not have any columns
if (!parent.isValid())
{
return 5;
}
else if (parent == RootIndex())
{
return 5; // <----------------------- This is the weird location, see text above.
}
else
{
// Determine the patient object by doing a dynamic_cast from the void pointer to the nodebase to the patientNode class,
// in the macro function VOIDTONODETYPE
PatientNode * pat;
VOIDTONODETYPE( parent.internalPointer() , pat , PatientNode );
if (pat)
{
return 1;
}
else
{
return 5;
}
}
}


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.

gaohan
22nd October 2012, 11:02
Anybody????