PDA

View Full Version : qtableview qcompleter



Cremers
10th May 2014, 10:42
Hi,
I'm trying to get a tableview scroll to the current index I get from completer->currentIndex but I'm having no luck



PlaceDialog::PlaceDialog(QWidget *parent, QString def, QString filename)
{
// etc
completer = new QCompleter(this);
completer->setCompletionMode( QCompleter::InlineCompletion);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setModel(model);
edit = new LineEdit(this);
connect(edit, SIGNAL(textChanged(QString)), this, SLOT(edited(QString)));
edit->setCompleter(completer);
//etc
}

void PlaceDialog::edited(QString s)
{
QDebug()<s << completer->currentCompletion() << completer->currentIndex();
table->setCurrentIndex(completer->currentIndex());
}


I can see the completer working, the lineedit is filled with the completing but the index stays at 0.

anda_skoa
10th May 2014, 11:26
QAbstractItemView::setCurrentIndex() just sets the cell for the given index to be the current one, e.g. receiving keyboard events, usually also becoming selected.

For your initial decscription you want to scroll to that cell, see QAbstractItemView::scrollTo()

Cheers,
_

Cremers
10th May 2014, 12:29
Thanks, I tried that, no change.
I think my problem is something else because in the textChanged slot completer->currentCompletion() always returns the correct string BUT completer->currentRow() and completer->currentIndex() somehow always return 0.

anda_skoa
10th May 2014, 15:23
Well, textChanged() is emitted whenever the line edit's text changes.
Have you tried listening to the completer's signals?

Cheers,
_

Cremers
10th May 2014, 15:38
Do you have an example of what you mean? QCompleter only has signals for when an item in the popup list is activated or higlighted. With Completer::InlineCompletion there is no popup

anda_skoa
10th May 2014, 17:24
Ah, sorry, missed the inline completion bit.

I assume currentCompletion() is also never anything other than a null QString?

Hmm.
You could try calling QAbstractItemModel::match() and call scrollTo() if the result has only one entry, i.e. when the match is unique.

Cheers,
_

Cremers
10th May 2014, 18:56
Yes I had been trying something like that. Fortunately currentCompletion() returns the correct string else I wouldn't have a clue at all. :) This works:


void PlaceDialog::edited(QString s)
{
QModelIndexList matches = model->match(model->index(0, 0), Qt::DisplayRole, completer->currentCompletion());
foreach(const QModelIndex &index, matches)
{
table->setCurrentIndex(model->index(index.row(), 0));
break;
}
}