简体   繁体   English

QTableWidget和QLineEdit-位置和文本内容

[英]QTableWidget and QLineEdit - position and text content

I'm having a hard time figuring out how to get the position(column and row) and the content in the QLineEdit. 我很难弄清楚如何在QLineEdit中获取位置(列和行)和内容。 I'm using a eventFilter to get the signal but from there i'm stuck. 我正在使用eventFilter来获取信号,但从那里我被卡住了。 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. 选择一个单词后,该单词将填充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". 我只对“ 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. 正如您在注释中指出的那样,仅当选择了QCompleter设置的元素时,才希望获取文本,为此,我们必须使用void QCompleter::activated(const QString & text)信号。

To do this, a slot is created and the connection is made: 为此,将创建一个插槽并建立连接:

*.h *。H

private slots:
    void onActivated(const QString &text);

*.cpp *的.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() . 首先使用我们通过QCompleterwidget()方法获得的QLineEdit的位置,然后使用QCompleter通过sender()获得它的位置, sender()是发出信号和pos() then we get the QModelIndex with indexAt() , and this has the information of the row and column: 然后我们获得带有indexAt()QModelIndex ,它具有行和列的信息:

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 在下面的链接中,您可以找到两个完整的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM