PDA

View Full Version : Qtableview, insert and selection



noValue
1st May 2010, 10:38
Hi There,

just playing around with Qtableview.
What I want to do is insert a new row, scroll to it, and show it selected.

I have a working insert with the following code connected to a button


private void insert()
{
String Nr="999";
model.insertRow(model.rowCount());
model.setData(model.index(model.rowCount()-1,0), Integer.parseInt(Nr));
}

I achieved to scroll down the view by doing:


insertButton.clicked.connect(view, "scrollToBottom()");

But I can't accomplish to select the inserted row automaticly.
Tried view.selectrow in my insert routine and fumbled with a selectionmodel but all failed in the end
with a NullPointerException.

I' am aware this is a very basic question and tried my best to squeeze the answer out of google, but
nothing got me on track, so any hint would be greatly appreciated.

Thanks

tbscope
1st May 2010, 11:26
Your try with a selectionmodel is the correct one.
Create a selectionmodel and attach it to your tableview.
Then use the selectionmodel to select the modelindex of your inserted row.

noValue
1st May 2010, 11:51
thanks a lot for your quick respond.

I connected the selectionmodel to the view as propsed and added


SM.select(model.index(model.rowCount()-1, 0),QItemSelectionModel.SelectionFlag.Select );

to my insert routine, wich gives me again a NullPointerException.

I miss something, do I!?

noValue
1st May 2010, 12:45
ah, sorted out the NullPointerException which was nothing but a typo.
So, now I don't get any errors anymore, but the row isn't selected either.

Lykurg
1st May 2010, 19:37
What's with QTableView::selectRow()?

noValue
1st May 2010, 23:08
What's with QTableView::selectRow()?

thanks, for responding...
that was my first approach,I tried:


view.selectRow(model.rowCount()-1)

no error message, but also no selected row.
Tried also simply selecting row nr. 1, same effect -> nothing.

Here is some more info about how I bound model & view & SM together:


model.setTable(eTable);
model.sort(0, Qt.SortOrder.AscendingOrder);
model.setEditStrategy(QSqlTableModel.EditStrategy. OnManualSubmit);
model.select();
view.setModel(model);
view.setSelectionBehavior(QAbstractItemView.Select ionBehavior.SelectRows);
view.setSelectionMode(QAbstractItemView.SelectionM ode.SingleSelection);
QItemSelectionModel SM = new QItemSelectionModel(model);
view.setSelectionModel(SM);

Maybe something wrong there?

tbscope
2nd May 2010, 08:35
Here's an example with a simple QStringListModel.

I have created a window with a listview, a lineedit and a pushbutton.

Define the following variables:
QStringListModel *model;
QItemSelectionModel *selectionModel;
QStringList data;

Then assign them to the listview and connect the clicked signal of the button to a slot.
data.clear();
model = new QStringListModel(this);
model->setStringList(data);
selectionModel = new QItemSelectionModel(model);
ui->listView->setModel(model);
ui->listView->setSelectionModel(selectionModel);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(addAndSelect()));

The most interesting happens inside the slot:
void MainWindow::addAndSelect()
{
data << ui->lineEdit->text();
model->setStringList(data);
QModelIndex index = model->index(data.count()-1, 0);
selectionModel->setCurrentIndex(index, QItemSelectionModel::Select);
}

This is what happens:
1. Add the new line to the stringlist data;
2. Reset the stringlist in the stringlistmodel;
3. Get the modelindex of the last item;
4. Set the current item in the selectionmodel to the last item.

noValue
4th May 2010, 00:06
Hi tbscope,

thanks for your detailed example.
Pushed me in the right direction, even though I can't see why me first experiments with
selectionmodel didn't worked out. Maybe something wrong with obtaining the index from the model.

Anyway, after I revised my code according to your approach, everything is working fine.

Thanks again.