简体   繁体   English

Qt GraphicsScene背景不变

[英]Qt GraphicsScene Background does not change

i am a newbie to Qt.i am handling canvas widgets through QGraphicsScene class.But i cant change the default white background of the widget.Here is the code.i have tried to use the QBrush to set the background.But it did not work.it remains white.what is the problem in the following code? 我是Qt的新手,我正在通过QGraphicsScene类处理画布小部件,但是我无法更改该小部件的默认白色背景,这是代码。我试图使用QBrush设置背景,但是没有用.it保持白色。以下代码中的问题是什么?

int main(int argc, char **argv){

    QApplication a(argc, argv);


    QGraphicsScene canvas;
    canvas.addText("Hello World");
    QColor *color=new QColor(0x70,0x80,0x50,255);
    QBrush *brush=new QBrush();
    brush->setColor(*color);
    canvas.setBackgroundBrush(*brush);

    QGraphicsView view(&canvas);
    view.show();




    return a.exec();


}

Try passing the color into the brush constructor instead of afterwards 尝试将颜色传递到画笔构造器中,而不是在之后传递

QBrush brush(QColor(0x70, 0x80, 0x50, 255));
canvas.setBackgroundBrush(brush);

Which will set the brush style to Qt::SolidPattern . 这会将画笔样式设置为Qt::SolidPattern The default brush constructor sets the style to Qt::NoBrush . 默认的画笔构造函数将样式设置为Qt::NoBrush See http://qt-project.org/doc/qt-4.8/qbrush.html#QBrush 参见http://qt-project.org/doc/qt-4.8/qbrush.html#QBrush

view.setStyleSheet("background-color: black;");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM