Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6546281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:41:20+00:00 2026-05-25T11:41:20+00:00

When I was using QGridLayout to display my widgets, only the widget was shown

  • 0

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.

void Piece::paintEvent(QPaintEvent *)
{
    string image = ":/images/" + color + piece + ".png";
    pixmap.load(image.c_str());
    //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240)));

    QPainter paint(this);
    paint.drawPixmap(0, 0, pixmap);
} 

That’s how the image is displayed on my widget. When I used the code,

layout->addWidget(0,0,1,1);

the background is transparent. But when I use,

scene->addWidget(piece);

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

EDIT: I can’t figure this problem out at all… I tried using setAutoFillBackground(false); but that didn’t work. So my last hope was converting my whole class from a QWidget to a QGrahhicsItem. That didn’t work and the background of the image is still gray instead of transparent. If you can’t figure out what’s wrong with this code can someone please post or link me to an example of how to display an image with a transparent background using QGraphicsScene? Here is the original code, followed by the QGraphicsItem code, followed by my main function.

#include "headers/piece.h"
#include <QPainter>
#include <QMouseEvent>
#include <QBitmap>
#include <QCursor>
using namespace std;

Piece::Piece(string color, string piece, QWidget *parent) :
    QWidget(parent)
{
    this->piece = piece;
    this->color = color;
    this->setMaximumHeight(36);
    this->setMaximumWidth(36);
    x = 0;
    y = 0;
    setMouseTracking(false);
}

void Piece::paintEvent(QPaintEvent *)
{
    string image = ":/images/" + color + piece + ".png";
    pixmap.load(image.c_str());
    //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240)));

    QPainter paint(this);
    paint.drawPixmap(0, 0, pixmap);
}

void Piece::setPosition(int file, int rank)
{
    pixmap.load(":/images/whitepawn.png");
    QImage image = pixmap.toImage();
    x = (file-1)*50 + 18;// - image.width()/2;
    y = (rank-1)*50 + 18;// - image.height()/2;
    move(x, y);
}

void Piece::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
    x = event->globalX()-18;
    y = event->globalY()-18;
    move(x,y);
    }
}

.

#include "piece2.h"
#include <QPainter>
#include <QMouseEvent>
#include <QBitmap>
#include <QCursor>
#include <QGraphicsSceneMouseEvent>
using namespace std;

Piece2::Piece2(string color, string piece, QObject *parent) :
    QGraphicsItem()
{
    this->piece = piece;
    this->color = color;
    //this->setMaximumHeight(36);
    //this->setMaximumWidth(36);
    x = 0;
    y = 0;
    //setMouseTracking(false);
}

void Piece2::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    string image = ":/images/" + color + piece + ".png";
    pixmap.load(image.c_str());
    //pixmap.setMask(pixmap.createMaskFromColor(QColor(240, 240, 240)));

    //QPainter paint(this);
    painter->drawPixmap(0, 0, pixmap);
}

void Piece2::setPosition(int file, int rank)
{
    pixmap.load(":/images/whitepawn.png");
    QImage image = pixmap.toImage();
    x = (file-1)*50 + 18;// - image.width()/2;
    y = (rank-1)*50 + 18;// - image.height()/2;
    setPos(x, y);
}

void Piece2::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton)
    {
//  x = event->globalX()-18;
//  y = event->globalY()-18;
    setPos(x,y);
    }
}

.

#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "headers/board.h"
#include "headers/pawn.h"
#include "headers/knight.h"
#include "headers/bishop.h"
#include "headers/rook.h"
#include "headers/king.h"
#include "headers/queen.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QGraphicsScene *scene = new QGraphicsScene();
    QGraphicsView *view = new QGraphicsView();
    Board board;

    scene->addWidget(&board);
    scene->addWidget(board.pawn2);
    board.pawn2->setPosition(1,1);

    //view->viewport()->setPalette(QColor(Qt::transparent));
    //view->viewport()->setAutoFillBackground(false);
    view->setScene(scene);
    //view->setBackgroundRole(QPalette::NoRole);
    view->show();
    return app.exec();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:41:20+00:00Added an answer on May 25, 2026 at 11:41 am

    Did you try using a stylesheet to set the background transparency?

    yourWidget->setStyleSheet("background-color: transparent;");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I'm trying to implement image scaling in OpenGL using only glTexCoord2f() and glVertex2f() .
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C#, I need a class called User that has a username, password, active
EDIT #2 - I'm done the previous state part. Now I need to my
I've created a small PyQt based utility in Python that creates PNG graphs using
I am writing a Tetris-based game, and I am using a GridLayout to display
I have a problem which appears when using Ubuntu Linux/Gnome, but seemingly not on
I'm working on a GUI that requires a scrollable QGridLayout based on the size
Using layouts for me results in so much frustration that I end up using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.