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. How to get a class pointer without messing up data.
QtWS25 Last Chance

How to get a class pointer without messing up data.

Scheduled Pinned Locked Moved Solved General and Desktop
inheritancepointerpointersclass
4 Posts 2 Posters 1.6k 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.
  • Basti46B Offline
    Basti46B Offline
    Basti46
    wrote on last edited by
    #1

    Hello guys, my question is how can i use the same class pointer (or is it called instance?) in two different classes. I have a SettingsDialog class, where i set the values for my serial port, then i have the serial port class, where i use these values to configure the serial port. Then in the mainwindow i'll handle the settings widget, e.g. open the window etc.. But using something like

    SettingsDialog p =new SettingsDialog(),  
    

    in both classes (Serialport.c and MainWindow.c) seems to mess up something because the serialport won't open from time to time. I started to delete the settings pointer in the mainwindow, so i only have the settings in my serialPort.c. But is there a way to use the same object in two classes. I tried to give the pointer of the settings to my mainWindow:

    SettingsDialog* SerialPort::getSettings()
    {
        return this->settings;
    }
    

    But then again, i have to use something like

    Serialport p =new Serialport();
    

    To get the serialport, which could mess up something different (because i already did this in another class). Can anyone help me getting this problem solved because i don't fully understand how this works. Thank you

    Thank you

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Basti46 said:

      Hi
      To use the same instance of a class ,
      in another class, you can simply
      give it the pointer.
      You can have SerialPort in main window and then give it to settings dialog;

      so i if you have
      SerialPort *MySerialPort; in the class in mainwindow .h

      and create new instance for it.
      MySerialPort = new SerialPort;

      Then you can do
      SettingsDialog p =new SettingsDialog(MySerialPort),

      and give it directly in constructor
      or u can make a setFunktion
      SettingsDialog p =new SettingsDialog(),
      p->setComPort(MySerialPort);

      Basti46B 1 Reply Last reply
      2
      • mrjjM mrjj

        @Basti46 said:

        Hi
        To use the same instance of a class ,
        in another class, you can simply
        give it the pointer.
        You can have SerialPort in main window and then give it to settings dialog;

        so i if you have
        SerialPort *MySerialPort; in the class in mainwindow .h

        and create new instance for it.
        MySerialPort = new SerialPort;

        Then you can do
        SettingsDialog p =new SettingsDialog(MySerialPort),

        and give it directly in constructor
        or u can make a setFunktion
        SettingsDialog p =new SettingsDialog(),
        p->setComPort(MySerialPort);

        Basti46B Offline
        Basti46B Offline
        Basti46
        wrote on last edited by Basti46
        #3

        @mrjj Thank you for your answer. Unfortunately I don't understand your answer and everything i tried did'nt work. Soo to make things clear, here is a code:

        //HEADER MainWindow:
        class SettingsDialog;
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        .....
        some functions...
        ......
        private:
        SettingsDialog *settings;
        //SOURCE MainWindow
        #include "MainWindow.h"
        #include "ui_MainWindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow),
        //----------here is the problem----------------------------------------------!
              settings(new SettingsDialog()),
        //---------------------------------------------------------------------------------!
        {
            ui->setupUi(this);
        }
        MainWindow::~MainWindow()
        {
            delete settings;
            delete ui;
        }
        //just an example
        void MainWindow::closeEvent(QCloseEvent *event)
        {
                if (settings->isVisible())
            {
                settings->close();
            }
        }
        //HEADDER Serialport
        class SerialPort : public QObject
        {
            Q_OBJECT
        ....
        //some more stoff
        .....
        private:
            SettingsDialog *settings;    
            QSerialPort *serialPort;               
            
        //SOURCE Serialport
        #include "SettingsDialog.h"
        SerialPort::SerialPort(QObject *parent)
            : QObject(parent),
        //----------------------------here is the problem------------------------!!
              settings(new SettingsDialog()),
        //---------------------------------------------------------------------------------!!
        {
        }
        //example function
        void SerialPort::openSerialPort()
        {
            SettingsDialog::Settings p = settings->settings();
            serialPort->setPortName(p.name);
        }
        

        Maybe you already answerd my question but i didn't see it yet.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          Just a note first
          Having
          class SerialPort : public QObject
          and then
          QSerialPort *serialPort;
          Where only diffence is capital S to the main class is just confusing.

          anyway, why is the issue with
          settings(new SettingsDialog()),

          did you declare it as
          void settings(SettingsDialog *mydialog)

          Im not sure what you dont understand.

          To use a pointer to a class in another class, you simply give the pointer to the other class and it can use it

          class B {};
          
          class A {
            B* mykeptBpointer;
            setB(B* myb) {
              mykeptBpointer = myb;
            }
            void DoB {
              mykeptBpointer->xxxx();
            };
          
            A mya;
            B* myb = new B;
            mya.setB(b);
          
          
          

          oh, is
          settings(new SettingsDialog())
          rather
          settings = new SettingsDialog();

          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