1

I am trying to initialize some widgets of my MainWindow by another class Qt/C++. However, I do not succeed to access the elements of MainWindow from the other class.

I give you a simple example:

In this example, I try to update the text of the label of MainWindow to write "test". However, label is not known by initialization. Any idea of what I am doing wrong?

main.cpp

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

int main(int argc, char *argv[])
{
    QApplication::setStyle("Fusion");
    QApplication a(argc, argv);
    a.setStyle(QStyleFactory::create("Fusion"));
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

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

#include <string>
using namespace std;

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

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

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

initialization.cpp

#include "initialization.h"


initialization::initialization(QObject* parent): m_parent(parent)
{
    m_parent->label->setText("Test");
}

initialization.h

#ifndef INITIALIZATION_H
#define INITIALIZATION_H

#include "mainwindow.h"

class initialization
{
public:
    initialization(QObject*);
private:
    QObject* m_parent;
};

#endif // INITIALIZATION_H
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
froz
  • 163
  • 1
  • 12

1 Answers1

0

you are trying to find the widget label using the ui pointer, so it can be found only including the ui_mainwindow in the initializer...

your approach is kind of weird but you can do:

#include <QWidget>
#include "ui_mainwindow.h"
class Init
{
public:
    Init(Ui::MainWindow *ui);
};


#include "Init.h"

Init::Init(Ui::MainWindow *ui)
{
    ui->pushButton1s->setText("11111");
}

and pass the ui in the constructor:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),  ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Init a(ui);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • 1
    Thank you very much, it works. You say my approach is strange so maybe I dont do the things correctly. How do you proceed to control your GUI? You put all your functions in mainwindow.cpp or you control your widgets in other cpp files? – froz Apr 24 '19 at 18:39
  • every widget that belong to mainWindow should be controlled by the main window... – ΦXocę 웃 Пepeúpa ツ Apr 24 '19 at 18:42
  • specially because you give pointers to another classes and you will lose the control over it, you can never known if the init class delete the ui .... that will cause you several problems later... – ΦXocę 웃 Пepeúpa ツ Apr 24 '19 at 18:43
  • In this case I will follow your advice and put everything in mainwindow. But the risk is to have a 20 000 lines file which is quite difficult to understand. Thank you for your explanations – froz Apr 24 '19 at 18:44