更改QMessageBox和SaveFileDialog的光标

时间:2018-07-27 12:09:21

标签: python python-3.x pyqt pyqt5 qmessagebox

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

    Object.setCursor(QCursor(Qt.PointingHandCursor))

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

class QPushButton(QPushButton):
    def __init__(self, parent=None):
        super(QPushButton, self).__init__(parent)
        self.setCursor(QCursor(Qt.PointingHandCursor))

如何更改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()

1 个答案:

答案 0 :(得分:1)

QMessageBoxQFileDialog具有setCursor()方法,因为它们继承自QWidget。但是您遇到的问题是静态方法,因为您不能直接访问该对象。

因此解决方案是利用这些静态方法的特定特征:它们是topLevels,因此我们可以使用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可能是窗口。

from PyQt5 import QtCore, QtGui, QtWidgets


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

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

        QtCore.QTimer.singleShot(0, self.onTimeout)
        fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, 
            "Save File",
            QtCore.QDir.homePath(),
            "Images (*.png *.xpm *.jpg)",
            "",
            QtWidgets.QFileDialog.DontUseNativeDialog)

    def onTimeout(self):
        for w in QtWidgets.QApplication.topLevelWidgets():
            if isinstance(w, QtWidgets.QMessageBox) and w.parent() == self:
                for button in w.buttons():
                    button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
            elif isinstance(w, QtWidgets.QFileDialog) and w.parent() == self:
                for button in w.findChildren(QtWidgets.QPushButton):
                    button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))



if __name__ == '__main__':
    import sys

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