PDA

View Full Version : Blacking out the screen



WinchellChung
19th March 2008, 20:19
My client wants something artsy. The application has a screen full of widgets. If the user clicks the "X" go-away button they want the main screen to be over laid with a 85% semi-transparent black, and have an "Are You Sure?" dialog appear.

I have the dialog working just fine, but my black overlay is not working. Probably due to a trivial newbie mistake.

The situation is that the main window contains a single QStacked widget, each page of which contains a complicated QWidget composed of numerous controls. In the main window I have sub-classed closeEvent(). This is the code I'm using to vainly to apply an 85% black:


int theIndex = m_stackedWidget->currentIndex();
QWidget *currentWidget = m_stackedWidget->widget(theIndex);
QWidget *crepuscularWidget = new QWidget(currentWidget);
crepuscularWidget->resize(currentWidget->size());
QPainter painter(crepuscularWidget);
painter.setOpacity(0.85);
painter.fillRect(crepuscularWidget->rect(), QBrush(Qt::black));
crepuscularWidget->repaint();
// then follows the code to put up the "Are You Sure?" modal dialog

Originally this was more compact, I've expanded it to aid debugging (e.g., the first line used to end in = m_stackedWidget->widget(m_stackedWidget->currentIndex()))

It doesn't work.

I've tried initializing crepuscularWidget with the main window, stackedWidget, as well as m_stackedWidget->widget(n). I've tried not using crepuscularWidget at all, instead initializing painter with the main window, stackedWidget, and m_stackedWidget->widget(n). I've used crepuscularWidget->repaint() and crepuscularWidget->update(). Nothing seems to work.

Can anybody see what I am doing wrong?

marcel
19th March 2008, 20:31
You have to call show() on the new widget :).

WinchellChung
19th March 2008, 20:42
Alas, I put in a crepuscularWidget->show() right before the crepuscularWidget->repaint(), but it had no effect. It still is not drawing. Thanks for the suggestion anyway, it was stupid of me to forget the show().

marcel
19th March 2008, 20:47
Oh... What was I thinking...
You can't paint a widget outside its paintEvent...

So, there's your reason.

WinchellChung
19th March 2008, 20:55
Ah, that would certainly do it. Thank you very much!

WinchellChung
19th March 2008, 21:27
Yes, I moved the drawing code into the CrepuscularWidget's paintEvent, and it is working perfectly. Thank you very much!