如何从 QLineEdit 对象中检索文本
How to retrieve text from a QLineEdit object?
我有一个指向QLineEdit对象的指针数组,我想遍历它们并输出它们包含的文本。似乎我在指针方面遇到了麻烦。.
QList<QLineEdit *> boxes = ui->gridLayoutWidget->findChildren<QLineEdit *>();
for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
qDebug() << **it->text(); //not sure how to make this correct
}
我可以使用 qDebug 输出对象和名称,所以我知道 findChildren() 和迭代器设置正常,但我不确定如何获取文本。
尝试:
for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
qDebug() << (*it)->text();
}
它与下面的代码相同,只需保存一个中间指针:
for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
QlineEdit* p= *it; // dereference iterator, get the stored element.
qDebug() << p->text();
}
operator->
的优先级高于operator*
,请参阅C++运算符 wiki
为什么要使用迭代器?Qt有一个很好的foreach
循环,可以为你做这些事情并简化语法:
QList<QLineEdit *> boxes = ui->gridLayoutWidget->findChildren<QLineEdit *>();
foreach(QLineEdit *box, boxes) {
qDebug() << box->text();
}
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 如何从C++代码中检索 QML 的文本字段中的文本?
- 如何从控件中正确检索Unicode文本
- 如果没有数据,则创建一个包含数据的文本文件,然后读取它以检索数据
- 使用命令行Auguments从文本文件中写入文本并检索文本
- WinAPI 从编辑控件中检索文本
- 在C++中,从只知道起始数字(但需要整行)的文本文件中检索某一行
- 从CListCtrl检索当前列表项文本时出现问题
- 从数据库检索后,将其他文本添加到组合框中
- 使用 IFilter 检索的文本的编码是什么?
- 从某个文本文件中检索所有数字
- Qt c++ QDomDocument,递归迭代XML数据,检索没有子数据的文本元素
- 正在从cpp中的curl检索响应文本
- 如何从 QLineEdit 对象中检索文本
- Qt -检索文本从QTextBrowser
- 从文本控件中检索字符位置的客户端区域坐标