[英]PyQt5 notification from QWidget
我希望通过PC上的手机发送自己的通知,例如notify-senf。 我有弹出窗口小部件,但是我不能删除它。 当我删除它时,仍然有空的地方。 我怎样才能做到这一点? 此代码是示例。
class Notification(QWidget):
signNotifyClose = QtCore.pyqtSignal(str)
def __init__(self, parent = None):
time = datetime.now()
currentTime = str(time.hour) + ":" + str(time.minute) + "_"
self.LOG_TAG = currentTime + self.__class__.__name__ + ": "
super(QWidget, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #убирает заголовок, поверх всех окон (| QtCore.Qt.WindowStaysOnTopHint)
resolution = QDesktopWidget().screenGeometry(-1)
screenWidth = resolution.width()
screenHeight = resolution.height()
print(self.LOG_TAG + "width: " + str(resolution.width()) + " height: " + str(resolution.height()))
self.count = 0 # Счетчик уведомлений
self.timer = 3
self.vboxMainLayout = QVBoxLayout() # layout contain notifications
self.move(screenWidth, 0)
self.setLayout(self.vboxMainLayout)
def setNotify(self, title, notify):
count = self.count
title = QLabel()
title.setStyleSheet("border: 1px solid #000")
title.setText(title)
title.setStyleSheet("font-family: 'Roboto', sans-serif; font-size: 14px; font-weight: bold; padding: 0;")
text = QLabel()
text.setText(notify)
text.setStyleSheet("font-family: 'Roboto', sans-serif; font-size: 12px; font-weight: normal; padding: 0;")
gridNotify = QGridLayout()
gridNotify.addWidget(title, 0, 0)
gridNotify.addWidget(text, 1, 0)
buttonClose = QPushButton()
buttonClose.clicked.connect(self.deleteWidgets)
buttonClose.setIcon(QIcon("res/close1.png"))
buttonClose.setFlat(False)
buttonClose.setMaximumWidth(14)
buttonClose.setMaximumHeight(14)
gridClose = QGridLayout()
gridClose.addWidget(buttonClose, 0, 0)
gridLayoutMain = QGridLayout()
gridLayoutMain.setColumnStretch(0,1)
gridLayoutMain.setColumnStretch(0,2)
gridLayoutMain.setColumnStretch(0,3)
gridLayoutMain.addLayout(gridClose, 0, 4)
gridLayoutMain.addLayout(gridNotify, 0, 0)
self.count += 1
self.vboxMainLayout.addLayout(gridLayoutMain)
self.show()
threading.Timer(2, self.delete, args=(gridLayoutMain,)).start()
def delete(self, layout):
for i in reversed(range(layout.count())):
item = layout.takeAt(i)
widget = item.widget()
if widget is not None:
# widget.deleteLater()
elif item.layout() is not None:
print("")
self.delete(item.layout())
通知
删除后的位置
为了简化任务,最好为消息创建一个小部件,如下所示:
class Message(QWidget):
def __init__(self, title, message, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QGridLayout())
self.titleLabel = QLabel(title, self)
self.titleLabel.setStyleSheet(
"font-family: 'Roboto', sans-serif; font-size: 14px; font-weight: bold; padding: 0;")
self.messageLabel = QLabel(message, self)
self.messageLabel.setStyleSheet(
"font-family: 'Roboto', sans-serif; font-size: 12px; font-weight: normal; padding: 0;")
self.buttonClose = QPushButton(self)
self.buttonClose.setIcon(QIcon("res/close1.png"))
self.buttonClose.setFixedSize(14, 14)
self.layout().addWidget(self.titleLabel, 0, 0)
self.layout().addWidget(self.messageLabel, 1, 0)
self.layout().addWidget(self.buttonClose, 0, 1, 2, 1)
当你要删除的消息,首先必须从与布局中删除它removeWidget()
布局的功能,那么你必须使用deleteLater()
删除本身及其子部件。 当我们发送信号时,我们可以获取通过sender()
发出信号的对象,在我们的例子中将是按钮,并通过该对象获得作为消息小部件的父对象。 然后,要更新布局的正确大小,请使用adjustSize()
。 即使删除整个小部件也需要最小值,因此,如果没有消息,建议将其关闭。
class Notification(QWidget):
signNotifyClose = pyqtSignal(str)
def __init__(self, parent = None):
time = datetime.now()
currentTime = str(time.hour) + ":" + str(time.minute) + "_"
self.LOG_TAG = currentTime + self.__class__.__name__ + ": "
super(QWidget, self).__init__(parent)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
resolution = QDesktopWidget().screenGeometry(-1)
screenWidth = resolution.width()
screenHeight = resolution.height()
print(self.LOG_TAG + "width: " + str(resolution.width()) + " height: " + str(resolution.height()))
self.nMessages = 0
self.mainLayout = QVBoxLayout(self)
self.move(screenWidth, 0)
def setNotify(self, title, message):
m = Message(title, message, self)
self.mainLayout.addWidget(m)
m.buttonClose.clicked.connect(self.onClicked)
self.nMessages += 1
self.show()
def onClicked(self):
self.mainLayout.removeWidget(self.sender().parent())
self.sender().parent().deleteLater()
self.nMessages -= 1
self.adjustSize()
if self.nMessages == 0:
self.close()
这是如何与以下代码一起使用的示例:
class Example(QWidget):
counter = 0
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QVBoxLayout())
btn = QPushButton("Send Notify", self)
self.layout().addWidget(btn)
self.notification = Notification()
btn.clicked.connect(self.notify)
def notify(self):
self.counter += 1
self.notification.setNotify("Title{}".format(self.counter),
"message{}".format(self.counter))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.