Weird behavior of QList<T>
-
Hello there!
I have a slight problem with a QList.
In the given code, rooms is a QList. Using the "at(i)" method i am trying to get data out of that list.
But when i am using variables instead of a number as the "i" attribute i am getting an assert error.
Thanks in advance, Jan//doesn't work int n = 1; printf(building->rooms.at(n).name.toLatin1().data()); //works printf(building->rooms.at(1).name.toLatin1().data());
-
Hello there!
I have a slight problem with a QList.
In the given code, rooms is a QList. Using the "at(i)" method i am trying to get data out of that list.
But when i am using variables instead of a number as the "i" attribute i am getting an assert error.
Thanks in advance, Jan//doesn't work int n = 1; printf(building->rooms.at(n).name.toLatin1().data()); //works printf(building->rooms.at(1).name.toLatin1().data());
-
@jsulm
This is the function in which it happens, roomCount is 2 in this example, the for-loop works for i = 0 but fails when it increments to i = 1void CmdHandler::listRooms() { int roomCount = building->rooms.count(); printf("Room listing: \n\r"); printf(QString(QString::number(building->rooms.count()) + "\r\n").toLatin1().data()); printf("+---------------------------+\n\r"); for (int i = 0; i < roomCount; i++) { printf(QString::number(i).toLatin1().data()); printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data()); printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data()); printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data()); printf("+---------------------------+\n\r"); } }
@VRonin
This is what i get
https://pydio.advtaco.de/public/33a27b -
@jsulm
This is the function in which it happens, roomCount is 2 in this example, the for-loop works for i = 0 but fails when it increments to i = 1void CmdHandler::listRooms() { int roomCount = building->rooms.count(); printf("Room listing: \n\r"); printf(QString(QString::number(building->rooms.count()) + "\r\n").toLatin1().data()); printf("+---------------------------+\n\r"); for (int i = 0; i < roomCount; i++) { printf(QString::number(i).toLatin1().data()); printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data()); printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data()); printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data()); printf("+---------------------------+\n\r"); } }
@VRonin
This is what i get
https://pydio.advtaco.de/public/33a27b -
@jsulm
This is the function in which it happens, roomCount is 2 in this example, the for-loop works for i = 0 but fails when it increments to i = 1void CmdHandler::listRooms() { int roomCount = building->rooms.count(); printf("Room listing: \n\r"); printf(QString(QString::number(building->rooms.count()) + "\r\n").toLatin1().data()); printf("+---------------------------+\n\r"); for (int i = 0; i < roomCount; i++) { printf(QString::number(i).toLatin1().data()); printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data()); printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data()); printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data()); printf("+---------------------------+\n\r"); } }
@VRonin
This is what i get
https://pydio.advtaco.de/public/33a27b@advtaco said in Weird behavior of QList<T>:
This is the function in which it happens, roomCount is 2 in this example, the for-loop works for i = 0 but fails when it increments to i = 1
void CmdHandler::listRooms()
{
int roomCount = building->rooms.count();
printf("Room listing: \n\r");
printf(QString(QString::number(building->rooms.count()) + "\r\n").toLatin1().data());printf("+---------------------------+\n\r");
for (int i = 0; i < roomCount; i++)
{
printf(QString::number(i).toLatin1().data());
printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data());
printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data());
printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data());
printf("+---------------------------+\n\r");
}
}can your share full code in building and room class ?
-
Hello there!
I have a slight problem with a QList.
In the given code, rooms is a QList. Using the "at(i)" method i am trying to get data out of that list.
But when i am using variables instead of a number as the "i" attribute i am getting an assert error.
Thanks in advance, Jan//doesn't work int n = 1; printf(building->rooms.at(n).name.toLatin1().data()); //works printf(building->rooms.at(1).name.toLatin1().data());
@advtaco said in Weird behavior of QList<T>:
//doesn't work int n = 1; printf(building->rooms.at(n).name.toLatin1().data()); //works printf(building->rooms.at(1).name.toLatin1().data());
int n = 0;
printf(building->rooms.at(n).name.toLatin1().data());
its works
? -
@advtaco said in Weird behavior of QList<T>:
This is the function in which it happens, roomCount is 2 in this example, the for-loop works for i = 0 but fails when it increments to i = 1
void CmdHandler::listRooms()
{
int roomCount = building->rooms.count();
printf("Room listing: \n\r");
printf(QString(QString::number(building->rooms.count()) + "\r\n").toLatin1().data());printf("+---------------------------+\n\r");
for (int i = 0; i < roomCount; i++)
{
printf(QString::number(i).toLatin1().data());
printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data());
printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data());
printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data());
printf("+---------------------------+\n\r");
}
}can your share full code in building and room class ?
kbuilding.cpp :
#include "kbuilding.h" kBuilding::kBuilding(QString name) { this->name = name; } void kBuilding::addRoom(kRoom newRoom) { rooms.append(newRoom); } void kBuilding::removeRoom(int remRoom) { rooms.removeAt(remRoom); }
kbuilding.h :
#ifndef KBUILDING_H #define KBUILDING_H #include <QList> #include <QString> #include "kroom.h" class kBuilding { public: kBuilding(QString name); QString name; QList<kRoom> rooms; void addRoom(kRoom newRoom); void removeRoom(int remRoom); }; #endif // KBUILDING_H
kRoom.cpp :
#include "kroom.h" kRoom::kRoom(QString name) { this->name = name; } void kRoom::addSwitch(kSwitch newSwitch) { switches.append(newSwitch); } void kRoom::removeSwitch(int remSwitch) { switches.removeAt(remSwitch); } void kRoom::addBlind(kBlind newBlind) { blinds.append(newBlind); } void kRoom::removeBlind(int remBlind) { blinds.removeAt(remBlind); }
kroom.h :
#ifndef KROOM_H #define KROOM_H #include <QList> #include <QString> #include <kswitch.h> #include <kblind.h> class kRoom { public: kRoom(QString name); QList<kSwitch> switches; QList<kBlind> blinds; QString name; void addSwitch(kSwitch newSwitch); void removeSwitch(int remSwitch); void addBlind(kBlind newBlind); void removeBlind(int remBlind); }; #endif // KROOM_H
kswitch.cpp :
#include "kswitch.h" kSwitch::kSwitch(bool initialState, QString knx_address) { this->currentState = initialState; this->knx_address = knx_address; } bool kSwitch::changeState() { bool newState; if( currentState == true ) { newState = false; } else { newState = true; } // change state via knx if( 1 /* if changing state succeeds */) { this->currentState = newState; return true; } return false; }
kswitch.h :
#ifndef KSWITCH_H #define KSWITCH_H #include <QString> class kSwitch { public: kSwitch(bool initialState, QString knx_address); QString knx_address; bool currentState; bool changeState(); }; #endif // KSWITCH_H
kblind.cpp :
#include "kblind.h" kBlind::kBlind(float initialHeight, QString knx_address) { if(!(0 < initialHeight < 1)) { // error; } else { this->currentHeight = initialHeight; } this->knx_address = knx_address; } bool kBlind::changeHeight(float newHeight) { if(!(0 < newHeight < 1)) { return false; } else { // change state via knx if(1 /* if changing state succeeds */) { this->currentHeight = newHeight; return true; } return false; } }
kblind.h
#ifndef KBLIND_H #define KBLIND_H #include <QString> class kBlind { public: kBlind(float initialHeight, QString knx_address); QString knx_address; float currentHeight; bool changeHeight(float newHeight); }; #endif // KBLIND_H
https://pydio.advtaco.de/public/217d9a
That's the stack trace -
kbuilding.cpp :
#include "kbuilding.h" kBuilding::kBuilding(QString name) { this->name = name; } void kBuilding::addRoom(kRoom newRoom) { rooms.append(newRoom); } void kBuilding::removeRoom(int remRoom) { rooms.removeAt(remRoom); }
kbuilding.h :
#ifndef KBUILDING_H #define KBUILDING_H #include <QList> #include <QString> #include "kroom.h" class kBuilding { public: kBuilding(QString name); QString name; QList<kRoom> rooms; void addRoom(kRoom newRoom); void removeRoom(int remRoom); }; #endif // KBUILDING_H
kRoom.cpp :
#include "kroom.h" kRoom::kRoom(QString name) { this->name = name; } void kRoom::addSwitch(kSwitch newSwitch) { switches.append(newSwitch); } void kRoom::removeSwitch(int remSwitch) { switches.removeAt(remSwitch); } void kRoom::addBlind(kBlind newBlind) { blinds.append(newBlind); } void kRoom::removeBlind(int remBlind) { blinds.removeAt(remBlind); }
kroom.h :
#ifndef KROOM_H #define KROOM_H #include <QList> #include <QString> #include <kswitch.h> #include <kblind.h> class kRoom { public: kRoom(QString name); QList<kSwitch> switches; QList<kBlind> blinds; QString name; void addSwitch(kSwitch newSwitch); void removeSwitch(int remSwitch); void addBlind(kBlind newBlind); void removeBlind(int remBlind); }; #endif // KROOM_H
kswitch.cpp :
#include "kswitch.h" kSwitch::kSwitch(bool initialState, QString knx_address) { this->currentState = initialState; this->knx_address = knx_address; } bool kSwitch::changeState() { bool newState; if( currentState == true ) { newState = false; } else { newState = true; } // change state via knx if( 1 /* if changing state succeeds */) { this->currentState = newState; return true; } return false; }
kswitch.h :
#ifndef KSWITCH_H #define KSWITCH_H #include <QString> class kSwitch { public: kSwitch(bool initialState, QString knx_address); QString knx_address; bool currentState; bool changeState(); }; #endif // KSWITCH_H
kblind.cpp :
#include "kblind.h" kBlind::kBlind(float initialHeight, QString knx_address) { if(!(0 < initialHeight < 1)) { // error; } else { this->currentHeight = initialHeight; } this->knx_address = knx_address; } bool kBlind::changeHeight(float newHeight) { if(!(0 < newHeight < 1)) { return false; } else { // change state via knx if(1 /* if changing state succeeds */) { this->currentHeight = newHeight; return true; } return false; } }
kblind.h
#ifndef KBLIND_H #define KBLIND_H #include <QString> class kBlind { public: kBlind(float initialHeight, QString knx_address); QString knx_address; float currentHeight; bool changeHeight(float newHeight); }; #endif // KBLIND_H
https://pydio.advtaco.de/public/217d9a
That's the stack trace -
kbuilding.cpp :
#include "kbuilding.h" kBuilding::kBuilding(QString name) { this->name = name; } void kBuilding::addRoom(kRoom newRoom) { rooms.append(newRoom); } void kBuilding::removeRoom(int remRoom) { rooms.removeAt(remRoom); }
kbuilding.h :
#ifndef KBUILDING_H #define KBUILDING_H #include <QList> #include <QString> #include "kroom.h" class kBuilding { public: kBuilding(QString name); QString name; QList<kRoom> rooms; void addRoom(kRoom newRoom); void removeRoom(int remRoom); }; #endif // KBUILDING_H
kRoom.cpp :
#include "kroom.h" kRoom::kRoom(QString name) { this->name = name; } void kRoom::addSwitch(kSwitch newSwitch) { switches.append(newSwitch); } void kRoom::removeSwitch(int remSwitch) { switches.removeAt(remSwitch); } void kRoom::addBlind(kBlind newBlind) { blinds.append(newBlind); } void kRoom::removeBlind(int remBlind) { blinds.removeAt(remBlind); }
kroom.h :
#ifndef KROOM_H #define KROOM_H #include <QList> #include <QString> #include <kswitch.h> #include <kblind.h> class kRoom { public: kRoom(QString name); QList<kSwitch> switches; QList<kBlind> blinds; QString name; void addSwitch(kSwitch newSwitch); void removeSwitch(int remSwitch); void addBlind(kBlind newBlind); void removeBlind(int remBlind); }; #endif // KROOM_H
kswitch.cpp :
#include "kswitch.h" kSwitch::kSwitch(bool initialState, QString knx_address) { this->currentState = initialState; this->knx_address = knx_address; } bool kSwitch::changeState() { bool newState; if( currentState == true ) { newState = false; } else { newState = true; } // change state via knx if( 1 /* if changing state succeeds */) { this->currentState = newState; return true; } return false; }
kswitch.h :
#ifndef KSWITCH_H #define KSWITCH_H #include <QString> class kSwitch { public: kSwitch(bool initialState, QString knx_address); QString knx_address; bool currentState; bool changeState(); }; #endif // KSWITCH_H
kblind.cpp :
#include "kblind.h" kBlind::kBlind(float initialHeight, QString knx_address) { if(!(0 < initialHeight < 1)) { // error; } else { this->currentHeight = initialHeight; } this->knx_address = knx_address; } bool kBlind::changeHeight(float newHeight) { if(!(0 < newHeight < 1)) { return false; } else { // change state via knx if(1 /* if changing state succeeds */) { this->currentHeight = newHeight; return true; } return false; } }
kblind.h
#ifndef KBLIND_H #define KBLIND_H #include <QString> class kBlind { public: kBlind(float initialHeight, QString knx_address); QString knx_address; float currentHeight; bool changeHeight(float newHeight); }; #endif // KBLIND_H
https://pydio.advtaco.de/public/217d9a
That's the stack trace -
@advtaco Are you sure you really have roomCount entries in the list? You should check that first.
-
@advtaco said in Weird behavior of QList<T>:
That's the stack trace
Which one is line 55 of cmdhandler.cpp?
I know this is something unbelievably obvious that I'm missing but I just can't see the bug yet
@advtaco said in Weird behavior of QList<T>:
#ifndef KROOM_H
#define KROOM_H#include <QList>
#include <QString>
#include <kswitch.h>
#include <kblind.h>class kRoom
{
public:
kRoom(QString name);
QList<kSwitch> switches;
QList<kBlind> blinds;
QString name;
void addSwitch(kSwitch newSwitch);
void removeSwitch(int remSwitch);
void addBlind(kBlind newBlind);
void removeBlind(int remBlind);
};When I used class in other classes I had a problem with the following:
like this> #include <QList> > #include <QString> > #include <kswitch.h> > #include <kblind.h> class kSwtich; class kBlind;
Could this be a problem anyway?
-
@advtaco said in Weird behavior of QList<T>:
#ifndef KROOM_H
#define KROOM_H#include <QList>
#include <QString>
#include <kswitch.h>
#include <kblind.h>class kRoom
{
public:
kRoom(QString name);
QList<kSwitch> switches;
QList<kBlind> blinds;
QString name;
void addSwitch(kSwitch newSwitch);
void removeSwitch(int remSwitch);
void addBlind(kBlind newBlind);
void removeBlind(int remBlind);
};When I used class in other classes I had a problem with the following:
like this> #include <QList> > #include <QString> > #include <kswitch.h> > #include <kblind.h> class kSwtich; class kBlind;
Could this be a problem anyway?
-
@advtaco Not related to your question: may I ask you why you use printf C function? It makes your code more complex as you need to get char* out of QString. Why not use qDebug for debugging or std::cout for regular std out - you're using C++ not C :-)
-
I'm speechless... I can only thing of something weird going on in
building
ok, let's try with an alternative.
replacefor (int i = 0; i < roomCount; i++) { printf(QString::number(i).toLatin1().data()); printf(QString("Room #" + QString::number(i) + ", " + building->rooms.at(i).name + "\n\r").toLatin1().data()); printf(QString("Switches: " + QString::number(building->rooms.at(i).switches.count()) + "\n\r").toLatin1().data()); printf(QString("Blinds: " + QString::number(building->rooms.at(i).blinds.count()) + "\n\r").toLatin1().data()); printf("+---------------------------+\n\r"); }
with
foreach(const kRoom& singleRoom, building->rooms){ qDebug() << singleRoom.name; }
-
-
@advtaco Not related to your question: may I ask you why you use printf C function? It makes your code more complex as you need to get char* out of QString. Why not use qDebug for debugging or std::cout for regular std out - you're using C++ not C :-)
@jsulm said in Weird behavior of QList<T>:
@advtaco Not related to your question: may I ask you why you use printf C function? It makes your code more complex as you need to get char* out of QString. Why not use qDebug for debugging or std::cout for regular std out - you're using C++ not C :-)
For more infos on this front, see https://stackoverflow.com/questions/3886105/how-to-print-to-console-when-using-qt