开发者

Why don't my widgets get displayed?

开发者 https://www.devze.com 2023-01-15 20:51 出处:网络
I am trying to create layout like this In the QT i created widgets and put in the main widget, the problem is not visible. none of the widget\'s are not shown. please help me to fix this issue

I am trying to create layout like this

Why don't my widgets get displayed?

In the QT i created widgets and put in the main widget, the problem is not visible. none of the widget's are not shown. please help me to fix this issue

Complete Source Code sandbox.ifuturemec.com/test.zip

Source code mainWindow.cpp

#include "mainwindow.h"
#include <QtGui>
#include "headerbar.h"
#include <QGridLayout>
#include <QPushButton>
#include <QBoxLayout>
#include "statusbar.h"
#include "leftpanel.h"
#include "rightpanel.h"
#include "centerpanel.h"

mainWindow::mainWindow(QWidget *parent) : QWidget(parent)
{
   QGridLayout *layout=new QGridLayout(this);

    headerBar *Header= new headerBar(this);
    leftPanel *LeftPanel=new leftPanel(this);
    centerPanel *CenterPanel=new centerPanel(this);
    rightPanel *RightPanel=new rightPanel(this);
    statusBar *Status=new statusBar(this);

    QHBoxLayout *box=new QHBoxLayout();
    box->addWidget(LeftPanel);
    box->addWidget(CenterPanel);
    box->addWidget(RightPanel);

    layout->addWidget(Header,0,0);
    layout->addLayout(box,1,0);
    layout->addWidget(Status,2,0);

    setLayout(layout);
}

mainWindow::~mainWindow() {}

mainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>

class mainWindow : public QWidget
{
    Q_OBJECT
public:
    mainWindow(QWidget *parent = 0);
    ~mainWindow();

signals:

public slots:

};

#endif // MAINWINDOW_H

headerbar.cpp

#include "headerbar.h"
#include <QPushButton>
#include <QMessageBox>

headerBar::headerBar(QWidget *parent) : QWidget(parent)
{
    this->setMaximumHeight(24);
    this->setStyleSh开发者_C百科eet("background-color: rgb(85, 170, 255)");
}

headerBar::~headerBar(){}

headerbar.h

#ifndef HEADERBAR_H
#define HEADERBAR_H

#include <QWidget>

class headerBar : public QWidget
{
    Q_OBJECT
public:
    headerBar(QWidget *parent = 0);
    ~headerBar();

signals:

public slots:

};

#endif // HEADERBAR_H

Please help me to fix this problem.


Actually, your widgets do show ! But they are empty and have nothing to show !

Regarding the background color you are setting : the background-color property is not showing, because this property is not supported by QWidget.

Have a look at the documentation regarding this : List of stylable widgets.

More specifically :

QWidget : Supports only the background, background-clip and background-origin properties.

If you try to put, for instance, a label in your widgets, you'll see they do show :

centerPanel::centerPanel(QWidget *parent) :
    QWidget(parent)
{
    QHBoxLayout *box = new QHBoxLayout(this);
    QLabel* pLabel = new QLabel("Center panel", this);
    box->addWidget(pLabel);
    this->setStyleSheet("background-color: rgb(85, 100, 100)");
}

Why don't my widgets get displayed?


If you just want the mainWindow to have a solid background color, you might try to just forget using the stylesheet and override the paintEvent method like this:

void mainWindow::paintEvent(QPaintEvent *event)
{
setPalette(QPalette(QColor(85, 170, 255)));
setAutoFillBackground(true); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号