Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamic array object constructor logical error
Forum Updated to NodeBB v4.3 + New Features

Dynamic array object constructor logical error

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 502 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kycho
    wrote on last edited by Kycho
    #1

    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.!

    sierdzioS 1 Reply Last reply
    0
    • K Kycho

      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.!

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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 say Room::count is static.

      In other words, every time your call createRoom you create a new object and put it's address to room[9] (I assume Room::count value is either 8 or 9).

      (Z(:^

      K 2 Replies Last reply
      4
      • sierdzioS sierdzio

        @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 say Room::count is static.

        In other words, every time your call createRoom you create a new object and put it's address to room[9] (I assume Room::count value is either 8 or 9).

        K Offline
        K Offline
        Kycho
        wrote on last edited by
        #3

        @sierdzio

        Thank you very much!

        It was a great help to solve.

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          @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 say Room::count is static.

          In other words, every time your call createRoom you create a new object and put it's address to room[9] (I assume Room::count value is either 8 or 9).

          K Offline
          K Offline
          Kycho
          wrote on last edited by
          #4

          @sierdzio

          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;
          
          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved