0

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

VMI
  • 61
  • 10
  • When do you want to get the row and column, when you finish editing? – eyllanesc Mar 18 '18 at 21:36
  • Eyllanesc: Yes, 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. Thank you – VMI Mar 18 '18 at 23:17

1 Answers1

1

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:

  • The first to use the position of the 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;
    }
}
  • Or the row and column is saved as a property:

    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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Eyllanesc: Yes, 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. Thank you – VMI Mar 18 '18 at 23:30
  • @VMI and what happens if the user writes a text different from the content of the list, should he also indicate the position? – eyllanesc Mar 18 '18 at 23:35
  • 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 on the list. – VMI Mar 18 '18 at 23:37
  • @VMI Edit your question and specify it. – eyllanesc Mar 18 '18 at 23:39