PDA

View Full Version : QTableview Sorting and hiding



hmarani
23rd February 2011, 19:08
:)Hi, the code below does not sort and does not hide the column but the column header shows active when is clicked. Please, help



model->setQuery("SELECT f1, id, f3, serialno FROM person2 WHERE f1 like '" + nametxt + "'");
model->setHeaderData(0, Qt::Horizontal, tr("f1"));
model->setHeaderData(1, Qt::Horizontal, tr("id"));


QTableView *view = new QTableView;

view->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
view->setSortingEnabled(true);
:) view->setSelectionBehavior(QAbstractItemView::SelectRows );
view->setSelectionMode(QAbstractItemView::SingleSelectio n);

view->setAlternatingRowColors(true);
view->setAutoFillBackground( true );
QPalette p = view->palette();
p.setColor(QPalette::Base, Qt::darkYellow);
p.setColor(QPalette::Highlight, Qt::magenta);
view->setPalette(p);

view->hideColumn(0); // does not work!

view->verticalHeader()->hide();

view->setModel(model);
view->show();

tbscope
24th February 2011, 06:03
What happens if you hide the column after you've set the model?

hmarani
24th February 2011, 16:19
I think the problem is I am using QTableView with QSqlQueryModel instead of QSqlTableModel but I do not
know how to sovle it maybe Proxy? please help.
Thanks

ChrisW67
24th February 2011, 23:13
You cannot hide columns in the view before the view knows it has columns to hide. Set the model first, then hide the column.

hmarani
25th February 2011, 16:20
Chris,
Solved, thanks a lot; now the problem is mapToSource:


QModelIndex index=model->index(index.row());
index = proxyModel->mapToSource(index);
connect(view, SIGNAL(clicked(QModelIndex)),
this, SLOT(for3(QModelIndex)));




void MainWindow::for3(QModelIndex,index)
QSqlRecord record = model->record(index.row());


What should be for model in for3 function or any other sugestion; sorry for the so many mistakes, but please give some hints. Thanks!

ChrisW67
25th February 2011, 22:20
What's the problem and what do you want to achieve? I can give you hints and advice like "Be nice to your mother" but it probably won't help.

If the view is using a proxy model then the index in the clicked() signal will be a proxy model index. If you need the source model index then you need to use mapToSource() but you might not need to do that depending on what you wish to achieve.

hmarani
25th February 2011, 22:48
:)Hi Chris,
I created a view, it was working fine (but not sorting) when clicked could go to for3 function, from there I could manipulate the record with model record. Then, I used QSortFilterProxyModel as below for sorting, it sorts but when it is clicked it goes to wrong record. As you mentioned I do not know how to implement the index2 with click! Or I might be missing or using wrong statements. Sorry for confusion and thank you for your help.



QSortFilterProxyModel *filterModel = new QSortFilterProxyModel(this);
filterModel->setSourceModel(model);
QTableView *view = new QTableView;
filterModel->setDynamicSortFilter(true);
filterModel->dynamicSortFilter();

view->setModel(filterModel);
QModelIndex index = view->currentIndex();
QModelIndex index2 = filterModel->mapToSource(index);

connect(view, SIGNAL(clicked(QModelIndex)),
this, SLOT(for3(QModelIndex)));



void MainWindow::fory3(QModelIndex index)
{
QSqlRecord record = model->record(index.row());
QString name = record.value("artist").toString();
QString jobv = record.value("job").toString();

QString person2serial= record.value("serialno").toString();

ChrisW67
27th February 2011, 21:04
Line 8 and 9 in your first code snippet make no sense where you have put them in the construction code.

I have commented in your second code snippet:


void MainWindow::fory3(QModelIndex index) // I assume this is a typing error fory3 or for3?
// ^^^^^ this is an index in the proxy model
{
// Before you can use the index against the source QSqlTableModel you need to map it
QModelIndex srcIndex = filterModel->mapToSource(index);
QSqlRecord record = model->record(srcIndex.row());
QString name = record.value("artist").toString();
QString jobv = record.value("job").toString();

QString person2serial= record.value("serialno").toString();

However, you don't need to worry about the layering of models if you use the Model/View interface to access the data


void MainWindow::for3(QModelIndex index)
{
int row = index.row();
QString name = index.sibling(row, nameColumn).data().toString();
QString jobv = index.sibling(row, jobvColumn).data().toString();
QString person2serial= index.sibling(row, serialnoColumn).data().toString()

hmarani
28th February 2011, 15:05
:)Hi Chris,
Solved; supper. I realy appriciate your great favor. That is very kind of you.
Thanks a lot