开发者

How to create full transparency in a QTextEdit

开发者 https://www.devze.com 2023-03-25 15:46 出处:网络
I have been trying for many days to figure out a way to create a transparent Qtextedit with opaque text. Because the term \"transparency\" is often ambiguous, I define Qtextedit\"transparency\" as bei

I have been trying for many days to figure out a way to create a transparent Qtextedit with opaque text. Because the term "transparency" is often ambiguous, I define Qtextedit"transparency" as being able to see the text in the Qtextedit overlaid upon whatever is directly behind the main window (such as the desktop background, windows media player etc.) If possible I would like to be able to set the transparency at various levels and cross system compatible, but this is not required.

I am an extreme beginner, as I have only been using pyqt4 for 3 weeks and python 3.x for a few months and this is all the experience with programming that I have obtained in my existence. I have been attempting to decipher the Pyqt documentation with regard to this matter, but it is written in a way that seems to assume that one has been a gui programer for decades, not to mention having knowlege of C++. Furthermore, when this question is asked online it never seems to be resolved in way that is either: a) well documented or b) generalizable

This is very surprising because it seems like a basic operation that people would want to do

This solution works but doesn't seem to be directly useful for anything but displaying transparent images. I also don't really understand it all that well, as simply changing the base class from QWidget to QMainWindow makes the whole thing fail

http://www.loopbacking.info/blog/2008/07/11/transparent-windows-howto/

The following link embodies the common ways people suggest to solve problems similar to this, their pitfalls and why they don't work, but unfortunately they use the C++ version of Qt and are also a bit advanced for my skills at this point.

http://www.qtcentre.org/threads/18072-How-to-set-Qt-window-transparent

My system is windows 7 ultimate 32 bit on a dell latitude d830 with a Quadro NVS 140 whose driver version is current as of this post (Verde 275.33) My version of Pyqt is 4.8 (PyQt-Py3.2-x86-gpl-4.8.5-1.exe Windows 32 bit installer) I am also using Python 3.2.1 (Open Source version)

A basic example of my code lies beneath with the relevant (and failed) lines commented out:

When I tried the commented out code the color I generally just saw blackness. Also, when I resized my windows the darkness would randomly change intensity and the display of the main window seemed to get corrupted when maximized.

I would greatly appreciate any help on this matter!

import sys
import PyQt4
from PyQt4 import QtGui, QtCore

class Transparent(QtGui.QMainWindow):
    def __init__(self,parent = None):
        QtGui.QMainWindow.__init__(self,parent)
        self.initialize()

    def initialize(self):
        #self.colorset(self,'Window',200,255,100,20) 
        #self.colorset(self,'Base',200,255,100,20)
        #self.setBackgroundRole(QtGui.QPalette.Base)
        #self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
        #self.setAutoFillBackground(True)
        #self.mask()
        self.setWindowTitle("Chernobyl-like Failure")

        self.answerlabel = QtGui.QLabel('Text Response Display')
        self.answerlabel.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Raised)
        self.answerlabel.setMinimumHeight(25)
        self.questionlabel = QtGui.QLabel("Question:")
        self.questionlabel.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Raised)

        self.questionbox = QtGui.QLineEdit()
        self.questionbox.setMinimumWidth(500)

        self.askbutton = QtGui.QPushButton("Ask it!")

        self.historybox = QtGui.QTextEdit('Question & Answer history will be displayed here')
        self.historybox.setReadOnly(True)
        #self.colorset(self.historybox,'Base',200,255,100,127) 
        self.grid = QtGui.QGridLayout()
        widgetlist = [['answerlabel',0,0,1,3],['questionlabel',1,0,1,1],
                      ['questionbox',1,1,1,1],['askbutton',1,2,1,1],['historybox',2,0,1,3]]
        for widget in widgetlist:
            self.grid.addWidget(eval("self.{0}".format(widget[0])),*widget[1:])

        self.centralwidget = QtGui.QFrame()
        self.centralwidget.setFrameStyle(QtGui.QFrame.Box|QtGui.QFrame.Raised)
        self.centralwidget.setLineWidth(5)
        self.centralwidget.setLayout(self.grid)
        #self.colorset(self.centralwidget,'Base',200,255,100,127) 
        self.setCentralWidget(self.centralwidget)

    def colorset(self,widget,part,h,s,l,a):
        pal = widget.palette()
        color = QtGui.QColor()
    开发者_如何学编程    color.setHsl(h,s,l,a)
        pal.setColor(eval('QtGui.QPalette.{0}'.format(part)),color)
        widget.setPalette(pal)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = Transparent()
    main_window.show()
    sys.exit(app.exec_())


To make your main window transparent, you have to set the Qt.WA_TranslucentBackground attribute (using setAttribute(Qt.WA_TranslucentBackground)). Under Windows, you also must set the Qt.FramelessWindowHint attribute on your main window. According to the docs, however, "The user cannot move or resize a borderless window via the window system." So, if you want that functionality, you have to implement it manually. Here is a thread giving an example of that in C++.

Once you have a transparent MainWindow you can control the opacity of it and any child widgets by setting the background color to an RGBA value. Here is a dumb example,

from PyQt4 import QtGui, QtCore
import sys

class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        frame = QtGui.QFrame(parent=self)
        frame.setStyleSheet("QFrame {background: rgba(0,255,0,20%)}")
        box=QtGui.QHBoxLayout()

        edit = QtGui.QTextEdit()
        edit.setStyleSheet("background: rgba(0,0,255,20%)")
        box.addWidget(edit)

        edit2=QtGui.QTextEdit()
        edit2.setStyleSheet("background: rgb(255,0,0)")
        box.addWidget(edit2)
        frame.setLayout(box)

        pushbutton = QtGui.QPushButton('Quit')
        pushbutton.clicked.connect(self.close)
        box.addWidget(pushbutton)

        self.setCentralWidget(frame)        

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()

    app.exec_()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号