0

I have been trying to run the code. It runs successfully but and I can see the popup window but it doesn't show anything. You can see the popup image below.

This is what pops up
enter image description here

And you can see my code below:

This is the Header file

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QWidget>
#include <QSpinBox>
#include <QSlider>
#include <QDial>
#include <QProgressBar>

class MainWindow : public QMainWindow
{
    Q_OBJECT
private:
    QSpinBox *spinbox;
    QSlider *slider1;
    QDial *dial;
    QProgressBar *progressbar;

public:
    MainWindow();
    //~MainWindow();
};
#endif // MAINWINDOW_H

This is the MainWindow file

#include "mainwindow.h"
#include <QVBoxLayout>
#include <QGridLayout>

MainWindow::MainWindow()
{
    spinbox = new QSpinBox;
    slider1 = new QSlider(Qt::Horizontal);
    dial = new QDial;
    progressbar = new QProgressBar;

    QVBoxLayout *lay1 = new QVBoxLayout(this);
    //QGridLayout *lay1 = new QGridLayout(this);

    lay1 -> addWidget(spinbox);
    lay1 -> addWidget(slider1);
    lay1 -> addWidget(dial);
    lay1 -> addWidget(progressbar);

    setLayout(lay1);
}

This is the Main file

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
Pawara Siriwardhane
  • 1,873
  • 10
  • 26
  • 38
  • Please don't post your images as links -- you can insert pics right into your question when you create it. – Joseph Larson Oct 26 '21 at 17:34
  • As for why your window is blank -- don't you need to do it like `foo = new Foo(this)` so your objects have a parent? – Joseph Larson Oct 26 '21 at 17:35
  • Fixed it, still doesn't work – Ftjgsrhhjhfd Oct 26 '21 at 17:39
  • 2
    Reading https://doc.qt.io/qt-5/qmainwindow.html#details It's look like your MainWindow miss a central widget – APianist Oct 26 '21 at 17:40
  • 1
    Do you not see any warnings at the console? You shouldn't call `setLayout` in a `QMainWindow` -- it already has its own layout manager to handle toolbars, dock widgets etc. Use [`QMainWindow::setCentralWidget`](https://doc.qt.io/qt-5/qmainwindow.html#setCentralWidget) instead. – G.M. Oct 26 '21 at 17:41
  • @G.M. I have actually ran this code in another computer and it worked. I don't know why it doesn't work in my computer – Ftjgsrhhjhfd Oct 26 '21 at 18:01

0 Answers0