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.- Why is QFrame::NoFrame needed?
- Why is the coloring different (see image)?
QPlainTextEdit exhibits the same issue.
QLabel and QLineEdit don't have the issueEnvironment
Qt 5.9.1
Windows 10
Visual Studio 2015Example
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.setText("Working"); 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.setText("Broken"); text2.setStyleSheet("background-color : rgba(255,0,0,50%); color : black;"); text2.setGeometry(0, 0, 80, 80); scene.addWidget(&text2); QTextEdit text3; text3.setText("Almost Working"); text3.setStyleSheet("background-color : rgba(255,0,0,50%); color : white;"); text3.setGeometry(100, 0, 80, 80); text3.setFrameShape(QFrame::NoFrame); scene.addWidget(&text3); QFrame frame; frame.setStyleSheet("background-color : rgba(255,0,0,50%);"); frame.setGeometry(200, 0, 80, 80); scene.addWidget(&frame); view.setScene(&scene); w.show();
-
@David-Stephan
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 addborder: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.