Solved Dynamic array object constructor logical error
-
I found a logical error in the 'connect' function in the source below.
This source is compiled well.
but, if I define 'connect (room [0] ....)' there is an logical error that all 'room' array objects behave the same as 'room [0]'.
I do not know why this is an error.please help me... T^T......
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { settingUi(); objectConnect(); createRoom("101","20000","71000",Room::take_sleepRoom); // room[0] createRoom("102","20000","72000",Room::take_sleepRoom); // room[1] createRoom("103","20000","73000",Room::take_sleepRoom); // room[2] createRoom("105","20000","74000",Room::take_sleepRoom); // room[3] createRoom("106","20000","75000",Room::take_sleepRoom); // room[4] createRoom("107","20000","76000",Room::take_sleepRoom); // room[5] createRoom("201","25000","81000",Room::take_sleepRoom); // room[6] createRoom("202","25000","82000",Room::take_sleepRoom); // room[7] createRoom("203","25000","83000",Room::take_sleepRoom); // room[8] connect(room[0],&Room::loadPrice,this,&MainWindow::setPriceLCD); // This line is logical error!!! }
void MainWindow::createRoom(const QString &_roomId , QString _takePrice , QString _sleepPrice , int _sellType) { room[Room::count] = new Room(_roomId,_takePrice,_sleepPrice,_sellType); // Array object constructor // Room declaration = Room *room[1000]; connect(room[Room::count],&Room::loadNum,ui->roomselectImageLabel,&QLabel::setText); connect(room[Room::count],&Room::loadNum,ui->payRoomLabel,&QLabel::setText); connect(ui->sleepingRoomButton,&QPushButton::clicked,room[Room::count],&Room::sleepClicked); connect(ui->takeRoomButton,&QPushButton::clicked,room[Room::count],&Room::takeClicked); connect(room[Room::count],&Room::clicked,this,&MainWindow::seletedRoom); // 3*3 GridLayout setup int page = Room::count / 9; // count is static value int row = (Room::count % 9) / 3; int column = Room::count % 3; if(!roomPage[page]){ roomPage[page] = new QWidget; roomLay[page] = new QGridLayout; roomPage[page]->setLayout(roomLay[page]); ui->roomStack->addWidget(roomPage[page]); roomLay[page]->addWidget(room[Room::count],row,column); } else { roomLay[page]->addWidget(room[Room::count],row,column); } }
Thanks in advance for your help.!
-
@Kycho said in Dynamic array object constructor logical error:
but, if I define 'connect (room [0] ....)' there is an logical error that all 'room' array objects behave the same as 'room [0]'.
You mean all 9 objects get connected to that slot, instead of just the first one? Interesting.
room[Room::count] = new Room(_roomId,_takePrice,_sleepPrice,_sellType);
You do realise that you are creating 9 Room objects but assign only one of them to
room[Room::count]
, right? The other pointers are just free floating in memory. I base that on the fact that you sayRoom::count
is static.In other words, every time your call
createRoom
you create a new object and put it's address toroom[9]
(I assumeRoom::count
value is either 8 or 9). -
-
Fixed the error by correcting the definition of createRoom function as below.
thank you for your reply.
int page = (Room::count-1) / 9; // count is static value int row = ((Room::count-1) % 9) / 3; int column = (Room::count-1) % 3;