Results 1 to 4 of 4

Thread: QWidget, StackUnder and setWindowOpacity

  1. #1
    Join Date
    Dec 2010
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QWidget, StackUnder and setWindowOpacity

    Hi All,

    I am trying to create a few QWidgets (parent and a few siblings). The siblings overlap with each other and hence am using QWidget::stackUnder() to set the order. I also want to make some of these widgets translucent. So I need to use QWidget::setWindowOpacity(). But to use QWidget::setWindowOpacity() I have set the window flags to Qt::Window|Qt::FramelessWindowHint. And setting this flags break the stackUnder() order because stackUnder needs the widgets to be siblings. So as soon as I set the window flags to Qt::Window|Qt::FramelessWindowHint the z-index (stackUnder) ordering breaks.

    Can anyone tell me how to break this and let me use both z-index ordering (stackUnder) and also use the setWindowOpacity to make the widgets translucent.

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QWidget, StackUnder and setWindowOpacity

    Basically I am surprised that what you wanted to chieve didn't work out of the box.

    Play with the example by removing calls to stackUnder. See that semitransparent backgrounds work and not transparent line is drawn on top of underlying widgets.

    Qt Code:
    1. #include <QtGui>
    2. #include <QWidget>
    3.  
    4. class TestWidget : public QWidget
    5. {
    6.  
    7. public:
    8. TestWidget(QColor color, QWidget *parent) : QWidget(parent),m_color(color) {}
    9.  
    10. protected:
    11.  
    12. void paintEvent(QPaintEvent *)
    13. {
    14. QPainter p(this);
    15. QColor semiTrans = m_color;
    16. semiTrans.setAlpha(50);
    17. p.fillRect(rect(),semiTrans);
    18. p.fillRect(0,height()/2 - 5,width(),5, m_color);
    19. }
    20. private:
    21. QColor m_color;
    22. };
    23.  
    24. int main(int argc, char * argv[])
    25. {
    26. QApplication app (argc, argv);
    27.  
    28.  
    29. TestWidget l1(Qt::black,&w);
    30. l1.setGeometry(0,0,100,20);
    31. TestWidget l2(Qt::red,&w);
    32. l2.setGeometry(10,0,100,20);
    33. TestWidget l3(Qt::green,&w);
    34. l3.setGeometry(20,0,100,20);
    35. TestWidget l4(Qt::yellow,&w);
    36. l4.setGeometry(30,0,100,20);
    37.  
    38. l2.stackUnder(&l1);
    39. l3.stackUnder(&l2);
    40. l4.stackUnder(&l3);
    41.  
    42. w.resize(130, 40);
    43. w.show();
    44. return app.exec();
    45. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Rachol for this useful post:

    sky (28th June 2011)

  4. #3
    Join Date
    Dec 2010
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget, StackUnder and setWindowOpacity

    Thank you Rachol. You code sure works. But my application is a bit complicated. All my widgets are Phonon::VideoPlayers and I cannot overload the paintEvent of the videoWidget in the videoPlayer.

  5. #4
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: QWidget, StackUnder and setWindowOpacity

    I am not sure what you are trying to achieve but have a look at void QWidget::setMask ( const QBitmap & bitmap ) method and QImage VideoWidget::snapshot () const. Combining these 2 might help you maybe.

Similar Threads

  1. Replies: 1
    Last Post: 23rd June 2011, 14:45
  2. Problem applying setWindowOpacity to a custom Widget
    By yellowmat in forum Qt Programming
    Replies: 8
    Last Post: 1st November 2006, 11:05
  3. setWindowOpacity doesn't work!
    By nupul in forum Qt Programming
    Replies: 5
    Last Post: 21st April 2006, 19:28
  4. setWindowOpacity()
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 3rd February 2006, 15:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.