Solved Call widget of mainwindow in an other class
-
#include <QApplication> #include "mainwindow.h" #include "engine/usapp_app.h" #include "engine/workingset.h" #include "splash.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); // MainWindow w; // w.showMaximized(); Splash w; w.show();
I wrote this in the main and the class splash must call the mainWindow:
void Splash::on_pushButton_2_clicked() { close(); ptr->stackedWidget_3->setCurrentIndex(3); g.showMaximized(); }
in Spash.h:
private: MainWindow g; Ui::MainWindow *ptr; Ui::Splash *ui; };
but the program crashes but I don't know
-
Hi
Are you sure it crashes ?
Not just closing?
try for testvoid Splash::on_pushButton_2_clicked() { g.showMaximized(); }
and see if mainwindow shows.
-
@mrjj if I write only this works, but why in the other way doesn't work, the application writes crashes
-
@vale88
I dont think it crashes i think it just closes as there is no top window.
QApplication handles that.you can try
int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setQuitOnLastWindowClosed(false); // MainWindow w; // w.showMaximized(); Splash w; w.show();
and see if that helps.
-
Hello! I think the problem is with not initialized pointer -
Ui::MainWindow *ptr;
. For example try to initialize it in theSplash
constructor.Example:
ptr = new Ui::MainWindow();
Also don't forget to
delete ptr
in theSplash
destructor later.By the way, try to attach the debugger to detect the issue.
-
@Cobra91151 I wrote:
void Splash::on_pushButton_2_clicked() { ptr = new Ui::MainWindow; close(); g.showMaximized(); ptr->stackedWidget_3->setCurrentIndex(3); //emit invio_segnaleM(); }
but it crashes
-
First, the problem with your code is, that every time you press the button (button click) new object generates and it create the memory leak.
Set
ptr = new Ui::MainWindow;
to theSplash
constructor to fix it and delete it in the destructor.Secondly, if it still crashes, try to debug your application with attached debugger.
-
@Cobra91151 I tried with debug but I on't understand, it says that the inferior is...
-
What is your
compiler
andOS
? Attach the screenshot of the issue from debugger here. -
Hi
Why are you creating
void Splash::on_pushButton_2_clicked()
{
ptr = new Ui::MainWindow; <<< WHY ? the g already done it
close();
g.showMaximized();
// this is not good. = ptr->stackedWidget_3->setCurrentIndex(3);
//should be
g.SetPage(3); // SetPage(int index) being a new function that does ui->stackedWidget_3->setCurrentIndex(index);
//emit invio_segnaleM();
} -
@mrjj yes but if I want to acceed to an object of the mainwindow, like a pushButton, how must I do?
-
@vale88
Well you can make the UI public. Bad design.
Or simply add public functions to MainWindow that does what you want.
like
g.SomethingWithButton();inside SomethingWithButton(), you have access to its UI and can
do what ever you like.So make a few functions, that does what you need with MainWindows Widgets.
-
@mrjj I used connect and it works, but nw I try in this way
-
@vale88
The good part about using functions to access the private UI in MainWindow is
that rest of program dont know which widgets it has.
So if you change something in MainWindow, you only need to change one other place
in the (access) function. rest of the program needs no changes. -
@mrjj thanks it works