Results 1 to 9 of 9

Thread: Widget showing up without a transparent background in QGraphicsView

  1. #1
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Widget showing up without a transparent background in QGraphicsView

    When I was using QGridLayout to display my widgets, only the widget was shown and the part of the image that was transparent was not shown. Now I switched to using QGraphicsScene and QGraphicsView, and now my images have a gray background wherever they used to be transparent.
    Qt Code:
    1. void Piece::paintEvent(QPaintEvent *)
    2. {
    3. string image = ":/images/" + color + piece + ".png";
    4. pixmap.load(image.c_str());
    5. //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240)));
    6.  
    7. QPainter paint(this);
    8. paint.drawPixmap(0, 0, pixmap);
    9. }
    To copy to clipboard, switch view to plain text mode 
    That's how the image is displayed on my widget. When I used the code,
    Qt Code:
    1. layout->addWidget(0,0,1,1);
    To copy to clipboard, switch view to plain text mode 
    the background is transparent. But when I use,
    Qt Code:
    1. scene->addWidget(piece);
    To copy to clipboard, switch view to plain text mode 
    The widget has a gray background. How can I make it transparent? The full code can be found here if necessary (probably won't be necessary): https://github.com/gsingh93/Chess

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Widget showing up without a transparent background in QGraphicsView

    You can either setAutoFillBackground() of the widget to false or make your widget a regular graphics item.
    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.


  3. #3
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget showing up without a transparent background in QGraphicsView

    Both of those solutions did not work. Here is my first try with QWidget:
    Qt Code:
    1. #include "headers/piece.h"
    2. #include <QPainter>
    3. #include <QMouseEvent>
    4. #include <QBitmap>
    5. #include <QCursor>
    6. using namespace std;
    7.  
    8. Piece::Piece(string color, string piece, QWidget *parent) :
    9. QWidget(parent)
    10. {
    11. this->piece = piece;
    12. this->color = color;
    13. this->setMaximumHeight(36);
    14. this->setMaximumWidth(36);
    15. x = 0;
    16. y = 0;
    17. setMouseTracking(false);
    18. }
    19.  
    20. void Piece::paintEvent(QPaintEvent *)
    21. {
    22. string image = ":/images/" + color + piece + ".png";
    23. pixmap.load(image.c_str());
    24. //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240)));
    25.  
    26. QPainter paint(this);
    27. paint.drawPixmap(0, 0, pixmap);
    28. }
    29.  
    30. void Piece::setPosition(int file, int rank)
    31. {
    32. pixmap.load(":/images/whitepawn.png");
    33. QImage image = pixmap.toImage();
    34. x = (file-1)*50 + 18;// - image.width()/2;
    35. y = (rank-1)*50 + 18;// - image.height()/2;
    36. move(x, y);
    37. }
    38.  
    39. void Piece::mouseMoveEvent(QMouseEvent *event)
    40. {
    41. if(event->buttons() == Qt::LeftButton)
    42. {
    43. x = event->globalX()-18;
    44. y = event->globalY()-18;
    45. move(x,y);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    Here's how it looks with QGraphicsItem:

    Qt Code:
    1. #include "piece2.h"
    2. #include <QPainter>
    3. #include <QMouseEvent>
    4. #include <QBitmap>
    5. #include <QCursor>
    6. #include <QGraphicsSceneMouseEvent>
    7. using namespace std;
    8.  
    9. Piece2::Piece2(string color, string piece, QObject *parent) :
    10. {
    11. this->piece = piece;
    12. this->color = color;
    13. //this->setMaximumHeight(36);
    14. //this->setMaximumWidth(36);
    15. x = 0;
    16. y = 0;
    17. //setMouseTracking(false);
    18. }
    19.  
    20. void Piece2::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    21. {
    22. string image = ":/images/" + color + piece + ".png";
    23. pixmap.load(image.c_str());
    24. //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240)));
    25.  
    26. //QPainter paint(this);
    27. painter->drawPixmap(0, 0, pixmap);
    28. }
    29.  
    30. void Piece2::setPosition(int file, int rank)
    31. {
    32. pixmap.load(":/images/whitepawn.png");
    33. QImage image = pixmap.toImage();
    34. x = (file-1)*50 + 18;// - image.width()/2;
    35. y = (rank-1)*50 + 18;// - image.height()/2;
    36. setPos(x, y);
    37. }
    38.  
    39. void Piece2::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    40. {
    41. if(event->buttons() == Qt::LeftButton)
    42. {
    43. // x = event->globalX()-18;
    44. // y = event->globalY()-18;
    45. setPos(x,y);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    And here's the main function:
    Qt Code:
    1. #include <QtGui>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4. #include "headers/board.h"
    5. #include "headers/pawn.h"
    6. #include "headers/knight.h"
    7. #include "headers/bishop.h"
    8. #include "headers/rook.h"
    9. #include "headers/king.h"
    10. #include "headers/queen.h"
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. QApplication app(argc, argv);
    15. Board board;
    16.  
    17. scene->addWidget(&board);
    18. scene->addWidget(board.pawn2);
    19. board.pawn2->setPosition(1,1);
    20.  
    21. //view->viewport()->setPalette(QColor(Qt::transparent));
    22. //view->viewport()->setAutoFillBackground(false);
    23. view->setScene(scene);
    24. //view->setBackgroundRole(QPalette::NoRole);
    25. view->show();
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    With everything I try the image still has a white background instead of transparent.

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

    Default Re: Widget showing up without a transparent background in QGraphicsView

    Are you sure the image has an alpha channel?
    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. #5
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget showing up without a transparent background in QGraphicsView

    Well it's a png image so I believe it does, I made it transparent myself in paint.net. And also it was working fine when I placed the image in a QGridLayout, but the background only showed up when using QGraphicsScene.


    Added after 12 minutes:


    The solution was board.pawn2->setStyleSheet("background-color: transparent;");
    Last edited by gsingh93; 4th September 2011 at 18:39.

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

    Default Re: Widget showing up without a transparent background in QGraphicsView

    Quote Originally Posted by gsingh93 View Post
    Well it's a png image so I believe it does, I made it transparent myself in paint.net. And also it was working fine when I placed the image in a QGridLayout, but the background only showed up when using QGraphicsScene.
    I'm more interested in the graphics item approach and not the widget approach. For the widget I told you to set auto fill background to false which you kindly ignored.
    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. #7
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget showing up without a transparent background in QGraphicsView

    For the widget I told you to set auto fill background to false which you kindly ignored.
    I didn't ignore that... Look in the posted code and you'll see it commented out, meaning I tried it and it didn't work.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Widget showing up without a transparent background in QGraphicsView

    I don't see anywhere that you set autoFillBackground of the Piece instance to false, could you point it to me please?
    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.


  9. #9
    Join Date
    Aug 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Widget showing up without a transparent background in QGraphicsView

    Sorry, I misinterpreted. I thought you meant set it on the view, not the piece, like: view->viewport()->setAutoFillBackground(false);
    But anyway, I just tried it out on an object that inherits piece using: king->setAutoFillBackground(false); // king is of type King, King inherits from Piece
    and that did not fix the background problem. However when I did: king->setStyleSheet("background-color: transparent;");
    then the background went away.

Similar Threads

  1. Replies: 2
    Last Post: 6th October 2010, 14:25
  2. How to Transparent QGraphicsView widget Background?
    By ashukla in forum Qt Programming
    Replies: 31
    Last Post: 6th March 2010, 11:35
  3. Draw transparent SVG in Background of Widget
    By soul_rebel in forum Qt Programming
    Replies: 0
    Last Post: 28th October 2008, 20:44
  4. Semi-Transparent Background on Widget?
    By JimDaniel in forum Qt Programming
    Replies: 3
    Last Post: 16th January 2008, 18:19
  5. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52

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.