2016-04-25 15 views
-1

Ich versuche, zwei Zeichenfolgen mit Standard in Qt zu vergleichen, aber es schlägt immer fehl. es ist immer falschVergleichen von Zeichenfolgen in Qt funktioniert nicht

die Aussage, wenn Gruppe == Standard, geht zu s.beginGroup, aber die Gruppe heißt Default. Ich weiß nicht wo das Problem ist, das ist so komisch.

foreach (const QString &group, s.childGroups()) { 
     if(group =="Default") 
      continue; 
     s.beginGroup(group); 
     Profile *profile = new Profile(); 
     profile->setObjectName(group); 
     profile->load(s); 
     s.endGroup(); 

     m_Profiles << profile; 

    } 

Antwort

0

Die Definition von foreach in Qt an folgende löst: so

# define Q_FOREACH(variable, container)        \ 
for (QForeachContainer<QT_FOREACH_DECLTYPE(container)> _container_((container)); \ 
_container_.control && _container_.i != _container_.e;   \ 
++_container_.i, _container_.control ^= 1)      \ 
    for (variable = *_container_.i; _container_.control; _container_.control = 0) 

wie Sie for Schleifen sind zwei sehen können gibt es möglicherweise Schlüsselwort die weiterhin nicht mit dem man nicht mehr weiter Sie möchten , aber der innere.

bearbeiten nach Kommentar des @ chi

Es gibt eine schöne Erklärung ist in der Qt-Header-Datei:

// Explanation of the control word: 
// - it's initialized to 1 
// - that means both the inner and outer loops start 
// - if there were no breaks, at the end of the inner loop, it's set to 0, which 
// causes it to exit (the inner loop is run exactly once) 
// - at the end of the outer loop, it's inverted, so it becomes 1 again, allowing 
// the outer loop to continue executing 
// - if there was a break inside the inner loop, it will exit with control still 
// set to 1; in that case, the outer loop will invert it to 0 and will exit too 

, wie Sie es sehen können, nur der Pause keine Erwähnung der continue ist. Und es gibt keine Erwähnung der continue auf der foreach Dokumentation Seite entweder: http://doc.qt.io/qt-4.8/containers.html#the-foreach-keyword nur die Pause wird vorgestellt. Wenn Sie

+0

Wow. Die dem '_container_.control' zugrunde liegende Logik ist ... interessant. – chi

+0

Egal was passiert, ein 'continue' oder ein' break' wird den aktuellen Block verlassen, so dass das ursprüngliche Problem nicht behoben wird. Es wird nicht zu "s.beginGroup" gehen! –

+0

"Weiter" und "Pause" funktionieren in 'Q_FOREACH' gut. Vielleicht sind sie in der Gräuel gebrochen, die die Umsetzung ist, die auf VC6 funktioniert, aber dann ist alles andere bereits dort kaputt, also lasst uns nicht weiter darüber sprechen. –

1

Compiler C++ 11 aktiviert, besser Schalter auf Fern-für statt:

for (const QString& group : s.childGroups()) 
{ ... } 

Ranged-for-Schleifen continue Stichwort unterstützen wie erwartet

Auch CONFIG += c++11 muss ein hinzugefügt werden * .pro-Datei Ihres Projekts

0

Works für mich:

#include <QtCore> 

int main() { 
    auto const kDefault = QStringLiteral("Default"); 
    QStringList groups{"a", kDefault, "b"}; 
    QStringList output; 
    // C++11 
    for (auto group : groups) { 
     if(group == kDefault) 
     continue; 
     Q_ASSERT(group != kDefault); 
     output << group; 
    } 
    // C++98 
    foreach (const QString &group, groups) { 
     if(group == kDefault) 
     continue; 
     Q_ASSERT(group != kDefault); 
     output << group; 
    } 
    Q_ASSERT(output == (QStringList{"a", "b", "a", "b"})); 
} 

Ihr Problem ist woanders, oder Ihr Debugger belügt Sie.

Verwandte Themen