PDA

View Full Version : QVBoxLayout whats wrong in this



sujan.dasmahapatra
25th March 2011, 08:17
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QPalette>
int main(int argc, char **argv)
{
QApplication app(argc,argv);
QWidget *widget = new QWidget;
widget->resize(500,300);

QPalette r(widget->palette());
r.setColor(QPalette::Background, Qt::red);
widget->setPalette(r);


QWidget *w1 = new QWidget(widget);
w1->resize(300,150);
QPalette p(w1->palette());
p.setColor(QPalette::Background, Qt::green);
w1->setPalette(p);

QWidget *w2 = new QWidget(widget);
w2->resize(300,150);
QPalette q(w2->palette());
q.setColor(QPalette::Background, Qt::blue);
w2->setPalette(q);

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(w1);
layout->addWidget(w2);

widget->setLayout(layout);

widget->show();

return app.exec();
}

I am not able to see the distinguished two colored widgets.

wladek
25th March 2011, 08:32
Hi sujan.dasmahapatra,

QWidget objects act as a container, and since your child widgets (w1 and w2) do not contain anything, they are not visible.
Try putting some objects into them.

Wladek.

Zlatomir
25th March 2011, 08:38
The palette is propagated from parent to child, so all your widgets will get red background, read more here (http://doc.qt.nokia.com/4.7-snapshot/qwidget.html#palette-prop)

BalaQT
25th March 2011, 08:51
hi,

I am not able to see the distinguished two colored widgets.
you can try stylesheet

widget->setStyleSheet("background-color:red;");
w1->setStyleSheet("background-color:green;");
w2->setStyleSheet("background-color:blue;");

hope it helps,
Bala

wladek
25th March 2011, 08:53
I would like to apologize for my reply, was a little "off targeted".