-1

I'm working on an assignment using QT Creator and as a requirement I have to create my GUI without using QT Designer. I have a header file with basically just a constructor. I want to call show my MainWindow in the main.cpp file (and have included my mainwindow.h file) and I have the following code in my main function:

QApplication a(argc, argv);
MainWindow w;

w.show();

return a.exec();

I set up my entire gui in my main window.cpp file as follows:

QMainWindow window;
QWidget *widget = new QWidget(&window);
QVBoxLayout *layout = new QVBoxLayout(widget);

...

window.show();

I have tried to maybe use the MainWindow constructor differently in main.cpp file however the GUI only seems to show when I place my Gui code in the main.cpp file instead. I want to separate my GUI so that I don't have to work with a monolith

Lebo
  • 19
  • 2
  • 2
    Please [edit] your question to provide a [mcve]. As it stands there simply isn't enough context to understand the problem. – G.M. May 15 '23 at 09:53
  • I think there is a misunderstanding with the way you're trying to use `window`, see this for explanation: [What and where are the stack and heap?](https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap), and this: [Stack vs Heap Memory Allocation](https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/) – Abderrahmene Rayene Mihoub May 15 '23 at 11:28
  • And you seem to already have `MainWindow` as a `QMainWindow`, why are your trying to use another one instead of making your `widget` a child of `MainWindow`? – Abderrahmene Rayene Mihoub May 15 '23 at 11:30

1 Answers1

0

QT Designer is a tool to create code of widgets.If you use QT Designer create mainwindow.ui. The mainwindow.ui will later be converted to ui_mainwindow.h which is a normal C++ header file.The extra thing you need to do is to implement this header by yourself when you don't have QT Designer.

This is a sample of ui_mainwindow.h auto created from mainwindow.ui.

ui_mainwindow.h

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QMenuBar *menubar;
    QStatusBar *statusbar;
    QToolBar *mainToolBar;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(800, 600);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        MainWindow->setCentralWidget(centralwidget);
        menubar = new QMenuBar(MainWindow);
        menubar->setObjectName(QString::fromUtf8("menubar"));
        menubar->setGeometry(QRect(0, 0, 800, 22));
        MainWindow->setMenuBar(menubar);
        statusbar = new QStatusBar(MainWindow);
        statusbar->setObjectName(QString::fromUtf8("statusbar"));
        MainWindow->setStatusBar(statusbar);
        mainToolBar = new QToolBar(MainWindow);
        mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
        MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
        mainToolBar->setWindowTitle(QCoreApplication::translate("MainWindow", "toolBar", nullptr));
    } // retranslateUi

};

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

};
#endif // MAINWINDOW_H

mainwindow.cpp

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

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

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

It can be found that you can improve the mainwindow class yourself to achieve the same effect using QT Designer.

main.cpp

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

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

    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
class QWiddget;
class QHBoxLayout;
class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    QHBoxLayout *verticalLayout;
    QWidget* _widget1;
    QWidget* _widget2;

};

mainwindow.cpp

#include "mainwindow.h"
#include <QWidget>
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    verticalLayout = new QHBoxLayout(this);
    _widget1 = new QWidget(this);
    _widget2 = new QWidget(this);
    verticalLayout->addWidget(_widget1);
    verticalLayout->addWidget(_widget2);
}

MainWindow::~MainWindow(){
}
Qiuren
  • 19
  • 3