Unsolved Unable to copy the object which is centralwidget. And how can I reuse the removed central widget ?
-
I have the address of more than 3 objects of the different classes. These objects are using to set as central widget. I am able to set it as central widget. But I need to copy the current status of the object which is in the central widget. So I created a temporary object and copy the current status of the central widget to that temporary object then change the central widget to another object.After that, I assign the address of the temporary object to the previous pointer. But it is not copying. It is showing an error that trying to refer deleted the object.
How can I copy the central widget or how can I reuse the removed central widget?
Below I have given the code.ToolsForm *m_tlFmThmSl; ScanGUI *m_scanGUIObj; TuneHome *m_tuneHomeObj; ToolsForm m_tlFmThmSlTemp; ScanGUI m_scanGUIObjTemp; TuneHome m_tuneHomeObjTemp; m_tlFmThmSl = new ToolsForm(); m_scanGUIObj = new ScanGUI(); m_tuneHomeObj=new TuneHome(); this->setCentralWidget(m_scanGUIObj); m_scanGUIObjTemp=*m_scanGUIObj; this->setCentralWidget(m_tlFmThmSl); m_scanGUIObj=&m_scanGUIObjTemp; this->setCentralWidget(m_tlFmThmSl);
-
@BinuJanardhanan QObject derived classes are not copyable. Read "No Copy Constructor or Assignment Operator" in http://doc.qt.io/qt-5/qobject.html
Why do you need to copy? You could just have several widgets and switch between them as needed. -
@jsulm Switching is not working..While I am trying to switch the application unexpectedly going to close.
-
@BinuJanardhanan Did you create an instance of the widget before switching? In such cases you should debug to see what exactly happens. Most probably you're using a dangling pointer.
-
@jsulm I am very sorry I didn't get you, It is because of my lack knowledge. How can I create a new instance of the widget. Do you mean the new object creation? Actually, i need to reuse the same state of the object later.
-
@BinuJanardhanan "How can I create a new instance of the widget" - you're already doing it in your code:
m_tlFmThmSl = new ToolsForm();
What do you pass as parameter if you call this->setCentralWidget ? Can you show your code?
-
@jsulm Thanks for communication.
Below i have given the code.Hope this is what your expecting.ToolsForm *m_tlFmThmSl;
ScanGUI *m_scanGUIObj;
TuneHome *m_tuneHomeObj;
m_tlFmThmSl = new ToolsForm();
m_scanGUIObj = new ScanGUI();
m_tuneHomeObj=new TuneHome();//One event for scan gui is triggering
this->setCentralWidget(m_scanGUIObj);
//One event for another gui is triggering
this->setCentralWidget(m_tlFmThmSl);
//SCAN GUI showing event again triggered
this->setCentralWidget(m_scanGUIObj);
While I am trying to do this, the application exit with an error that "unexpectedly stopped the application.
Actually, i want to switch the central widget. i need to reuse the previous state of the object. -
@BinuJanardhanan When exactly (at which line) does it crash? Did you try to debug as I already suggested before?
-
@jsulm in the last line of above code.
-
@BinuJanardhanan Please read documentation http://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget
"Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time."
This means: when you call this->setCentralWidget(m_tlFmThmSl); the previous central widget (m_scanGUIObj) is deleted. Later you're trying to set m_scanGUIObj again as central widget but the pointer is pointing to a deleted object!
This means: you need to create the widget each time you want to set it as central widget. If you want to keep its state (what do you actually mean by state?) you will need to store the state somewhere and read it again when creating the widget. -
@jsulm Yes exactly, I have tried for that.
First, i tried to use stacked widget.I added my widgets to the stacked widget and stacked widget set as central widget.Then I change the current index of stacked widget appropriately. It is worked up to the 2nd index.when I am trying to set the current index of the stacked widget as 3rd then it is not showing in the central widget. I have given below the code.
m_tlFmThmSl = new ToolsForm();
m_scanGUIObj = new ScanGUI();
m_tuneHomeObj=new TuneHome();
m_scanCurStaObj = new ScanCurrentStatus();stackedWidget = new QStackedWidget();
stackedWidget->insertWidget(0,m_tlFmThmSl);
stackedWidget->insertWidget(1,m_scanGUIObj);
stackedWidget->insertWidget(2,m_scanCurStaObj);
stackedWidget->insertWidget(3,m_tuneHomeObj);
this->setCentralWidget(stackedWidget);//When app is loading current index of stacked widget is set to 0
stackedWidget->setCurrentIndex(0);//One event for scan gui is triggering
stackedWidget->setCurrentIndex(1);//One event for scan current is triggering
stackedWidget->setCurrentIndex(2);//One event for tune home is triggering
stackedWidget->setCurrentIndex(3);in the above code, the 3rd index is not setting.so it is not showing in the central widget. 0 to 2 are working. I have tried via change the index of the widget in stacked widget then also the same. Which one is in 3rd index position that is not showing.
What is the issue in the stacked widget?Second, I have tried by copy the present object in the central widget to the temporary area for reuse. It is also not working properly.
I have given the code of that below.
ToolsForm *m_tlFmThmSl;
ScanGUI *m_scanGUIObj;
TuneHome *m_tuneHomeObj;ToolsForm m_tlFmThmSlTemp;
ScanGUI m_scanGUIObjTemp;
TuneHome m_tuneHomeObjTemp;m_tlFmThmSl = new ToolsForm();
m_scanGUIObj = new ScanGUI();
m_tuneHomeObj=new TuneHome();
this->setCentralWidget(m_scanGUIObj);
m_scanGUIObjTemp=*m_scanGUIObj;
this->setCentralWidget(m_tlFmThmSl);
m_scanGUIObj=&m_scanGUIObjTemp;
this->setCentralWidget(m_tlFmThmSl);How can i copy the centralwidget to other area and ho can i reuse it ?
Thank you in advance.
-
@BinuJanardhanan I don't know why it is not working with index 3. Is there anything in m_tuneHomeObj? You need to investigate more on this issue - in general it should work.
As I said: QObject derived classes cannot be copied - so no need to try to copy the central widget.