Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32

Thread: How to Transparent QGraphicsView widget Background?

  1. #21
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to Transparent QGraphicsView widget Background?

    Have a look at Qt::WA_TranslucentBackground widget attribute.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #22
    Join Date
    Mar 2009
    Posts
    72
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    7

    Default Re: How to Transparent QGraphicsView widget Background?

    Qt Code:
    1. // ...
    2. view.setAttribute(QT::WA_TranslucentBackground);
    3. view.viewport()->setAutoFillBackground(false);
    To copy to clipboard, switch view to plain text mode 

  3. #23
    Join Date
    Dec 2009
    Posts
    128
    Platforms
    Unix/X11 Windows
    Thanks
    7
    Thanked 14 Times in 14 Posts

    Default Re: How to Transparent QGraphicsView widget Background?

    Thank you for replying, but unfortunately i'm not sure I've been clear enough. What i want to obtain is described by attached picture


    That is to say, i want to see underlying windows in my view




    wsh.jpg

  4. #24
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to Transparent QGraphicsView widget Background?

    Did you even try the code you were given?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #25
    Join Date
    Dec 2009
    Posts
    128
    Platforms
    Unix/X11 Windows
    Thanks
    7
    Thanked 14 Times in 14 Posts

    Default Re: How to Transparent QGraphicsView widget Background?

    yes obviously, i even tried that way before posting.

    using for my QGraphicsView subclass :

    Qt Code:
    1. MyView::MyView()
    2. {
    3. // constructor...
    4. setAttribute(Qt::WA_TranslucentBackground);
    5. viewport()->setAutoFillBackground(false);
    6. }
    7.  
    8.  
    9. //----------------------------------------------------------------------------------
    10. void MyView::paintEvent(QPaintEvent * e)
    11. {
    12. QPainter p( viewport() );
    13.  
    14. p.save();
    15. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
    16. p.fillRect(this->rect(), Qt::transparent);
    17. p.restore();
    18.  
    19. // draw scene items
    20. QGraphicsView::paintEvent(e);
    21. }
    To copy to clipboard, switch view to plain text mode 

    and for my QMainWindow subclass :

    Qt Code:
    1. setStyleSheet("background: red") ;
    To copy to clipboard, switch view to plain text mode 




    result : i would like to see my desktop where it's red in the view around the scene, but i see red background.
    Question is : do i have to override my MainWindow:aintEvent() to be able to achieve this effect ?

    redbackground.png

  6. #26
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to Transparent QGraphicsView widget Background?

    The top-level widget you want to be transparent needs to have that flag set, not its most-child one.

    Note that you have to paint every area you want opaque yourself (you can see your image shines through over the calendar on the left).

    transparent.png

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. mw.setAttribute(Qt::WA_TranslucentBackground);
    6. QToolBar *tb = mw.addToolBar("xyz");
    7. mw.setCentralWidget(view);
    8. dw->setWidget(new QCalendarWidget);
    9. mw.addDockWidget(Qt::LeftDockWidgetArea, dw);
    10. view->setScene(&scene);
    11. QPalette p = view->viewport()->palette();
    12. p.setColor(QPalette::Base, Qt::transparent);
    13. view->viewport()->setPalette(p);
    14. scene.addRect(10,10,50,20, QPen(Qt::red), Qt::blue);
    15. mw.show();
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    totem (12th January 2010)

  8. #27
    Join Date
    Dec 2009
    Posts
    128
    Platforms
    Unix/X11 Windows
    Thanks
    7
    Thanked 14 Times in 14 Posts

    Default Re: How to Transparent QGraphicsView widget Background?

    Ok you solved my problem

    I just paste a modified-version of your sample code, because Windows needs some extra-flag to be set :
    Actually only Qt::FramelessWindowHint is needed (regarding documentation) but the others are usefull

    Qt Code:
    1. #include <QtGui>
    2. int main(int argc, char **argv)
    3. {
    4. QApplication app(argc, argv);
    5. mw.setAttribute(Qt::WA_TranslucentBackground);
    6. mw.setWindowFlags( Qt::FramelessWindowHint|Qt::WindowSystemMenuHint|Qt::WindowStaysOnTopHint ) ;
    7. QToolBar *tb = mw.addToolBar("xyz");
    8. mw.setCentralWidget(view);
    9. dw->setWidget(new QCalendarWidget);
    10. mw.addDockWidget(Qt::LeftDockWidgetArea, dw);
    11. view->setScene(&scene);
    12. QPalette p = view->viewport()->palette();
    13. p.setColor(QPalette::Base, Qt::transparent);
    14. view->viewport()->setPalette(p);
    15. scene.addRect(10,10,50,20, QPen(Qt::red), Qt::blue);
    16. mw.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Now I will try to paint every area I want opaque, and voilÃ

    thank you

  9. #28
    Join Date
    May 2007
    Posts
    106
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanked 3 Times in 3 Posts

    Default Re: How to Transparent QGraphicsView widget Background?

    Thanks to all of you guys for this useful post!! It works great.

    I want user to be able to peep through the whole UI and see something working behind application. henceforth, I tried doing:

    Qt Code:
    1. void MotionAlarm::setThroughTransparent(QWidget *widget)
    2. {
    3. if(widget==NULL)
    4. return;
    5. widget->setAttribute(Qt::WA_TranslucentBackground);
    6. setThroughTransparent(widget->parentWidget());
    7. }
    8.  
    9. void MotionAlarm::somefunction()
    10. {
    11. setThroughTransparent(graphicsView);
    12. }
    To copy to clipboard, switch view to plain text mode 

    It works but the problem is that even the blank areas on parent windows become transparent which should not happen! Is there any way, that I can make a hole in UI for a specific region (graphics view), see the desktop but still operate on graphics view?

    I tried doing it in the following way:

    Qt Code:
    1. void MotionAlarm::on_buttonReset_clicked()
    2. {
    3. QRegion region = graphicsView->visibleRegion();
    4. region.translate(graphicsView->pos());
    5. createHole(region);
    6. }
    7.  
    8. void MotionAlarm::createHole(QRegion region)
    9. {
    10. QRegion widgetRegion = visibleRegion();
    11. QRegion visibleReg = widgetRegion.subtracted(region);
    12. this->setMask(visibleReg);
    13. }
    To copy to clipboard, switch view to plain text mode 

    but then I lose all the control on graphics view and don't see any graphics items rendered as well as mouse events are lost!

  10. #29
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to Transparent QGraphicsView widget Background?

    Set a mask on the window (QWidget::setMask()).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #30
    Join Date
    May 2007
    Posts
    106
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanked 3 Times in 3 Posts

    Default Re: How to Transparent QGraphicsView widget Background?

    I did, but then I can not see Graphics items at all and mouse events are also not captured by application! Please see the function createHole()

  12. #31
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to Transparent QGraphicsView widget Background?

    Is "MotionAlarm" class object a top-level window?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #32
    Join Date
    May 2007
    Posts
    106
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60
    Thanked 3 Times in 3 Posts

    Default Re: How to Transparent QGraphicsView widget Background?

    For my current test application, yes. It may not be the top level widget/window in real application, but all the test cases which I have described belong to the test application only.

Similar Threads

  1. Graphics widget with background
    By ad5xj in forum Newbie
    Replies: 2
    Last Post: 24th August 2007, 16:29
  2. Creating a widget on a QGraphicsView
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 9th August 2007, 17:54
  3. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  4. Replies: 3
    Last Post: 8th December 2006, 18:51
  5. Replies: 1
    Last Post: 5th April 2006, 16:44

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
  •  
Qt is a trademark of The Qt Company.