C++ 遍历QList并对包含的对象调用函数
我在遍历QList和调用所包含对象的成员函数时遇到问题。我正在尝试禁用此窗口中的所有按钮 这是我的密码C++ 遍历QList并对包含的对象调用函数,c++,qt,iterator,C++,Qt,Iterator,我在遍历QList和调用所包含对象的成员函数时遇到问题。我正在尝试禁用此窗口中的所有按钮 这是我的密码 void TicTacToe::disableGame(){ QList<QPushButton *> allPButtons = findChildren<QPushButton *>(); QList<QPushButton *>::iterator i; for (i = allPButtons.begin(); i != al
void TicTacToe::disableGame(){
QList<QPushButton *> allPButtons = findChildren<QPushButton *>();
QList<QPushButton *>::iterator i;
for (i = allPButtons.begin(); i != allPButtons.end(); ++i)
allPButtons[i]->setEnabled(false);
}
void TicTacToe::disableGame(){
QList allPButtons=findChildren();
QList::迭代器i;
for(i=allPButtons.begin();i!=allPButtons.end();+i)
allPButtons[i]->setEnabled(假);
}
但它不会编译。那么如何正确地使用迭代器呢?迭代器的要点是,在进行迭代时,您不再需要列表(或其他容器对象)。你只需要一个迭代器,知道如何遍历它的内容,还有一个迭代器,知道它应该停在哪里 这里有一个指针迭代器,因此必须在迭代器上调用
操作符*()
,然后在指针上调用*
(或->
,以使您的生活更轻松)
因此,您需要使用
(*i)->setEnabled(false);
您还可以查看qt,或者使用一个。它们使您的代码更干净、更清晰。foreach工作得很好。我很感激这个问题的答案,还有进一步的说明,foreach很快就会在Qt中被弃用,range for是未来的方向。哦,我不知道。谢谢无论如何,在c++11循环范围内是有意义的。