简体   繁体   English

禁用QDial鼠标单击并更换针头

[英]Disable QDial mouse clicks and change the needle

I have a QDial widget and I'm wondering if there is a way to disable mouse clicks on the dial. 我有一个QDial小部件,我想知道是否有一种方法可以禁用在表盘上的鼠标单击。 I only want to be able to set the value of the QDial by using setValue() . 我只希望能够通过使用setValue()来设置QDial的值。 When a user clicks on the dial, I want nothing to happen instead of moving to the spot where the user clicked. 当用户单击拨号盘时,我希望什么都不会发生,而是希望移至用户单击的位置。 I have looked into mousePressEvent handler, but it still moves the needle to a new location. 我已经研究了mousePressEvent处理程序,但是它仍将指针移至新位置。 How can I disable mouse clicks from affecting the value of the QDial? 如何禁止鼠标单击影响QDial的值? Also: is there a way to change the current value indicator (circle object) into a needle through stylesheets? 另外:有没有办法通过样式表将当前值指示器(圆形对象)更改为针形?

from PyQt4 import QtGui, QtCore

class DisabledDial(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.dial = QtGui.QDial()
        self.dial.setMaximum(360)
        self.dial.setNotchesVisible(True)
        self.dial.setWrapping(True)

        self.layout = QtGui.QVBoxLayout()
        self.layout.addWidget(self.dial)
        self.setLayout(self.layout)

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.tick_update)
        self.timer.start(1000)

    def tick_update(self):
        self.dial.setValue(self.dial.value() + 10)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    dial = DisabledDial()
    dial.show()
    sys.exit(app.exec_())

You have to disable the mousePressEvent() , mouseReleaseEvent() and mouseMoveEvent() methods: 您必须禁用mousePressEvent()mouseReleaseEvent()mouseMoveEvent()方法:

from PyQt4 import QtCore, QtGui


class Dial(QtGui.QDial):
    def mousePressEvent(self, event):
        pass

    def mouseReleaseEvent(self, event):
        pass

    def mouseMoveEvent(self, event):
        pass


class DisabledDial(QtGui.QWidget):
    def __init__(self, parent=None):
        super(DisabledDial, self).__init__(parent)

        self.dial = Dial(
            maximum=360,
            notchesVisible=True,
            wrapping=True,
        )
        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(self.dial)

        timer = QtCore.QTimer(
            self,
            timeout=self.tick_update,
            interval=1000,
        )
        timer.start()

    @QtCore.pyqtSlot()
    def tick_update(self):
        self.dial.setValue(self.dial.value() + 10)


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    dial = DisabledDial()
    dial.show()
    sys.exit(app.exec_())

By default, users can control a QDial by using mouse clicks, the mouse wheel, or keyboard shortcuts. 默认情况下,用户可以使用鼠标单击,鼠标滚轮或键盘快捷键来控制QDial To prevent all of these, a simple solution is to use an event-filter , like this: 为了防止所有这些,一个简单的解决方案是使用一个事件过滤器 ,如下所示:

class DisabledDial(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.dial = QtGui.QDial()
        self.dial.installEventFilter(self)
        ...

    def eventFilter(self, source, event):
        if (source is self.dial and isinstance(event, (
            QtGui.QMouseEvent, QtGui.QWheelEvent, QtGui.QKeyEvent))):
            return True
        return QtGui.QWidget.eventFilter(self, source, event)

As for your other question: at present, there is very little that can be done to change the appearance of a QDial using stylesheets. 至于您的另一个问题:目前,使用样式表更改QDial外观的工作QDial By default, the shape of the pointer is determined by the widget-style : 默认情况下,指针的形状由widget-style决定

在此处输入图片说明在此处输入图片说明

On my system, the default appearance in PyQt4 looks like the one on the left. 在我的系统上,PyQt4的默认外观类似于左侧的外观。 But if I use the preview feature in Qt Designer to switch to the "plastique" style, it looks like the one on the right. 但是,如果我使用Qt Designer中的预览功能切换到“ plastique”样式,则看起来就像右边的样式。 However, there is no way to change the style for individual widgets - it's a case of all or none. 但是,无法更改单个小部件的样式-是全部或全部都不存在。 Therefore, the only real solution for altering an individual dial would be to reimplement its paintEvent and use custom drawing to get a different pointer shape. 因此,更改单个刻度盘的唯一实际解决方案是重新实现其paintEvent并使用自定义绘图来获得不同的指针形状。 But that is not a straightforward thing to do, so you will need to ask a new question if you want help with that. 但这并不是一件容易的事,因此如果您需要帮助,您将需要提出一个新问题。

Easier might be to use setEnabled(False) , which should prevent user input. 可能更容易使用setEnabled(False) ,这应该阻止用户输入。 However, this also visually disables the dial, which might not look nice. 但是,这也从视觉上禁用了转盘,看起来可能不太好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM