Arturito
Arturito

Reputation: 1

A widget inside another widget does not appear

I am writing Qt/C++ project and I created my mainwindow.ui in Qt Designer. I placed in mainwindow.ui an empty widget, which later I want to extend by putting there my widget written in code. This is my code:

Class which extends QWidget:

#pragma once
#include <QtGui>
#include <QWidget>

using namespace QtDataVisualization;

class GraphDataCreator : public QWidget
{
    Q_OBJECT

public:
    GraphDataCreator(QWidget* parent = 0);
    ~GraphDataCreator();
};

cpp of this class:

#include "GraphDataCreator.h"

GraphDataCreator::GraphDataCreator(QWidget* parent)
    : QWidget(parent)
{
    this->setStyleSheet("background-color:green;");   
}

MainWindow class:

#pragma once

#include <QMainWindow>
#include "ui_MainWindow.h"
#include "GraphDataCreator.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QMainWindow*parent = Q_NULLPTR);
    ~MainWindow();

private:
    Ui::MainWindow ui;
};

Source file of MainWindow class:

#include "MainWindow.h"
#include <string>
#include <QtGui>
#include "GraphDataCreator.h"

MainWindow::MainWindow(QMainWindow*parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    QGridLayout* layout = new QGridLayout(ui.Chart3DWidget);
    GraphDataCreator* chart3D = new GraphDataCreator(ui.Chart3DWidget);
    layout->addWidget(chart3D);


    QWidget* test_widget = new QWidget;
    test_widget->setStyleSheet("background-color: red;");
    layout->addWidget(test_widget);

    ui.Chart3DWidget->setLayout(layout);
}

I wanted to the whole widget be green in the UI, but it didn't appear, so I put there a test_widget with a red background and the result is that it is half nothing/half red. So the green widget exists there, but it is not visible.

Why is the green part not visible? And how to solve it?

Of course, names of classes are weird because I tried to do something else, and this explanation simplifies that problem.

Upvotes: 0

Views: 161

Answers (0)

Related Questions