我在这里尝试的是使 CancelCross 成为 Notifier 类的子级,但是当我这样做时,CancelCross 的十字架是不可见的。但是,如果我取消它的父母,那么它可以很好地看到。我也尝试使用 QMainWindow 来养育它,但没有运气。我的代码有问题吗?
class CloseCrosss(QtGui.QFrame):
def __init__(self, QWidgetparent = None):
QtGui.QFrame.__init__(self, parent=QWidgetparent)
def paintEvent(self, e):
qp = QtGui.QPainter()
pen = QtGui.QPen(Qt.red)
qp.begin(self)
qp.setPen(pen)
qp.drawLine(460, 10, 470, 20)
qp.drawLine(470, 10, 460, 20)
qp.end()
class Notifier(QtGui.QWidget):
def __init__(self, message=None):
QtGui.QWidget.__init__(self)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)
_desktop = QtGui.QApplication.desktop()
size = _desktop.screenGeometry()
self.resize(size.width() * 25 / 100, size.height() * 3 / 100)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.main_layout = QtGui.QVBoxLayout()
self.move(size.width() - (size.width() * 25 / 100) - 10, 50)
self.main_layout.setContentsMargins(0, 0, 0, 0)
self.main_layout.setSpacing(0)
self.label = QtGui.QLabel(message)
self.label.setStyleSheet("color: lightgreen; font: 14px;")
self.label.setIndent(20)
self.main_layout.addWidget(self.label)
self.setLayout(self.main_layout)
def paintEvent(self, event):
s = self.size()
qp = QtGui.QPainter()
qp.begin(self)
qp.setRenderHint(QtGui.QPainter.Antialiasing, True)
qp.setPen(QtGui.QColor(255, 255, 255, 200))
qp.setBrush(QtGui.QColor(0, 0, 0, 200))
qp.drawRoundedRect(0, 0, s.width(), s.height(), 5, 5)
qp.end()
def closeEvent(self):
self.kill()
if __name__ == "__main__":
QApp = QtGui.QApplication(sys.argv)
window = Notifier()
cross = CloseCrosss(QWidgetparent=window)
window.show()
cross.show()
QApp.exec_()