在这个例子中,QT 中的 foreach 循环不起作用
foreach loop in QT not working in this example?
我通常不会陷入循环。但看起来我对循环逻辑一无所知。当我在内部 for 循环中放置一个中断时,我得到的值始终为 0。但是,当我删除中断时,我得到了正确的值。我的代码出了什么问题?
void ContentCache::getAggregatorList()
{
TLOG_FUNC_ENTER();
ContentAggregator *aggregator = ContentAggregator::Instance();
QList <ContentID> aggContentList;
/* Get the service list from the aggregator */
aggregator->getContentList(CONTENT_TYPE_LIVE_SERVICE, aggContentList);
QList<Attribute> attributeListOut;
ContentID content;
foreach( content, aggContentList )
{
TLOG_INFO("SJ..main loop");
//if(content.source == TERRESTRIAL_BROADCAST_SOURCE )
// {
doGetServiceAttributes(content,attributeListOut);
unsigned int attributeIndex = 0;
foreach( Attribute attr, attributeListOut)
{
TLOG_INFO("SJ..Inner loop");
if(attr.name == "chanNum")
{
TLOG_INFO("SJ..The attr channelNum is = "<<attr.value.toInt());
if( attr.value.toUInt() == 999)
{
TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
attr.name = "chanNum";
attr.value = 200;
attributeListOut.replace(attributeIndex,attr);
TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
m_cacheDB->terrestrialUpdateService(content,attributeListOut);
}
else
{
TLOG_INFO("SJ..The unmodified attr channelNum is"<<attr.value.toUInt());
}
break;
}
attributeIndex++;
// }
}
}
// getAttributeValues(ContentID id, QList<QString> value, QList<Attribute> &attributeListOut);
}*
没有休息时间,你正在计算所有Attribute
s。 休息后,您只会将属性计算到第一个具有attr.name == "chanNum"
属性,这似乎不太可能有用。 目前还不完全清楚你打算让break
做什么。
帮助解决这些事情的一种方法是只提取逻辑并以简化的形式尝试它。 例如(以 C++11 形式):
#include <iostream>
#include <string>
int main()
{
std::string string1{"BONGO"};
std::string string2{"This is string2"};
std::cout << "With the breakn";
for ( auto ch : string1 )
{
unsigned int attributeIndex = 0;
for( auto c: string2 )
{
if( c > 'a')
{
if( c > 'i')
{
std::cout << ch << c << "+";
}
else
{
std::cout << ch << c << "-";
}
break;
}
std::cout << ch << c << "@";
attributeIndex++;
}
std::cout << attributeIndex << 'n';
}
std::cout << "Without the breakn";
for ( auto ch : string1 )
{
unsigned int attributeIndex = 0;
for( auto c: string2 )
{
if( c > 'a')
{
if( c > 'i')
{
std::cout << ch << c << "+";
}
else
{
std::cout << ch << c << "-";
}
// no break
}
std::cout << ch << c << "@";
attributeIndex++;
}
std::cout << attributeIndex << 'n';
}
}
这将生成以下输出:
With the break
BT@Bh-1
OT@Oh-1
NT@Nh-1
GT@Gh-1
OT@Oh-1
Without the break
BT@Bh-Bh@Bi-Bi@Bs+Bs@B @Bi-Bi@Bs+Bs@B @Bs+Bs@Bt+Bt@Br+Br@Bi-Bi@Bn+Bn@Bg-Bg@B2@15
OT@Oh-Oh@Oi-Oi@Os+Os@O @Oi-Oi@Os+Os@O @Os+Os@Ot+Ot@Or+Or@Oi-Oi@On+On@Og-Og@O2@15
NT@Nh-Nh@Ni-Ni@Ns+Ns@N @Ni-Ni@Ns+Ns@N @Ns+Ns@Nt+Nt@Nr+Nr@Ni-Ni@Nn+Nn@Ng-Ng@N2@15
GT@Gh-Gh@Gi-Gi@Gs+Gs@G @Gi-Gi@Gs+Gs@G @Gs+Gs@Gt+Gt@Gr+Gr@Gi-Gi@Gn+Gn@Gg-Gg@G2@15
OT@Oh-Oh@Oi-Oi@Os+Os@O @Oi-Oi@Os+Os@O @Os+Os@Ot+Ot@Or+Or@Oi-Oi@On+On@Og-Og@O2@15
使用这样的技术可以帮助您抽象地制定逻辑,然后在实际项目中自信地进行编码。
我错过了清理对象 - 在压力下。这是我愚蠢问题的愚蠢解决方案。QList attributeListOut没有被覆盖,事实上它被附加到这里。这就是问题所在。理想情况下不应该是这种情况。但是,这完全取决于操作员如何超载。
void ContentCache::getAggregatorList()
{
TLOG_FUNC_ENTER();
ContentAggregator *aggregator = ContentAggregator::Instance();
QList <ContentID> aggContentList;
/* Get the service list from the aggregator */
aggregator->getContentList(CONTENT_TYPE_LIVE_SERVICE, aggContentList);
QList<Attribute> attributeListOut;
ContentID content;
foreach( content, aggContentList )
{
TLOG_INFO("SJ..main loop");
//if(content.source == TERRESTRIAL_BROADCAST_SOURCE )
// {
attributeListOut.clear();
doGetServiceAttributes(content,attributeListOut);
unsigned int attributeIndex = 0;
foreach( Attribute attr, attributeListOut)
{
TLOG_INFO("SJ..Inner loop");
if(attr.name == "chanNum")
{
TLOG_INFO("SJ..The attr channelNum is = "<<attr.value.toInt());
if( attr.value.toUInt() == 999)
{
TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
attr.name = "chanNum";
attr.value = 200;
attributeListOut.replace(attributeIndex,attr);
TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
m_cacheDB->terrestrialUpdateService(content,attributeListOut);
}
else
{
TLOG_INFO("SJ..The unmodified attr channelNum is"<<attr.value.toUInt());
}
break;
}
attributeIndex++;
// }
}
}
// getAttributeValues(ContentID id, QList<QString> value, QList<Attribute> &attributeListOut);
}*
相关文章:
- 桌面中的Qt QMessageBox中心'critical'
- 发布版本中的 Qt 隐形组合框(但不在调试版本中)
- 安卓中的Qt和应用程序数据存储
- 当用户在键盘上按住 CTRL 键时,如何在树视图中的 Qt 中实现多选?
- CentOS7中的QT版本
- 一个类中的QT信号/插槽,但从不同的线程发出
- 如何在没有QT项目的情况下使用Visual Studio中的QT资源文件
- MFC App中的QT DLL-如何使Qdialog *真正 *模式
- DCMTK 库与 mac 中的 qt
- Visual Studio 中的 Qt 应用程序 - 入口点
- DLL中的QT事件循环
- 函数参数中的Qt智能指针
- 使用由Eclipse中的Qt设计器创建的Qt GUI
- Qt 4.8.3 中的 Qt 资源系统错误
- 简单的颜色填充QCon中的QT
- 使用QMake将Ubuntu中的QT和OPENCV一起编译
- 使用C程序中的QT库
- DLL(VST插件)中的Qt GUI环境
- Qt Creator中的Qt GUI应用程序构建设置
- 信号中的Qt枚举需要qRegisterMetaType