c – 使用QGraphicsView时窗口小部件背景不透明但使用QGridLayout时透明
作者:互联网
当我使用QGridLayout显示我的小部件时,只显示了小部件,并且未显示透明的图像部分.现在我切换到使用QGraphicsScene和QGraphicsView,现在我的图像在任何地方都是透明的灰色背景.
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);
}
这就是图像在我的小部件上的显示方式.当我使用代码时,
layout->addWidget(0,0,1,1);
背景是透明的.但是当我用的时候,
scene->addWidget(piece);
小部件具有灰色背景.我怎样才能让它透明?如有必要,可在此处找到完整代码(可能不需要):https://github.com/gsingh93/Chess
编辑:我根本无法解决这个问题…我尝试使用setAutoFillBackground(false);但那没用.所以我最后的希望是将我的全班从QWidget转换为QGrahhicsItem.这不起作用,图像的背景仍然是灰色而不是透明.如果您无法弄清楚此代码有什么问题,有人可以发布或链接我的示例如何使用QGraphicsScene显示具有透明背景的图像?这是原始代码,后跟QGraphicsItem代码,后跟我的main函数.
#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();
}
解决方法:
您是否尝试使用样式表来设置背景透明度?
yourWidget->setStyleSheet("background-color: transparent;");
标签:qgraphicsview,c,qt,qt4,qt4-7 来源: https://codeday.me/bug/20190723/1515231.html