How to get a class pointer without messing up data.
-
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
-
@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 .hand 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); -
@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.
-
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();