Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 小部件背景在使用QGraphicsView时不透明,但在使用QGridLayout时透明_C++_Qt_Qt4_Qgraphicsview_Qt4.7 - Fatal编程技术网

C++ 小部件背景在使用QGraphicsView时不透明,但在使用QGridLayout时透明

C++ 小部件背景在使用QGraphicsView时不透明,但在使用QGridLayout时透明,c++,qt,qt4,qgraphicsview,qt4.7,C++,Qt,Qt4,Qgraphicsview,Qt4.7,当我使用QGridLayout显示我的小部件时,只显示小部件,而没有显示图像中透明的部分。现在我切换到使用QGraphicscene和QGraphicsView,现在我的图像在以前透明的地方都有灰色背景 void Piece::paintEvent(QPaintEvent *) { string image = ":/images/" + color + piece + ".png"; pixmap.load(image.c_str()); //pixmap.setMas

当我使用QGridLayout显示我的小部件时,只显示小部件,而没有显示图像中透明的部分。现在我切换到使用QGraphicscene和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);
小部件有灰色背景。如何使其透明?如有必要,可在此处找到完整代码(可能不需要):

编辑:我一点也不明白这个问题。。。我尝试使用setAutoFillBackground(false);但那没用。所以我最后的希望是将我的整个类从QWidget转换为QGrahhicsItem。这不起作用,图像的背景仍然是灰色而不是透明的。如果你不能找出这段代码的错误所在,请有人给我发帖子或链接到如何使用QGraphicscene显示透明背景图像的示例?下面是原始代码,后面是QGraphicsItem代码,后面是我的主要函数

#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);
    }
}
#包括“headers/piece.h”
#包括
#包括
#包括
#包括
使用名称空间std;
片段::片段(字符串颜色、字符串片段、QWidget*父项):
QWidget(父级)
{
这->件=件;
这个->颜色=颜色;
此->设置最大高度(36);
此->设置最大宽度(36);
x=0;
y=0;
setMouseTracking(假);
}
空件::paintEvent(QPaintEvent*)
{
字符串image=“:/images/”+color+piece+“.png”;
load(image.c_str());
//设置掩码(pixmap.createMaskFromColor(QColor(240240240));
QPainter油漆(本);
paint.drawPixmap(0,0,pixmap);
}
空块::设置位置(int文件,int秩)
{
加载(“:/images/whitepawn.png”);
QImage image=pixmap.toImage();
x=(文件-1)*50+18;//-image.width()/2;
y=(秩-1)*50+18;//-image.height()/2;
移动(x,y);
}
无效块::mouseMoveEvent(QMouseEvent*事件)
{
如果(事件->按钮()==Qt::LeftButton)
{
x=事件->全局ALX()-18;
y=event->globalY()-18;
移动(x,y);
}
}

#包括“piece2.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
Piece2::Piece2(字符串颜色、字符串片段、QObject*parent):
QGraphicsItem()
{
这->件=件;
这个->颜色=颜色;
//此->设置最大高度(36);
//此->设置最大宽度(36);
x=0;
y=0;
//setMouseTracking(假);
}
void Piece2::paint(QPainter*painter,const QStyleOptionGraphicsItem*选项,QWidget*小部件)
{
字符串image=“:/images/”+color+piece+“.png”;
load(image.c_str());
//设置掩码(pixmap.createMaskFromColor(QColor(240240240));
//QPainter油漆(本);
画师->绘图像素贴图(0,0,像素贴图);
}
void Piece2::setPosition(int文件,int秩)
{
加载(“:/images/whitepawn.png”);
QImage image=pixmap.toImage();
x=(文件-1)*50+18;//-image.width()/2;
y=(秩-1)*50+18;//-image.height()/2;
setPos(x,y);
}
void Piece2::mouseMoveEvent(QGraphicsSceneMouseEvent*事件)
{
如果(事件->按钮()==Qt::LeftButton)
{
//x=事件->全局ALX()-18;
//y=event->globalY()-18;
setPos(x,y);
}
}

#包括
#包括
#包括
#包括“标题/board.h”
#包括“标题/典当h”
#包括“标题/knight.h”
#包括“标题/bishop.h”
#包括“headers/rook.h”
#包括“标题/king.h”
#包括“headers/queen.h”
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
qgraphicscene*场景=新的qgraphicscene();
QGraphicsView*视图=新的QGraphicsView();
董事会;
场景->添加小部件(&B);
场景->添加小部件(board.pawn2);
板。pawn2->设置位置(1,1);
//视图->视口()->setPalette(QColor(Qt::transparent));
//视图->视口()->setAutoFillBackground(假);
查看->设置场景(场景);
//查看->设置背景角色(Qpalete::NoRole);
查看->显示();
返回app.exec();
}

是否尝试使用样式表设置背景透明度

yourWidget->setStyleSheet("background-color: transparent;");

哇!我可以发誓我已经试过了。我可能把参数搞乱了。无论如何,谢谢你,这个问题让我的进步停滞了一周!我不知道为什么这不是一个默认设置,但似乎布局使其默认透明,这也应该。
#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;");