Home > Net >  Access widgets in QMainWindow from another class
Access widgets in QMainWindow from another class

Time:10-14

Although it is a frequently asked question, and I've tried many ways including those from SO, like Trying to access widgets of MainWindow from another class, However I still cannot work out a solution, below is my code which reported error "Unknown type name 'CustomClass'" in mainwindow.h: Thanks in advance for any help!

customclass.h

#ifndef CUSTOMCLASS_H
#define CUSTOMCLASS_H

#include "mainwindow.h"
#include "ui_mainwindow.h"

class MainWindow;

class CustomClass
{
public:
    CustomClass(MainWindow *parent);
    MainWindow * mainWindow;
    void testFunc();
};

#endif // CUSTOMCLASS_H

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "customclass.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    Ui::MainWindow *ui;
    CustomClass *customClass = new CustomClass(this);
};
#endif // MAINWINDOW_H

customclass.cpp

#include "customclass.h"

CustomClass::CustomClass(MainWindow *parent)
{
    this->mainWindow = parent;
}

void CustomClass::testFunc()
{
    mainWindow->ui->label->setText("Hello World!");
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    customClass->testFunc();
}

MainWindow::~MainWindow()
{
    delete ui;
}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

CodePudding user response:

please read about Circular Dependencies in C .

you have problems because you create a loop, you included #include "customclass.h" in MainWindow class and also #include "mainwindow.h" in CustomClass .

it's better that you don't use MainWindow in other classes and add CustomClass objects in MainWindow. The idea is that MainWindow is your root window class and we create one object from it in main.cpp you can do what you ask but logically it's not good.

as we can see in QMainWindow Class document.

The QMainWindow class provides a main application window

A main window provides a framework for building an application's user interface.

This is your base, you should add the feature you want inside this class not add it in other widgets. It's the main class for other widgets. This is a clean way to code and if you look at big projects in GitHub you will see this.

CodePudding user response:

move #include "mainwindow.h" and #include "ui_mainwindow.h" inside customclass.h into customclass.cpp before #include "customclass.h" and it works! Thank you!@drescherjm

The fixed code is shown below:

customclass.h

#ifndef CUSTOMCLASS_H
#define CUSTOMCLASS_H

class MainWindow;

class CustomClass
{
public:
    CustomClass(MainWindow *parent);
    ~CustomClass();
    MainWindow *mainWindow;
    void testFunc();
};

#endif // CUSTOMCLASS_H

customclass.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"//Move these two lines from the header to here
#include "customclass.h"

CustomClass::CustomClass(MainWindow *parent)
{
    this->mainWindow = parent;
}

CustomClass::~CustomClass()
{
}

void CustomClass::testFunc()
{
    mainWindow->ui->label->setText("Hello World!");
}
  • Related