PDA

View Full Version : Chancing values to a QList<object>



terhje
19th May 2011, 02:52
Hi, I'm new to QT and c++ and need some help. Im making a application for circuit board spesifications.

I have a class Panel which have a list of boards:QList<Board>, and in the Board class i have a Qlist of Layers, Qlist<Layer>. Im trying to modify my layer class values through two QLists, but the values wont budge. any idea?



//layer.h
private:
QString type;
public:
QString getType();
void setType(QString newType);

//layer.cpp
void layer::setType(QString newType){newType = type;}
QString layer::getType(){return type;}





//board.h
private:
QList<layer> layers;
public:
void addLayer();
QList<layer> getLayers();

//board.cpp
QList<layer> Board::getLayers(){return layers;}
void Board::addLayer(){
layer new1;
layers.append(new1);
}




//panel.h
QList<Board> b;
Board testBoard;

//panel.cpp
b.append(testBoard);
b.operator [](0).addLayer(); //adding a layer is no problem
b.operator [](0).getLayers().operator [](0).setType("nooo");
//trying to set the value, but nothing happends.
QString check = b.value(0).getLayers().value(0).getType();
//the check value returns the initial value of type from the constructor.


I appreciate any help. Thx
Terje H

ChrisW67
19th May 2011, 04:34
Your problems are basic C++ understanding rather than any thing Qt specific.

Line 6 of listing 2 tells me that your getLayers() returns a copy of the list of layers rather than a reference/pointer to the existing one. When you use that at line 8 of the last listing you are operating on a transient copy of the list which then is discarded. When you check the value at line 10 you are making another copy of the original, checking and then discarding it.

Lines 7, 8 and 10 in the last listing are using operator[] in a particularly odd way also.

Try this instrumented code (with explicit copy constructor) and pay particular attention to which layer objects you are accessing:

#include <QtCore>
#include <QDebug>

class layer {
QString type;
public:
layer(): type("default") {
qDebug() << "Construct" << this;
}
layer(const layer& other) {
qDebug() << "Copy construct" << this;
type = other.type;
}
~layer() {
qDebug() << "Destruct" << this;
}
QString getType() const {
qDebug() << "getType" << type << this;
return type;
}
void setType(QString newType) {
qDebug() << "setType" << newType << this;
type = newType;
}
};

class board {
QList<layer> layers;
public:
QList<layer> getLayers() {return layers;}
//QList<layer>& getLayers() {return layers;}
void addLayer() {
qDebug() << "start adding layer";
layer newLayer;
layers.append(newLayer);
qDebug() << "finish adding layer";
}

};

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QList<board> b;
board testBoard;
b.append(testBoard);
b[0].addLayer();
b[0].getLayers()[0].setType("Hi");
qDebug() << b[0].getLayers().at(0).getType();
}
Then change getLayers() to return "QList<layer>&" and look at the difference.

terhje
19th May 2011, 13:01
Thanks for taking the time to help me ChrisW67. :)