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. Double pointer error // please help me
Forum Updated to NodeBB v4.3 + New Features

Double pointer error // please help me

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 430 Views
  • 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 aha_1980
    #1

    Hello, everyone.

    I used double pointers.
    And I'm in a strange situation that I do not understand, please help me.

    The problem is in the 'roomOnClick' function.

    (The original source was too long to be compressed as much as possible.)

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    
    namespace Ui {class MainWindow;}
    
    class Room;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
        Room *room[900];
        Room **selectRoom; 
    
    
    private slots:
        void roomOnClick(Room *ptr);
    
    
    signals:
    
    };
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "room.h"
    #include "billacceptor.h"
    #include "billdispensor.h"
    #include "receiptprinter.h"
    #include "network.h"
    #include <QDebug>
    #include <QTimer>
    #include <QSerialPortInfo>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        ui->stackedWidget->setCurrentWidget(ui->selectRoomPage);
        ui->sleepingRoomButton->setEnabled(false);
        ui->takeRoomButton->setEnabled(false);
    
        createRoom("101","100","500",Room::take_sleepRoom);
        createRoom("102","200","1000",Room::take_sleepRoom);
        createRoom("103","300","1500",Room::take_sleepRoom);
        createRoom("105","400","2000",Room::take_sleepRoom);
        createRoom("106","500","2500",Room::take_sleepRoom);
        createRoom("107","600","3000",Room::take_sleepRoom);
        createRoom("108","700","3500",Room::take_sleepRoom);
        createRoom("109","800","4000",Room::take_sleepRoom);
        createRoom("110","900","4500",Room::take_sleepRoom);
    }
    void MainWindow::roomOnClick(Room *ptr)
    {
        selectRoom = &ptr;
        ui->roomselectImageLabel->setText(selectRoom[0]->getRoomId());
        ui->sleepPriceLabel->setText(selectRoom[0]->getSleepPirce());
        ui->takePriceLabel->setText(selectRoom[0]->getTakePrice());
        ui->sleepingRoomButton->setEnabled(true);
        ui->takeRoomButton->setEnabled(true);
        qDebug() << selectRoom[0] << selectRoom;
    }
    void MainWindow::createRoom(const QString &_roomId , const QString &_takePrice ,
                                const QString &_sleepPrice , int _sellType)
    {
       room[Room::count] = new Room(_roomId,_takePrice,_sleepPrice,_sellType);
       connect(room[Room::count],&Room::roomClicked,this,&MainWindow::roomOnClick);
    
       // 3*3 GridLayout setup
       int page = Room::count / 9;
       int row = Room::count % 9 / 3;
       int column = Room::count % 3;
       if(row == 0 && column == 0){
           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);
    }
    
    

    'selectRoom' is a double pointer to 'room'.
    The 'selectRoom' (double pointer) inside the 'roomOnClick' function works as I intended.
    But, when the function 'roomOnClick' is terminated, the address value stored in 'selectRoom' is destroyed.

    I do not understand why it is annihilated.
    How do I solve this problem?
    Please help me.

    jsulmJ 1 Reply Last reply
    0
    • K Kycho

      Hello, everyone.

      I used double pointers.
      And I'm in a strange situation that I do not understand, please help me.

      The problem is in the 'roomOnClick' function.

      (The original source was too long to be compressed as much as possible.)

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      #include <QMainWindow>
      
      namespace Ui {class MainWindow;}
      
      class Room;
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
      
      private:
          Ui::MainWindow *ui;
          Room *room[900];
          Room **selectRoom; 
      
      
      private slots:
          void roomOnClick(Room *ptr);
      
      
      signals:
      
      };
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include "room.h"
      #include "billacceptor.h"
      #include "billdispensor.h"
      #include "receiptprinter.h"
      #include "network.h"
      #include <QDebug>
      #include <QTimer>
      #include <QSerialPortInfo>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          ui->stackedWidget->setCurrentWidget(ui->selectRoomPage);
          ui->sleepingRoomButton->setEnabled(false);
          ui->takeRoomButton->setEnabled(false);
      
          createRoom("101","100","500",Room::take_sleepRoom);
          createRoom("102","200","1000",Room::take_sleepRoom);
          createRoom("103","300","1500",Room::take_sleepRoom);
          createRoom("105","400","2000",Room::take_sleepRoom);
          createRoom("106","500","2500",Room::take_sleepRoom);
          createRoom("107","600","3000",Room::take_sleepRoom);
          createRoom("108","700","3500",Room::take_sleepRoom);
          createRoom("109","800","4000",Room::take_sleepRoom);
          createRoom("110","900","4500",Room::take_sleepRoom);
      }
      void MainWindow::roomOnClick(Room *ptr)
      {
          selectRoom = &ptr;
          ui->roomselectImageLabel->setText(selectRoom[0]->getRoomId());
          ui->sleepPriceLabel->setText(selectRoom[0]->getSleepPirce());
          ui->takePriceLabel->setText(selectRoom[0]->getTakePrice());
          ui->sleepingRoomButton->setEnabled(true);
          ui->takeRoomButton->setEnabled(true);
          qDebug() << selectRoom[0] << selectRoom;
      }
      void MainWindow::createRoom(const QString &_roomId , const QString &_takePrice ,
                                  const QString &_sleepPrice , int _sellType)
      {
         room[Room::count] = new Room(_roomId,_takePrice,_sleepPrice,_sellType);
         connect(room[Room::count],&Room::roomClicked,this,&MainWindow::roomOnClick);
      
         // 3*3 GridLayout setup
         int page = Room::count / 9;
         int row = Room::count % 9 / 3;
         int column = Room::count % 3;
         if(row == 0 && column == 0){
             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);
      }
      
      

      'selectRoom' is a double pointer to 'room'.
      The 'selectRoom' (double pointer) inside the 'roomOnClick' function works as I intended.
      But, when the function 'roomOnClick' is terminated, the address value stored in 'selectRoom' is destroyed.

      I do not understand why it is annihilated.
      How do I solve this problem?
      Please help me.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kycho Instead of messing up with raw pointers you should use a container like QVector to store your Room instances.
      So, I suggest to change

      Room *room[900];
      Room **selectRoom;
      

      to

      QVector<Room> rooms;
      int selectedRoomIndex;
      

      Also, why do you do

      selectRoom = &ptr;
      

      ?
      Isn't ptr a pointer to a specific Room instance? I really don't see a need for a pointer to a pointer here.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 2 Replies Last reply
      6
      • jsulmJ jsulm

        @Kycho Instead of messing up with raw pointers you should use a container like QVector to store your Room instances.
        So, I suggest to change

        Room *room[900];
        Room **selectRoom;
        

        to

        QVector<Room> rooms;
        int selectedRoomIndex;
        

        Also, why do you do

        selectRoom = &ptr;
        

        ?
        Isn't ptr a pointer to a specific Room instance? I really don't see a need for a pointer to a pointer here.

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

        @jsulm

        Thank you.
        I will change according to your advice.

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Kycho Instead of messing up with raw pointers you should use a container like QVector to store your Room instances.
          So, I suggest to change

          Room *room[900];
          Room **selectRoom;
          

          to

          QVector<Room> rooms;
          int selectedRoomIndex;
          

          Also, why do you do

          selectRoom = &ptr;
          

          ?
          Isn't ptr a pointer to a specific Room instance? I really don't see a need for a pointer to a pointer here.

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

          @jsulm

          I modified the source according to your advice and the problem is completely resolved.

          Thank you!!

          mainwindow.cpp

          void MainWindow::createRoom(const QString &_roomId , const QString &_takePrice ,
                                      const QString &_sleepPrice , int _sellType)
          {
              rooms.append(new Room(_roomId,_takePrice,_sleepPrice,_sellType));
              int currentRoomIndex = rooms.count()-1;
              connect(rooms.at(currentRoomIndex),&Room::roomClicked,this,&MainWindow::roomOnClick);
             // 3*3 GridLayout setup
              int page = currentRoomIndex / 9;
              int row = currentRoomIndex % 9 / 3;
              int column = currentRoomIndex % 3;
              if(row == 0 && column == 0){
                  roomPage[page] = new QWidget;
                  roomLay[page] = new QGridLayout;
                  roomPage[page]->setLayout(roomLay[page]);
                  ui->roomStack->addWidget(roomPage[page]);
              }
              roomLay[page]->addWidget(rooms.at(currentRoomIndex),row,column);
          }
          
          void MainWindow::roomOnClick(Room *ptr)
          {
              selectRoomIndex = rooms.indexOf(ptr);
              ui->roomselectImageLabel->setText(rooms.at(selectRoomIndex)->getRoomId());
              ui->sleepPriceLabel->setText(rooms.at(selectRoomIndex)->getSleepPirce());
              ui->takePriceLabel->setText(rooms.at(selectRoomIndex)->getTakePrice());
              ui->sleepingRoomButton->setEnabled(true);
              ui->takeRoomButton->setEnabled(true);
          }
          
          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