QTableWidget 和 QLineEdit - 位置和文本内容
QTableWidget and QLineEdit - position and text content
我很难弄清楚如何在QLineEdit中获取位置(列和行(和内容。我正在使用事件过滤器来获取信号,但从那里我卡住了。有什么建议吗?谢谢
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)
{
}
我想在完成编辑后获得该职位。我想通过向上和向下键或鼠标左键从列表中选择一个单词。一旦选择一个单词,该单词将填充QLineEdit。然后我想知道这个职位。现在,如果用户编写的文本与列表内容不同,则不应返回任何位置。我只对"单词列表"中的内容感兴趣。谢谢
正如您在注释中指出的那样,您只想在选择QCompleter
中设置的元素时获取文本,为此我们必须使用 void QCompleter::activated(const QString & text)
信号。
为此,将创建一个插槽并建立连接:
*.h
private slots:
void onActivated(const QString &text);
*。.cpp
QCompleter *completer = new QCompleter(wordList);
...
connect(completer, qOverload<const QString &>(&QCompleter::activated), this, &MainWindow::onActivated);
有两种可能的解决方案:
- 首先使用我们通过
QCompleter
的widget()
方法获得的QLineEdit
的位置,以及我们通过sender()
获得它QCompleter
,这是发出信号和pos()
的对象。 然后我们得到带有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;
}
}
- 或者将行和列另存为属性:
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;
}
在以下链接中,您可以找到两个完整的示例
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 如何从QToolBox中动态创建的QLineEdit中获取文本
- QTableWidget 和 QLineEdit - 位置和文本内容
- QlineEdit:显示一个处理过的文本,而不是输入的文本,而是保留它(自定义回声模式)
- 当用户单击QT中的QlineEdit时,如何更改Qlineed的文本
- 如何使QLineEdit编辑在文本来自QPushButton时启用
- QLineEdit 在激活时更改文本权重
- QLineEdit:如果鼠标悬停,则显示超长文本作为工具提示
- 为qlineedit设置了背景图像时,如何在qlineedit中设置文本颜色
- 为什么QLineEdit::setCursorPosition(int)选择(突出显示)文本
- 在已经包含某些文本的QTextEdit中显示QLineEdit中的文本,并实时更新
- 更改 QLineEdit 占位符文本的颜色
- 如何从 QLineEdit 对象中检索文本
- 实时更改qt应用程序的qlineedit中的文本
- 如何按Escape键重置QLineEdit文本
- QT:简单的文本从QLineEdit到QLabel的不同类