更改QMessageBox和SaveFileDialog的光标

2024-04-27 18:44:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在PyQt5中,我可以使用以下命令更改对象的光标:

    Object.setCursor(QCursor(Qt.PointingHandCursor))

对于其他按钮,我使用这个类,但它不会更改QmessageBoxQfiledialog中的光标:

^{pr2}$

如何更改QMessageBoxQFileDialog中所有按钮的光标?在

Messagebox方法示例

def onNotConnected(self):
        err = QMessageBox.question(
            self, DONGLE_NOT_CONN, DONGLE_NOT_CONN_MSG, QMessageBox.Ok | QMessageBox.Cancel)
        if err == QMessageBox.Ok:            
            self.updating_thread(self.device_code)
        else:
            self.restart_program()

Tags: 对象命令selfobjectnotokconn按钮
1条回答
网友
1楼 · 发布于 2024-04-27 18:44:37

QMessageBoxQFileDialog具有{}方法,因为它们继承自QWidget。但在您的例子中,问题在于静态方法,因为您不能直接访问对象。在

所以解决方案是利用这些静态方法的一个特殊特性:它们是顶级的,所以我们可以使用QApplication.topLevelWidgets()对其进行过滤,但另一个问题是它们阻塞了,所以没有什么可以同步执行,所以诀窍是使用QTimer。在

from PyQt5 import QtCore, QtGui, QtWidgets

def onTimeout():
    for w in QtWidgets.QApplication.topLevelWidgets():
        if isinstance(w, QtWidgets.QMessageBox):
            for button in w.buttons():
                button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent=None)
        self.show()

        QtCore.QTimer.singleShot(0, onTimeout)
        res = QtWidgets.QMessageBox.question(self, 
            "title", 
            "text",  
            QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

同样,在您的示例中,我们可以使用QMessageBox的父级过滤过滤器,可能的话,QFileDialog就是窗口。在

^{pr2}$

相关问题 更多 >