我在这里尝试的是使Notifier类成为CancelCross子级,但是当我这样做时,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_()
最佳答案
子代的位置是相对于其父代的位置,如果小部件没有父代,则其位置是相对于屏幕的。该坐标系用于绘制,例如,在您的情况下,qp.drawLine(460, 10, 470, 20)
表示您要绘制从(460, 10)
到(470, 20)
到通告程序的线。通知程序的高度为屏幕的3%,而对于我的3%的屏幕,它的高度远远小于470,因此绘画将被丢弃。
就您的情况而言,我知道您想绘制一个10x10的十字,然后绘制该十字,然后将其移动到左上角。
import sys
from PyQt4 import QtCore, QtGui
class CloseCrosss(QtGui.QFrame):
clicked = QtCore.pyqtSignal()
def __init__(self, side=10, parent=None):
QtGui.QFrame.__init__(self, parent)
self._side = side
self.setFixedSize(side, side)
if parent:
parent.installEventFilter(self)
def mouseReleaseEvent(self, event):
self.clicked.emit()
QtGui.QFrame.mouseReleaseEvent(self, event)
def paintEvent(self, e):
qp = QtGui.QPainter(self)
qp.setPen(QtCore.Qt.red)
qp.drawLine(0, 0, self._side, self._side)
qp.drawLine(self._side, 0, 0, self._side)
def eventFilter(self, watched, event):
if watched == self.parentWidget() and self.parentWidget():
if event.type() == QtCore.QEvent.Resize:
self.adjustSize()
return QtGui.QFrame.eventFilter(self, watched, event)
def adjustSize(self):
# move widget to topright
self.move(self.parentWidget().width() -self.width()-5, 5)
class Notifier(QtGui.QWidget):
def __init__(self, message=None):
QtGui.QWidget.__init__(self, flags=QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint)
_desktop = QtGui.QApplication.desktop()
size = _desktop.screenGeometry().size()
self.resize(size.width() * 25 / 100, size.height() * 3 / 100)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.main_layout = QtGui.QVBoxLayout(self)
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)
cross = CloseCrosss(parent=self)
cross.clicked.connect(self.close)
def paintEvent(self, event):
qp = QtGui.QPainter(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(QtCore.QRect(QtCore.QPoint(), self.size()), 5, 5)
if __name__ == "__main__":
QApp = QtGui.QApplication(sys.argv)
window = Notifier("Hello World")
window.show()
sys.exit(QApp.exec_())
关于python - QFrame不以QWidget为父级吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52142950/