manmatha.roy
manmatha.roy

Reputation: 571

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?

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();


}

Upvotes: 1

Views: 3219

Answers (2)

Md. Minhazul Haque
Md. Minhazul Haque

Reputation: 638

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

Upvotes: -1

Kevin Tonon
Kevin Tonon

Reputation: 995

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. The default brush constructor sets the style to Qt::NoBrush. See http://qt-project.org/doc/qt-4.8/qbrush.html#QBrush

Upvotes: 6

Related Questions