Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, February 2007
The case of the disappearing widget


Message 1 in thread

I am having some trouble displaying plain QWidget with a color as a child of another.  Below is a simple reproducer of the problem (a test program I created):

class MyWin: public QWidget
{
    Q_OBJECT

public:
    MyWin(QWidget *parent = NULL)
      :
        QWidget(parent)
    {
        QPalette palette;
        palette.setColor(QPalette::Window, QColor("white"));
        palette.setColor(QPalette::WindowText, QColor("black"));
        setPalette(palette);
        resize(300, 300);
        
        
        QWidget *wid= new QWidget(this);

        palette = wid->palette();
        palette.setColor(QPalette::Window,     QColor("red"));
        palette.setColor(QPalette::WindowText, QColor("white"));
        wid->setPalette(palette);
        wid->resize(100, 100);
        wid->move(20, 20);
        
        QLabel *label = new QLabel("This is a label", wid);
        label->move(2, 2);
    }
};



The second QWidget briefly flashes, then disappears... Why?

Below is main:

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
     
    MyWin win;
    win.show();
    return app.exec();
}




Thanks in advance,
Susan

_____________________________ 
Susan Macchia 
mailto:susan@xxxxxxxxxxxx 
http://www.smacchia.net 
_____________________________ 




Message 2 in thread

On Tuesday 27 February 2007 14:35, Susan Macchia wrote:
> I am having some trouble displaying plain QWidget with a color as a child
> of another.  Below is a simple reproducer of the problem (a test program I
> created):
>
> class MyWin: public QWidget
> {
>     Q_OBJECT
>
> public:
>     MyWin(QWidget *parent = NULL)
>
>         QWidget(parent)
>     {
>         QPalette palette;
>         palette.setColor(QPalette::Window, QColor("white"));
>         palette.setColor(QPalette::WindowText, QColor("black"));
>         setPalette(palette);
>         resize(300, 300);
>
>
>         QWidget *wid= new QWidget(this);
>
>         palette = wid->palette();
>         palette.setColor(QPalette::Window,     QColor("red"));
>         palette.setColor(QPalette::WindowText, QColor("white"));
>         wid->setPalette(palette);
>         wid->resize(100, 100);
>         wid->move(20, 20);
>
>         QLabel *label = new QLabel("This is a label", wid);
>         label->move(2, 2);
>     }
> };
>
>
>
> The second QWidget briefly flashes, then disappears... Why?
>
> Below is main:
>
> int main(int argc, char **argv)
> {
>     QApplication app(argc, argv);
>
>     MyWin win;
>     win.show();
>     return app.exec();
> }
>
>
>
>
> Thanks in advance,
> Susan

You forgot to set the main widget.  QApplication::setMainWidget() or some 
such.

--
 [ signature omitted ] 

Message 3 in thread

> I am having some trouble displaying plain QWidget with a color as a child of
> another.  Below is a simple reproducer of the problem (a test program I created):

Susan, I ran into this exact same problem with some QPushButtons in an app I was
playing around with a few months ago.  It turns out that you have to set the palette
parameters in a very specific way (unfortunately, I don't remember what I did).

To figure it out, I fired up Qt designer and put a couple of frames and labels
together using the same palette options I was doing in my code.  Then I used Ctrl-R
to actually preview them and sure enough I would see the "flash" and the colors
would disappear.  After a few iterations of palette options, I was able to make it
work.  I remember the solution being pretty unexpected.

Hope that helps some,
Caleb

--
 [ signature omitted ] 

Message 4 in thread

Chris Thompson wrote:
>On Tuesday 27 February 2007 14:35, Susan Macchia wrote:
>> I am having some trouble displaying plain QWidget with a color as a child
>> of another.  Below is a simple reproducer of the problem (a test program I
>> created):
>>
>> class MyWin: public QWidget
>> {
>>     Q_OBJECT
>>
>> public:
>>     MyWin(QWidget *parent = NULL)
>>
>>         QWidget(parent)
>>     {
>>         QPalette palette;
>>         palette.setColor(QPalette::Window, QColor("white"));
>>         palette.setColor(QPalette::WindowText, QColor("black"));
>>         setPalette(palette);
>>         resize(300, 300);
>>
>>
>>         QWidget *wid= new QWidget(this);
>>
>>         palette = wid->palette();
>>         palette.setColor(QPalette::Window,     QColor("red"));
>>         palette.setColor(QPalette::WindowText, QColor("white"));
>>         wid->setPalette(palette);
>>         wid->resize(100, 100);
>>         wid->move(20, 20);
>>
>>         QLabel *label = new QLabel("This is a label", wid);
>>         label->move(2, 2);
>>     }
>> };
>>
>>
>>
>> The second QWidget briefly flashes, then disappears... Why?
>>
>> Below is main:
>>
>> int main(int argc, char **argv)
>> {
>>     QApplication app(argc, argv);
>>
>>     MyWin win;
>>     win.show();
>>     return app.exec();
>> }
>>
>>
>>
>>
>> Thanks in advance,
>> Susan
>
>You forgot to set the main widget.  QApplication::setMainWidget() or some
>such.

Hmm - I've never *had* to do that.  The examples in the Qt book also don't always show doing this.  But I can give it a try.  Note that The main widget "win" does show up...




--
 [ signature omitted ] 

Message 5 in thread

On Tuesday 27 February 2007 15:29, susan@xxxxxxxxxxxx wrote:
> Chris Thompson wrote:
> >On Tuesday 27 February 2007 14:35, Susan Macchia wrote:
> >> I am having some trouble displaying plain QWidget with a color as a
> >> child of another.  Below is a simple reproducer of the problem (a test
> >> program I created):
> >>
> >> class MyWin: public QWidget
> >> {
> >>     Q_OBJECT
> >>
> >> public:
> >>     MyWin(QWidget *parent = NULL)
> >>
> >>         QWidget(parent)
> >>     {
> >>         QPalette palette;
> >>         palette.setColor(QPalette::Window, QColor("white"));
> >>         palette.setColor(QPalette::WindowText, QColor("black"));
> >>         setPalette(palette);
> >>         resize(300, 300);
> >>
> >>
> >>         QWidget *wid= new QWidget(this);
> >>
> >>         palette = wid->palette();
> >>         palette.setColor(QPalette::Window,     QColor("red"));
> >>         palette.setColor(QPalette::WindowText, QColor("white"));
> >>         wid->setPalette(palette);
> >>         wid->resize(100, 100);
> >>         wid->move(20, 20);
> >>
> >>         QLabel *label = new QLabel("This is a label", wid);
> >>         label->move(2, 2);
> >>     }
> >> };
> >>
> >>
> >>
> >> The second QWidget briefly flashes, then disappears... Why?
> >>
> >> Below is main:
> >>
> >> int main(int argc, char **argv)
> >> {
> >>     QApplication app(argc, argv);
> >>
> >>     MyWin win;
> >>     win.show();
> >>     return app.exec();
> >> }
> >>
> >>
> >>
> >>
> >> Thanks in advance,
> >> Susan
> >
> >You forgot to set the main widget.  QApplication::setMainWidget() or some
> >such.
>
> Hmm - I've never *had* to do that.  The examples in the Qt book also don't
> always show doing this.  But I can give it a try.  Note that The main
> widget "win" does show up...

Sorry, I was probably thinking of something else.

--
 [ signature omitted ] 

Message 6 in thread

>> I am having some trouble displaying plain QWidget with a color as a child of
>> another.  Below is a simple reproducer of the problem (a test program 
>>I created):
>
>Susan, I ran into this exact same problem with some QPushButtons in an app I was
>playing around with a few months ago.  It turns out that you have to set the palette
>parameters in a very specific way (unfortunately, I don't remember what I did).
>
>To figure it out, I fired up Qt designer and put a couple of frames and labels
>together using the same palette options I was doing in my code.  Then I used Ctrl-R
>to actually preview them and sure enough I would see the "flash" and the colors
>would disappear.  After a few iterations of palette options, I was able to make it
>work.  I remember the solution being pretty unexpected.
>
>Hope that helps some,
>Caleb

Thanks, I'll give that a try.  Pretty weird though.  I assume your think that my setting the palette of the child widget is the culprit?  Or is it also the top level widget?

-- Susan




--
 [ signature omitted ] 

Message 7 in thread

>
> Thanks, I'll give that a try.  Pretty weird though.  I assume your  
> think that my setting the palette of the child widget is the  
> culprit?  Or is it also the top level widget?
>
> -- Susan

Correct on the child widget.  I'm used to thinking the way of Qt3  
where you have foreground and background colors instead of color  
roles.  I was setting the button text color and the "window" color  
and I could see it reflect on the buttons for a split second before  
reverting to the color of the parent widget.  There was some kind of  
palette trickery involved, but I sure don't remember the specifics.

Caleb

--
 [ signature omitted ]