Results 1 to 6 of 6

Thread: transpaent widgets

  1. #1
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default transpaent widgets

    Hi,

    I need to put a widget on top of a QMainWindow (outside of the layout), and I would like the widget to be semi transparent. That widget will NOT BE a top modal window, so it should work everywhere.

    How can I do this? (styles maybe...?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: transpaent widgets

    No, this should be a combination of setGeometry and setWindowOpacity/QPainer::setOpacity( for the child widget ).

    As long as you don't want the child widget to get out of the boundaries of the main window, then this will work.

    Composition works just fine when the backdrop is a widget( here a main window ).

    Regards

  3. #3
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Re: transpaent widgets

    I am running these commands on the constructor of some widget, to test this code.

    Qt Code:
    1. QPushButton *b = new QPushButton( this );
    2. QRect r( 50, 100, 100, 25 );
    3. b->setGeometry( r );
    4. b->setWindowOpacity( 0.7 );
    5. //b->paintEngine()->painter()->setOpacity( 0.7 );
    To copy to clipboard, switch view to plain text mode 

    When I uncomment the last line, the application will die. Any hints...?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: transpaent widgets

    Qt Code:
    1. twidget::twidget(QWidget *parent, Qt::WFlags flags)
    2. : QMainWindow(parent, flags)
    3. {
    4. ui.setupUi(this);
    5. mTranspW = new QWidget();
    6. QPalette p = mTranspW->palette();
    7. p.setColor(QPalette::Window, Qt::blue);
    8. mTranspW->show();
    9. mTranspW->setPalette(p);
    10. mTranspW->setWindowOpacity(0.5);
    11. mTranspW->setFixedSize(200, 200);
    12. mTranspW->move(100, 100);
    13. }
    To copy to clipboard, switch view to plain text mode 

    It turns out that in 4.3 the the widget is composed even outside the parent widget bounds.

    In my example I did not set any window flags for the child. You might want it to be frameless, etc.

    Regards
    Attached Images Attached Images

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: transpaent widgets

    Quote Originally Posted by elcuco View Post
    I am running these commands on the constructor of some widget, to test this code.

    Qt Code:
    1. QPushButton *b = new QPushButton( this );
    2. QRect r( 50, 100, 100, 25 );
    3. b->setGeometry( r );
    4. b->setWindowOpacity( 0.7 );
    5. //b->paintEngine()->painter()->setOpacity( 0.7 );
    To copy to clipboard, switch view to plain text mode 
    When I uncomment the last line, the application will die. Any hints...?
    Well, you can't just access a painter that way. The painter will be valid only in paint event, otherwise it is null.

    Regards

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: transpaent widgets

    Here's even a better one. It allows you to drag the transparent widget around inside the main window.


    Qt Code:
    1. #include "twidget.h"
    2. #include <QPainter>
    3. #include <QMouseEvent>
    4.  
    5. twidget::twidget(QWidget *parent, Qt::WFlags flags)
    6. : QMainWindow(parent, flags)
    7. {
    8. ui.setupUi(this);
    9. mTranspW = new xwww(this);
    10. QPalette p = mTranspW->palette();
    11. mTranspW->setVisible(true);
    12. mTranspW->setFixedSize(200, 200);
    13. mTranspW->move(0, 0);
    14. }
    15.  
    16. twidget::~twidget()
    17. {
    18. }
    19.  
    20. void twidget::mousePressEvent(QMouseEvent *e)
    21. {
    22.  
    23. }
    24.  
    25. void twidget::mouseMoveEvent(QMouseEvent *e)
    26. {
    27. if( (e->buttons()&Qt::LeftButton))
    28. {
    29. QPoint pos = e->globalPos();
    30. QPoint topLeft = mapToGlobal(mTranspW->rect().topLeft());
    31. mTranspW->move(pos - topLeft);
    32. }
    33. }
    34.  
    35. xwww::xwww(QWidget* p)
    36. :QWidget(p, Qt::FramelessWindowHint | Qt::Widget)
    37. {
    38. }
    39.  
    40. xwww::~xwww()
    41. {
    42. }
    43.  
    44. void xwww::paintEvent(QPaintEvent* e)
    45. {
    46. QPainter p( this );
    47. p.setOpacity(0.5);
    48. p.fillRect(rect(), Qt::blue);
    49. }
    To copy to clipboard, switch view to plain text mode 

    The header:
    Qt Code:
    1. #ifndef TWIDGET_H
    2. #define TWIDGET_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_twidget.h"
    6.  
    7. class xwww : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. xwww(QWidget*);
    13. ~xwww();
    14. protected:
    15. virtual void paintEvent(QPaintEvent*);
    16. };
    17.  
    18.  
    19. class twidget : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. twidget(QWidget *parent = 0, Qt::WFlags flags = 0);
    25. ~twidget();
    26.  
    27. protected:
    28. virtual void mousePressEvent(QMouseEvent *e);
    29. virtual void mouseMoveEvent(QMouseEvent *e);
    30.  
    31. private:
    32. Ui::twidgetClass ui;
    33. xwww* mTranspW;
    34. };
    35. #endif // TWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    Regards

Similar Threads

  1. QLogText & QLogTable : 2 widgets to display text log
    By fcoiffie in forum Qt-based Software
    Replies: 7
    Last Post: 28th April 2019, 08:52
  2. New widgets after application begins
    By petty in forum Newbie
    Replies: 5
    Last Post: 8th May 2007, 13:10
  3. Performance in hiding/showing widgets
    By Paalrammer in forum Newbie
    Replies: 12
    Last Post: 14th February 2007, 19:57
  4. Replies: 11
    Last Post: 7th July 2006, 14:09
  5. Creating Widgets
    By hylke in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2006, 09:37

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.