[Qt-interest] QSortFilterModel and strange unhiding of columns

David Heremans david.heremans at intersoft-electronics.com
Tue Aug 24 14:53:27 CEST 2010


Hello,

I get a very weird behavior here and no clue as to what is causing it.

I have created a datamodel that has 3 columns, being:
 -an id
 -a description
 -an access indicator (a boolean like QString being "0" or "1").

I created 2 views so that in the one I see the data with the boolean
set, and the other show the data filtered on an unset boolean
I only want to see the description so I hide the first and last column
This is the code

    proxyModelGranted = new QSortFilterProxyModel(this);
    proxyModelGranted->setSourceModel(father->model);
    proxyModelGranted->setFilterRegExp(QRegExp("1",
             Qt::CaseInsensitive,QRegExp::FixedString));
    proxyModelGranted->setFilterKeyColumn(2);

    ui->listAccess->setModel(proxyModelGranted);
    ui->listAccess->setColumnHidden(0,true);
    ui->listAccess->setColumnHidden(2,true);
    ui->listAccess->setColumnWidth(1,ui->listAccess->width());

    proxyModelDenied= new QSortFilterProxyModel(this);
    proxyModelDenied->setSourceModel(father->model);
    proxyModelDenied->setFilterRegExp(QRegExp("0",
              Qt::CaseInsensitive,QRegExp::FixedString));
    proxyModelDenied->setFilterKeyColumn(2);

    ui->listDenied->setModel(proxyModelDenied);
    ui->listDenied->setColumnHidden(0,true);
    ui->listDenied->setColumnHidden(2,true);
    ui->listDenied->setColumnWidth(1,ui->listDenied->width());

    connect (father->model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
             proxyModelDenied,SLOT(invalidate()));
    connect (father->model,SIGNAL(dataChanged(QModelIndex,QModelIndex)),
             proxyModelGranted,SLOT(invalidate()));

This works nicely,
I then created the famous 4 buttons '<','<<','>' and '>>' to move items
between the two states, the strange thing is that if I select items
(sing or multiple selections) and 'move' them between the two views all
works well, UNLESS I select the first row and then try to move, in that
case the two hidden columns in that listView become visible again!!!

Here is the code of one of buttons
	void WizardPage2::on_toDenied_clicked()
	{
	    QModelIndexList indexes =
	 	ui->listAccess->selectionModel()->selectedIndexes();
	    QModelIndex index;
	    foreach (index,indexes){
	        index=proxyModelGranted->mapToSource(index);
	        father->model->setAccess(index.row(),false);
	    };	
	    father->model->informUpdate();
}

the informUpdate is the one that emits the dataChanged signal.
I already tried adding stuff like
ui->listAccess->selectionModel()->clear(); in the hope that clearing the
selection would solve the problem, but alas it doesn't.

What really bothers me is that this only happens when the item in the
first row is selected. And the fact that updating data in the
sortFilerModel influences the hidden columns of the view also doesn't
match my expectations of model/view architecture...

Can some please explain me:
a) what is going on here
b) how to solve it :-)

thanks for reading,

David Heremans








More information about the Qt-interest-old mailing list