Reputation: 159
I've created base class - MainWindow. In this class I create object of another class "SecondWindow". By using object of SecondWindow I want to use function() method that is defined in base class. How can I do this?
I've tried to transfer my base class as a parameter to SecondWindow object but that wasn't working (probably becouse in MainWindow I have included "secondWindow.h" and in SecondWindow I tried to include "mainwindow.h" header files). Is there way to simply use functions/variables of base class?
mainwindow.h
#include<QMainWindow>
#include "secondwindow.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
SecondWindow *secondWindow;
void function();
}
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow()
{
secondWindow = new secondWindow();
function();
}
void MainWindow::function()
{
qDebug()<<"yes";
}
secondwindow.h
class SecondWindow : public QMainWindow
{
Q_OBJECT
public:
SecondWindow();
}
secondwindow.cpp
#include "secondwindow.h"
SecondWindow::SecondWindow()
{
//here I want to use function();
}
Upvotes: 0
Views: 111
Reputation: 2304
You would want to inherit your MainWindow class, which would then inherit QMainWindow()
So in secondwindow.h you would change
class SecondWindow : public QMainWindow
{
Q_OBJECT
public:
SecondWindow();
};
to
#include "mainwindow.h"
class SecondWindow : public MainWindow
{
Q_OBJECT
public:
SecondWindow();
};
And you will then be able to use function() in secondwindow.cpp
Also, you have another issue at hand and it's that your mainwindow uses a pointer to second window, but you will then have a circular dependence issue. (You need SecondWindow in order to compile MainWindow, however, you want SecondWindow to be able to use function()
).
To resolve this, you need to forward-declare SecondWindow in your MainWindow class.
In mainwindow.h remove #include "secondwindow.h"
and replace it with class SecondWindow;
Upvotes: 2