Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从QLineEdit对象检索文本?_C++_Qt - Fatal编程技术网

C++ 如何从QLineEdit对象检索文本?

C++ 如何从QLineEdit对象检索文本?,c++,qt,C++,Qt,我有一个指向QLineEdit对象的指针数组,我希望遍历它们并输出它们所包含的文本。看来我的指针有问题 QList<QLineEdit *> boxes = ui->gridLayoutWidget->findChildren<QLineEdit *>(); for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++) { qDebug() <

我有一个指向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
}
QList-box=ui->gridLayoutWidget->findChildren();
for(QList::iterator it=box.begin();it!=box.end();it++)
{
qDebug()text();//不确定如何使其正确
}
我可以使用qDebug输出对象和名称,因此我知道findChildren()和迭代器设置得很好,但我不确定如何获取文本。

试试:

for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
     qDebug() << (*it)->text();  
}
for(QList::iterator it=box.begin();it!=box.end();it++)
{
qDebug()文本();
}
与下面的代码相同,只需保存一个中间指针:

for(QList<QLineEdit *>::iterator it = boxes.begin(); it != boxes.end(); it++)
{
    QlineEdit* p= *it; // dereference iterator, get the stored element.
    qDebug() << p->text();
}
for(QList::iterator it=box.begin();it!=box.end();it++)
{
QlineEdit*p=*it;//取消引用迭代器,获取存储的元素。
qDebug()文本();
}

<代码>运算符> <代码>比运算符*/COD>有更高的优先级,见C++ +/P>

为什么使用迭代器?Qt有一个很好的

foreach
循环,它可以为您完成这些工作,并简化语法:

QList<QLineEdit *> boxes = ui->gridLayoutWidget->findChildren<QLineEdit *>();

foreach(QLineEdit *box, boxes) {
    qDebug() << box->text();
}
QList-box=ui->gridLayoutWidget->findChildren();
foreach(QLineEdit*框,框){
qDebug()文本();
}

就是这样。括号中添加了什么?首先需要引用引用迭代器,因为C++操作符优先。谢谢,我不知道!