PDA

View Full Version : Promote Qwidget problem



oswalidos
15th April 2012, 23:09
Hello,

I'm using qt creator 2.4.1, Qt 4.8 on Ubuntu 10.10.
I need to make a background image resizable for a Qwidget, for the main QWidget i override resizeEvent in the generated file(widget.cpp) and it works fine, now i want to put inside my main QWidget another QWidget and give it a resizable background image as well so i found out the promoting to buid my custom widget (ZoneSup is just a QWidget where i override resizeEvent) .

my custom widget :


#ifndef ZONESUP_H
#define ZONESUP_H

#include <QFrame>
#include <QDebug>
#include <QResizeEvent>
#include <QPixmap>
#include <QPalette>


class ZoneSup : public QWidget
{
Q_OBJECT

public:
ZoneSup(QWidget *parent = 0);

protected:
void resizeEvent(QResizeEvent *); // virtual};
};
#endif // ZONESUP_H


#include "zonesup.h"

ZoneSup::ZoneSup(QWidget *parent) : QWidget(parent){

}

void ZoneSup::resizeEvent ( QResizeEvent * event )
{
qDebug() << "ZoneSup::resizeEvent " << event->size().width() << " ," << event->size().height();

QPalette palet = palette();
QPixmap backGroundPicture(":/image/bg1.jpg");
QPixmap pixmapBackGround(backGroundPicture.scaled(event->size()));

palet.setBrush(QPalette::Background, pixmapBackGround);
setPalette(palet);
}


resizevent is called but the background image is not shown.
the same code used for the main QWidget works fine.
I have no error and no warning.

Thanks in advance

oswalidos
16th April 2012, 22:44
i'm a beginner in Qt and i can't find a way to debug this problem, i'll try to make my problem more clear so maybe someone out there will help me,


/************************************************** ******************************
** Form generated from reading UI file 'widget.ui'
**
** Created: Mon Apr 16 22:29:26 2012
** by: Qt User Interface Compiler version 4.8.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
************************************************** ******************************/

#ifndef UI_WIDGET_H
#define UI_WIDGET_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QWidget>
#include "zonesup.h"

QT_BEGIN_NAMESPACE

class Ui_Widget
{
public:
ZoneSup *resizableWidgetWithBG;

void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(754, 531);
Widget->setWindowOpacity(1);
resizableWidgetWithBG = new ZoneSup(Widget);
resizableWidgetWithBG->setObjectName(QString::fromUtf8("resizableWidgetWithBG"));
resizableWidgetWithBG->setGeometry(QRect(150, 90, 221, 191));
resizableWidgetWithBG->setMaximumSize(QSize(221, 191));

retranslateUi(Widget);

QMetaObject::connectSlotsByName(Widget);
} // setupUi

void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
} // retranslateUi

};

namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_WIDGET_H

a background is not working, can i try to paint something else instead ? is it a way to find out the problem?

Any idea is welcome, Best Regards

ChrisW67
16th April 2012, 23:11
From the enum QPalette::ColorRole docs:


QPalette::Background Window This value is obsolete. Use Window instead.

So try that first.

QWidget::setAutoFillBackground() on the child widget straightens this out for me in a quick test. (I don't know if that is the "right" way to achieve this).

Also, when you resize the parent window the child widget resizeEvent() fires before the parent widget resizeEvent() on my test program. The parent widget background may be be overwriting the child background before they are repainted.

oswalidos
17th April 2012, 10:23
Thank you so much, QWidget::setAutoFillBackground() solved my problem, besides, in my project, parent widget resizeEvent fires before the child widget resizeEvent, and this is exactly what i need, so based on the new size of my parent window i'll resize the child window (or let the layout resize it if possible).