Results 1 to 5 of 5

Thread: QwtPlot: how to set the canvas background colour to a semi-transparent?

  1. #1
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot: how to set the canvas background colour to a semi-transparent?

    Hi,

    I have a QwtPlot object and I want and need to change its canvas background colour. To that end, I do something like:

    Qt Code:
    1. QBrush brush = canvasBackground();
    2.  
    3. brush.setColor(myColor);
    4.  
    5. setCanvasBackground(brush);
    To copy to clipboard, switch view to plain text mode 
    This works fine with opaque colours, but if I have a semi-transparent colour (e.g. #3a108080), then the effective colour becomes that of its corresponding opaque colour (i.e. #108080). Yet, if I query the canvas background colour, I correctly get #3a108080...!?

    So, what am I doing wrong here?...

    FWIW, I have managed to get things to work as I want by replacing the above code with:

    Qt Code:
    1. setStyleSheet(QString("QwtPlotCanvas {"
    2. " background: %1;"
    3. "}").arg(myColor.name(QColor::HexArgb)));
    To copy to clipboard, switch view to plain text mode 
    Now, although it works fine, I must confess that I would rather avoid having to use style sheets. So, any idea how I achieve the same result using something like my original code?

    Cheers, Alan.

    Hi,

    I have a QwtPlot object and I want and need to change its canvas background colour. To that end, I do something like:

    Qt Code:
    1. QBrush brush = canvasBackground();
    2.  
    3. brush.setColor(myColor);
    4.  
    5. setCanvasBackground(brush);
    To copy to clipboard, switch view to plain text mode 
    This works fine with opaque colours, but if I have a semi-transparent colour (e.g. #3a108080), then the effective colour becomes that of its corresponding opaque colour (i.e. #108080). Yet, if I query the canvas background colour, I correctly get #3a108080...!?

    So, what am I doing wrong here?...

    FWIW, I have managed to get things to work as I want by replacing the above code with:

    Qt Code:
    1. setStyleSheet(QString("QwtPlotCanvas {"
    2. " background: %1;"
    3. "}").arg(myColor.name(QColor::HexArgb)));
    To copy to clipboard, switch view to plain text mode 
    Now, although it works fine, I must confess that I would rather avoid having to use style sheets. So, any idea how I achieve the same result using something like my original code?

    Cheers, Alan.


    Added after 1 49 minutes:


    Actually, to use style sheets is not right for what I want since I want the semi-transparent colour to be on a white background, while my QwtPlot object has a grey background...

    This means that, in the end, I am doing the following:

    Qt Code:
    1. static const QColor White = Qt::white;
    2.  
    3. QBrush brush = canvasBackground();
    4. double ratio = myColor.alpha()/256.0;
    5.  
    6. brush.setColor(QColor((1.0-ratio)*White.red()+ratio*myColor.red(),
    7. (1.0-ratio)*White.green()+ratio*myColor.green(),
    8. (1.0-ratio)*White.blue()+ratio*myColor.blue()));
    9.  
    10. setCanvasBackground(brush);
    To copy to clipboard, switch view to plain text mode 

    I guess I am happy with that...
    Last edited by agarny; 6th November 2017 at 18:38.

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot: how to set the canvas background colour to a semi-transparent?

    Not sure, what went wrong in your code - maybe running into the trap, that QColor kills the alpha value, when being constructed from a rgb ( QColor::fromRgba() needs to be used ).

    But when checking the simpleplot example I had set the background of the plot to blue. Then I added the following line:

    Qt Code:
    1. QColor bg( Qt::red );
    2. bg.setAlpha( 100 );
    3. plot->setCanvasBackground( bg );
    To copy to clipboard, switch view to plain text mode 
    The result was like expected - something between red and blue.

    Uwe

  3. #3
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot: how to set the canvas background colour to a semi-transparent?

    Hi Uwe,

    I just used your code in mine and it works fine on Windows, but not on Linux and macOS. From there, I used your code in the simpleplot example on macOS and here is what I got. As you can see, the canvas background is not right and and it gets worse when I resize the window. Anyway, as I mentioned before, I have got a working solution, so I guess I am 'fine'...

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot: how to set the canvas background colour to a semi-transparent?

    Quote Originally Posted by agarny View Post
    It works fine on Windows, but not on Linux and macOS.
    I'm on Linux only - never seen any over those others.

    But I had tested with Qt5, where X11 always uses the raster paint engine. Is your application on Qt4 using the X11 paint engine ?

    Uwe

  5. #5
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot: how to set the canvas background colour to a semi-transparent?

    Qwt and the simpleplot example were compiled using Qt 5.9.2. As for my application, it is also built using Qt 5.9.2 on Windows, Linux and macOS. Otherwise, in my QwtPlot-based class, I have the following in my constructor:

    Qt Code:
    1. if (QwtPainter::isX11GraphicsSystem())
    2. canvas()->setAttribute(Qt::WA_PaintOnScreen, true);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 17th December 2013, 13:19
  2. Replies: 8
    Last Post: 17th April 2011, 18:16
  3. Non-transparent QWidget on semi-transparent parent
    By EuroElessar in forum Qt Programming
    Replies: 0
    Last Post: 29th August 2008, 17:20
  4. Semi-Transparent Background on Widget?
    By JimDaniel in forum Qt Programming
    Replies: 3
    Last Post: 16th January 2008, 19:19
  5. QTextEdit with semi-transparent background
    By ber_44 in forum Qt Programming
    Replies: 6
    Last Post: 29th April 2007, 01:01

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.