📜  PyQt5 – QDial(1)

📅  最后修改于: 2023-12-03 14:45:50.555000             🧑  作者: Mango

PyQt5 - QDial

Introduction

QDial is a PyQt5 widget that provides a rotating dial interface for the user to select a numeric value within a given range. The selected value can be either integer or floating-point.

Features
  • Allows selection of numeric values within a given range.
  • Supports integer and floating-point number formats.
  • Can be customized using various style options.
  • Emit signals to notify the value changes.
Usage
Basic Usage

To use the QDial widget, you need to create an instance of it and set its properties.

from PyQt5.QtWidgets import QApplication, QWidget, QDial, QVBoxLayout

app = QApplication([])

widget = QWidget()

dial = QDial()
dial.setRange(0, 100)
dial.setValue(50)

layout = QVBoxLayout(widget)
layout.addWidget(dial)

widget.show()

app.exec_()
Signals

The QDial widget emits two types of signals:

  • valueChanged: emitted whenever the value of the dial changes. The new value is passed as an argument to the connected slot.
  • sliderReleased: emitted whenever the user releases the mouse button after dragging the dial. This signal is useful to trigger actions only when the user finishes selecting a value.
from PyQt5.QtWidgets import QApplication, QWidget, QDial, QVBoxLayout, QLabel

app = QApplication([])

widget = QWidget()

dial = QDial()
label = QLabel()

def update_label(value):
    label.setText(str(value))

dial.valueChanged.connect(update_label)

layout = QVBoxLayout(widget)
layout.addWidget(dial)
layout.addWidget(label)

widget.show()

app.exec_()
Customizing Appearance

The QDial widget can be customized using various properties and styles. For example, you can change the color and shape of the dial:

from PyQt5.QtWidgets import QApplication, QWidget, QDial, QVBoxLayout
from PyQt5.QtGui import QColor, QPalette

app = QApplication([])

widget = QWidget()

dial = QDial()
dial.setRange(0, 100)
dial.setValue(50)
dial.setNotchesVisible(True)

palette = QPalette()
palette.setColor(QPalette.ButtonText, QColor(255, 0, 0))
dial.setPalette(palette)

layout = QVBoxLayout(widget)
layout.addWidget(dial)

widget.show()

app.exec_()
Additional Options

Some additional options that can be useful:

  • setWrapping: allows the dial to wrap around when reaching the maximum or minimum values.
  • setNotchesVisible: displays small notches around the dial to indicate the possible values.
  • setSingleStep: defines how much the value changes when the user moves the dial a little.
  • setPageStep: defines how much the value changes when the user moves the dial a lot.
Conclusion

QDial is a simple but powerful PyQt5 widget that can be used to add a nice and intuitive interface to select numeric values. With its numerous customization options, it's easy to adapt to your application's design and functionality.