PDA

View Full Version : Problem applying setWindowOpacity to a custom Widget



yellowmat
31st October 2006, 17:52
Hi everybody !

When I apply the setWindowOpacity method to the main window object of my application it works pretty well but not when I apply it to my custom widget, why ?

Both main window and custom widget inherits from QWidget no ? So why is it "effectless" on my widget ?

Shoul I do something specific in the paintEvent method of my widget ?

Thanks in advance.

Claymore
31st October 2006, 18:01
Did you try setting the WFlags argument to WType_TopLevel (or some other flag type) when you create the QWidget object? I'm not sure if that will help, but it may get the QWidget object to behave more like the QMainWindow object.

yellowmat
31st October 2006, 18:15
I have just tried to modify the flags at QWidget objet creation ... some changes happen :eek:

Now, my widget does not display anymore.

I have tryed with the following flags :


WStyle_Customize | WType_TopLevel


Perhaps your tips is the solution but I must find the good flags. I'm not sure if a top level window can owns a widget which is also top level ?

Claymore
31st October 2006, 18:26
Actually, I just tried setting the opacity of a QWidget in one of my projects and it worked just fine (even with the flags set to 0, which is the QWidget default).

Just to double-check, are you making sure that the opacity value you're using is between 0.0 and 1.0?

yellowmat
31st October 2006, 23:04
I checked the opacity value and it is valid because equal to 0.5.

Here are some more details about my application :
* it is an SDI application generated with the wizard
* I implement the init slot in which I set the palette background pixmap, I create a timer and connect its timeout signal to a custom slot, finally I create my custom widget and set its opacity to 0.5
* in the slot connected to the timer timeout signal I call the update method of my widget

Here is the specific code of my application :




void TransparentWidgetMainWindow::init()
{
// ...
setPaletteBackgroundPixmap(QPixmap("E:\\pix.png"));
resize(QSize(800, 480));

// ...
pWidget = new CMyWidget(this, "toto");
pWidget->setGeometry(10, 10, 140, 200);
pWidget->setFocusPolicy(QWidget::StrongFocus);
pWidget->setWindowOpacity(0.2);

// ...
pTimer = new QTimer(this);
connect(pTimer, SIGNAL(timeout()), this, SLOT(processTimerTimeout()));
pTimer->start(100);
}

void TransparentWidgetMainWindow::processTimerTimeout()
{
pWidget->update();
}


and here is the implementation code of my custom widget :


CMyWidget::CMyWidget(QWidget* parent, const char* name, WFlags f):
QWidget(parent, name, f),
m_pPixmap(0)
{
// ...
if( !m_pPixmap )
m_pPixmap = new QPixmap(QImage("E:\\\\anotherpix.png"));
}

CMyWidget::~CMyWidget()
{
if( m_pPixmap )
{
delete m_pPixmap;
m_pPixmap = 0;
}
}

void CMyWidget::paintEvent(QPaintEvent* ev)
{
QPainter p(this);

p.drawPixmap(ev->rect().x(), ev->rect().y(), *m_pPixmap, ev->rect().x(), ev->rect().y(), 140, 200);
}


The aim of this widget is not (for the moment) to be well designed, it is to be functional ... but in fact it is not yet functional.

As proposed in a previous answer of this post, I tried to set specific flag values passed in my widget constructor in order to configure it as a main window could be (it was proposed because a call setWindowOpacity has an effect on a main window), but settings my widget as a top level widget (as is my main window) does not solve my problem ... in fact I notice that the paintEvent method of my widget is never called.

I hope thoose details could help.

aamer4yu
1st November 2006, 04:02
I am not very good at Qt... but seeing ur code I can just hint at one thing...

in ur CWidget contructor, u are passing 3 arguments to the QWidget class... while in the documentation I saw that QWidget has only 2 parameters... so u r passing name as window flags for the QWidget class
I am not sure, but can u chk if thats the problem ??

Claymore
1st November 2006, 04:24
I am not very good at Qt... but seeing ur code I can just hint at one thing...

in ur CWidget contructor, u are passing 3 arguments to the QWidget class... while in the documentation I saw that QWidget has only 2 parameters... so u r passing name as window flags for the QWidget class
I am not sure, but can u chk if thats the problem ??

Actually, the constructor for QWidget can accept 3 arguments:
http://doc.trolltech.com/3.3/qwidget.html#QWidget

EDIT: aamer4yu, I just noticed that in Qt 4.0 you are correct - QWidget only takes 2 parameters:
http://doc.trolltech.com/4.2/qwidget.html#QWidget

I believe yellowmat's issue is dealing with Qt 3, correct? :) (or at least that's what the title of the thread points to)

My question for yellowmat is what is the CMyWidget class defaulting the WFlags f parameter to? In the header file for the class, do you have WFlags f set to a default value (like 0)? If not, it could be getting some random value and giving the object bogus flags to use.

Other than that, I don't see anything wrong with the code you've written...

yellowmat
1st November 2006, 09:39
The version of Qt librairies I use is 3.3.3 (or Qt 3 as specified in the post).

I give the header of my class and you can see I use default values for each parameter of my constructor.


class CMyWidget : public QWidget
{
public:
// ...
CMyWidget(QWidget* parent=0, const char* name=0, WFlags f=0);

// ...
~CMyWidget();

// ...
void paintEvent(QPaintEvent*);
private:
QPixmap* m_pPixmap;
};


I don't know why tunning opacity has no effect on my (a) widget ... I don't what to do, so if someone has a clue it would be great.

yellowmat
1st November 2006, 10:05
So ... I have seen a post of someone having the problem. He said he inherits is custom widget class from QDialog instead of QWidget and since this change he can control the opacity behaviour.

The problem is that since the custom inherits from QDialog instead of QWidget, it is not possible to manage it as a classic widget.

Is it possible to manage a dialog as a widget, for example integrating it in a layout and so on ?