Solved Reaching a pointer from another function
-
Hello, I am creating a new main window when user clicks to a button:
void secDialog::on_button_logo2_clicked() { MainWindow *mainwin = new MainWindow; mainwin->show(); }
And in another function I want to resize and move this main window to some spesific location. Which is in here :
void secDialog::on_pushButton_4_clicked() { QSize size = qApp->screens()[0]->size()+qApp->screens()[1]->size(); int height= this->height(); int width= this->width(); this->setGeometry(10,10,this->width(),this->height()); main->setGeometry(size.width()/2, 10, SecDialog->width(), SecDialog->height()); }
The problem is I don't know what I need to write in this second function instead of main. main->Setgeometry part is giving me error. How can I use the mainwindow I just created in the second fecond in here?
PS: I am very new to both QT and C++. -
Make mainwin a member pointer.
-
And how can I make it a member pointer?
-
@GunkutA said in Reaching a pointer from another function:
And how can I make it a member pointer?
Hm, like this (C++ basics):
class secDialog { private: MainWindow *mainwin;
-
@jsulm But in this way I have to create that in the header file right? So I will only be able to create it once. But I want to create this pointer everytime user presses the button. And when I try to create it, it says unknown type name 'MainWindow'
-
@GunkutA said in Reaching a pointer from another function:
it says unknown type name 'MainWindow'
Have you include the header file in which MainWindow is declared?
-
@KroMignon I couldn't because as I believe we cannot include a header file into another header file.
-
@GunkutA said in Reaching a pointer from another function:
it says unknown type name 'MainWindow'
Again C++ basics.
Add this:class MainWindow; // Add this line class secDialog { private: MainWindow *mainwin;
"But I want to create this pointer" - the pointer or object it is pointing to?
It is not clear what you want. Do you want to create 1..n instance of your MainWindow? Are you going to have more than one MainWindow instances at the same time? This would be unusual design as an application typically has only one main window. -
@GunkutA said in Reaching a pointer from another function:
I believe we cannot include a header file into another header file
Of course you can as long as you don't invent circular dependencies.
-
@jsulm said in Reaching a pointer from another function:
Do you want to create 1..n instance of your MainWindow?
I want to create the MainWindow when the user presses the button. That's why I believe I shouldn't create it in the header file.
-
@GunkutA "I want to create the MainWindow when the user presses the button" - that I already know, no need to repeat.
You didn't answer one of my questions: are you going to have more than one main window instances at the same time or only one? If only one then my suggestion is perfectly valid. If you want to have more than one instance at the same time then you could doclass MainWindow; class secDialog { private: std::vector<MainWindow*> mainwin;
"That's why I believe I shouldn't create it in the header file." - adding a pointer in header file does not create main window by the way...