简体   繁体   中英

Include header main window to a child window in QT

I try to include header window into his child window. Let see a code:

//header of main window
#include "second_window.h" // include child window

class mainclass : public QMainWindow
{
    Q_OBJECT

public:
    mainclass(QWidget *parent = 0);
    ~mainclass();
    second_window *h_secondwindow = nullptr;

private:
    Ui::mainclass ui;

};

And Second

//header second_window.h
#include "mainwindow.h" // only this from mainwindow class
class second_window : public QWidget
{
    Q_OBJECT

public:
    third_window * h_third_window = nullptr;
    second_window(QWidget *parent = 0);
    ~second_window();

private:
    void reDrawTable();
    Ui::second_window ui;

};

#endif // second_window_H

And when in "second_window.h" i try to include "header of main window" (lets call it mainwindow.h )

I got error by this line (in main window) :

second_window *h_secondwindow = nullptr;

Errors like:

//qdatetime.h ( i dont edit this file ;0 )

error C2059: syntax error : '::'
static inline qint64 nullJd() { return std::numeric_limits<qint64>::min(); }

//mainwindow.h

error C2143: syntax error : missing ';' before '*'
second_window *h_secondwindow = nullptr;

I need this variable bcoz i have to operate in main window method on this child window.

Any idea?

Try, cross checking your "second_window.h" to see if there any code errors, it might be there are some few errors. Such errors mainly happen with pointers and references, so an error might be possible!

You shouldn't include the header files inside each other. You can use a forward declaration of mainwindow class in second_window.h :

//header second_window.h
class mainwindow;

Forward declarations mean that you can only use that class as a pointer in the header file and not a class instance. So using this way you can now have a pointer to your mainwindow in second_window.h .

But it is recommended to check if you can change your design and eliminate this circular dependency.

I think your code is dark means. Why does second_window include main_window? You should init in main window.h:

second_window* _secondWindowUi;

in .cpp file : _secondWindowUI = new second_window(); It is easy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM