why opening 2 windows?

280 views Asked by At

My QT application is opening two windows. One empty and one with the button1 showing. Could someone tell me why?

I've tried everything, but I can't find the source of the problem. If anyone can help me.

main.cpp

#include <QtWidgets>
#include "./mainwindow.h"

int main(int argc, char **argv)
{
    QApplication app (argc, argv);
    MainWindow myWindow;
    myWindow.show();
    return app.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>


class MainWindow : public QMainWindow
{
    Q_OBJECT

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

#endif // MAINWINDOW_H

mainwindow.cpp

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

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    QWidget *page1 = new QWidget();
    QWidget *page2 = new QWidget();
    QGridLayout *layout = new QGridLayout();
    QGridLayout *layout1 = new QGridLayout();
    QPushButton *button = new QPushButton("Página 1", page1);
    QPushButton *button1 = new QPushButton("Página 2", page2);
    button->show();
    button1->show();
    layout->addWidget(button, 0, 0);
    layout1->addWidget(button1, 0, 0);
    page1->setLayout(layout);
    page2->setLayout(layout1);
    layout->setColumnMinimumWidth(0, 30);
    QStackedWidget *mainContainer = new QStackedWidget(parent);
    mainContainer->addWidget(page1);
    mainContainer->addWidget(page2);
    mainContainer->setCurrentIndex(1);
}
4

There are 4 answers

0
Alexandre On BEST ANSWER

I think it comes from this line:

QStackedWidget *mainContainer = new QStackedWidget(parent);

You should give this instead of parent for the QStackedWidget, because the parent of your MainWindow is null, and you probably want your StackWidget to be included in your MainWindow I guess.

Also, you should probably set your main container as the central widget of the main window. Something like this should work:

QWidget *page1 = new QWidget(this);
QWidget *page2 = new QWidget(this);
QGridLayout *layout = new QGridLayout(page1);
QGridLayout *layout1 = new QGridLayout(page2);
QPushButton *button = new QPushButton("Página 1", page1);
QPushButton *button1 = new QPushButton("Página 2", page2);
layout->addWidget(button, 0, 0);
layout1->addWidget(button1, 0, 0);
layout->setColumnMinimumWidth(0, 30);
QStackedWidget *mainContainer = new QStackedWidget(this);
mainContainer->addWidget(page1);
mainContainer->addWidget(page2);
mainContainer->setCurrentIndex(1);
setCentralWidget(mainContainer);
0
ΦXocę 웃 Пepeúpa ツ On

why opening 2 windows?

in QT Buttons are widgets and widgets can be shown as stand alone objects in their own window by calling for example, the method show() like you did in button->show(); and button1->show();

1
bogdyname On

If you using QWidget::show() method for main window you don't need to use this method for all UI objects like buttons, list widgets, line edit and etc.

0
RobbieE On

The reason you're getting multiple windows showing is because parent is set to nullptr, meaning that both the MainWindow class, and the QStackedWidget instance are top-level widgets.

If you want the QStackWidget to be a child of the main widget, you need to give it a parent widget, the most appropriate would be the central widget.

Change the following line of your code:

QStackedWidget *mainContainer = new QStackedWidget(centralWidget());