I'm having a hard time figuring out how to get the position(column and row) and the content in the QLineEdit. I'm using a eventFilter to get the signal but from there i'm stuck. any advice? Thank you
ui->tableWidget->setRowCount(5);
ui->tableWidget->setColumnCount(5);
QStringList wordList;
wordList << "alpha" << "omega" << "omega2" << "omega3" <<"omicron" << "zeta";
for(int i = 0; i<5;i++)
{
QLineEdit *lineEdit = new QLineEdit;
QCompleter *completer = new QCompleter(wordList);
completer->setCaseSensitivity(Qt::CaseInsensitive);
lineEdit->installEventFilter(this);
lineEdit->setCompleter(completer);
ui->tableWidget->setCellWidget(i,i,lineEdit);
}
....
bool MainWindow::eventFilter(QObject * object, QEvent *event)
{
}
I would like to get the position when I finish editing. I would like to pick a word from the list either through up and down key or left mouse click. Once a word is picked that word would populate the QLineEdit. Then i would want to know the position. Now, if the user writes a text different from the content of the list then no position should be returned. I'm only interested on whats in the "wordList". Thank you
As you indicate in your comments, you only want to obtain the text when an element that is set in the QCompleter
is selected, for this we must use the void QCompleter::activated(const QString & text)
signal.
To do this, a slot is created and the connection is made:
*.h
private slots:
void onActivated(const QString &text);
*.cpp
QCompleter *completer = new QCompleter(wordList);
...
connect(completer, qOverload<const QString &>(&QCompleter::activated), this, &MainWindow::onActivated);
There are 2 possible solutions:
QLineEdit
that we obtain through the widget()
method of the QCompleter
, and the QCompleter
we obtain it through sender()
which is the object that emits the signal and pos()
. then we get the QModelIndex
with indexAt()
, and this has the information of the row and column:void MainWindow::onActivated(const QString &text)
{
QCompleter *completer = static_cast<QCompleter *>(sender());
QModelIndex ix = ui->tableWidget->indexAt(completer->widget()->pos());
if(ix.isValid()){
qDebug()<<ix.row()<<ix.column()<<text;
}
}
QCompleter *completer = new QCompleter(wordList);
...
completer->setProperty("row", i);
completer->setProperty("column", i);
void MainWindow::onActivated(const QString &text)
{
QCompleter *completer = static_cast<QCompleter *>(sender());
qDebug()<< completer->property("row").toInt()<<completer->property("column").toInt()<<text;
}
In the following link you can find both complete examples