QTextEdit transparency in QGraphicsView

  • I've been trying to make a transparent QTextEdit in a QGraphicsView. However, the background of the QTextEdit is always white. After lots of trial and error I found that text->setFrameShape(QFrame::NoFrame) gets rid of the white background.

    1. Why is QFrame::NoFrame needed?
    2. Why is the coloring different (see image)?

    QPlainTextEdit exhibits the same issue.
    QLabel and QLineEdit don't have the issue


    Qt 5.9.1
    Windows 10
    Visual Studio 2015



    QMainWindow w;
    w.resize(400, 100);
    w.setStyleSheet(".QMainWindow{background : black;}");
    QWidget bar(&w);
    bar.setGeometry(0, 0, 400, 40);
    bar.setStyleSheet("background-color : rgb(0,255,0);");
    QTextEdit text1(&w);
    text1.setStyleSheet("background-color : rgba(255,0,0,50%); color : white;");
    text1.setGeometry(0, 0, 80, 80);
    QGraphicsScene scene;
    QGraphicsView view(&w);
    view.setGeometry(100, 0, 300, 100);
    view.setStyleSheet(".QGraphicsView{background : transparent;}");
    QTextEdit text2;
    text2.setStyleSheet("background-color : rgba(255,0,0,50%); color : black;");
    text2.setGeometry(0, 0, 80, 80);
    QTextEdit text3;
    text3.setText("Almost Working");
    text3.setStyleSheet("background-color : rgba(255,0,0,50%); color : white;");
    text3.setGeometry(100, 0, 80, 80);
    QFrame frame;
    frame.setStyleSheet("background-color : rgba(255,0,0,50%);");
    frame.setGeometry(200, 0, 80, 80);

  • Moderators

    i also encountered this a few times and debugging showed that this relies somewhere in the depths of QStyle imeplementations. Sometimes you need to style border and background together. So i bet it would also work when you add border:0; to your stylesheets.

  • Wow that also works. Thanks

  • Just wondering...why not QGraphicsTextItem? Wrapping a QWidget into a QGraphicsScene is always costly and complicated.

  • @Asperamanca
    I wanted QTextEdit's scrollbar.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.