Here's also some code which could be part of the problem
Code for setting the view to use the model and creating QSortFilterProxyModel wrapper around the model:
void MainWindow::setServiceModel(ServiceModel *model)
{
treeModel = model;
// Create proxyModel and set it's properties
proxyModel->setDynamicSortFilter(true);
proxyModel->setFilterKeyColumn(0);
proxyModel->setSourceModel(treeModel);
serviceTree->setAlternatingRowColors(true);
serviceTree->setModel(proxyModel);
//serviceTree->header()->setStretchLastSection(true);
serviceTree->setSortingEnabled(true);
//proxyModel->sort(0, Qt::AscendingOrder);
}
void MainWindow::setServiceModel(ServiceModel *model)
{
treeModel = model;
// Create proxyModel and set it's properties
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setDynamicSortFilter(true);
proxyModel->setFilterKeyColumn(0);
proxyModel->setSourceModel(treeModel);
serviceTree->setAlternatingRowColors(true);
serviceTree->setModel(proxyModel);
//serviceTree->header()->setStretchLastSection(true);
serviceTree->setSortingEnabled(true);
//proxyModel->sort(0, Qt::AscendingOrder);
connect(serviceTree->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
this, SLOT(handleSelections(const QItemSelection &, const QItemSelection &)));
}
To copy to clipboard, switch view to plain text mode
And the handleSelections slot which is connected to the selectionChanged signal from the selectionModel
{
QModelIndexList indexes = selected.indexes();
int row = -1;
ServiceItem *service = 0;
// Go through all the selected items
foreach (index, indexes) {
row = proxyModel->mapToSource(index).row();
service = static_cast<ServiceItem *>(i.internalPointer());
// Do something with the selected service
// This code should be rewritten if we decide to support multiple selections
// Foreach-loop is already supporting multiple selections
if(service)
{
statusBar()->showMessage(tr("Selected service: %1").arg(service->getServiceName()));
serviceInfoWidget->setCurrentServiceItem(service);
}
else
{
serviceInfoWidget->clearServiceInfo();
}
}
//}
// If none of the services in the tree is selected then clear the ServiceInfoWidgets content
if(indexes.isEmpty())
serviceInfoWidget->clearServiceInfo();
}
void MainWindow::handleSelections(const QItemSelection &selected, const QItemSelection &deselected)
{
QModelIndexList indexes = selected.indexes();
QModelIndex index, i;
int row = -1;
ServiceItem *service = 0;
// Go through all the selected items
foreach (index, indexes) {
row = proxyModel->mapToSource(index).row();
i = treeModel->index(row, 0, QModelIndex());
service = static_cast<ServiceItem *>(i.internalPointer());
// Do something with the selected service
// This code should be rewritten if we decide to support multiple selections
// Foreach-loop is already supporting multiple selections
if(service)
{
statusBar()->showMessage(tr("Selected service: %1").arg(service->getServiceName()));
serviceInfoWidget->setCurrentServiceItem(service);
}
else
{
serviceInfoWidget->clearServiceInfo();
}
}
//}
// If none of the services in the tree is selected then clear the ServiceInfoWidgets content
if(indexes.isEmpty())
serviceInfoWidget->clearServiceInfo();
}
To copy to clipboard, switch view to plain text mode
This one is the remove method from the model code
bool ServiceModel::removeService(ServiceItem *service)
{
bool success = false;
beginRemoveRows
(QModelIndex(), service
->childNumber
(), service
->childNumber
());
success = rootItem->removeChildren(service);
// If removing the service is successful then remove the service's name from the name list
if(success)
serviceNames.removeOne(service->getServiceName());
endRemoveRows();
return success;
}
bool ServiceModel::removeService(ServiceItem *service)
{
bool success = false;
beginRemoveRows(QModelIndex(), service->childNumber(), service->childNumber());
success = rootItem->removeChildren(service);
// If removing the service is successful then remove the service's name from the name list
if(success)
serviceNames.removeOne(service->getServiceName());
endRemoveRows();
return success;
}
To copy to clipboard, switch view to plain text mode
Don't know if these help but maybe someone can spot some mistakes here... or then it's related to some other things
If you have some assumptions for which functions (also another functions which aren't posted in this post) the problem might be relating, just point it out and I'll check if I can post that code in here
Bookmarks