I am attempting to create a stand-alone widget (call it QSpinSlider) that combines the functionality of a QSpinBox and QSlider. Here is a basic working example of my QSpinSlider class:
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QSpinBox, QSlider
from PyQt6.QtCore import Qt
class QSpinSlider(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QHBoxLayout(self)
self.spin_box = QSpinBox()
self.spin_box.setKeyboardTracking(False)
self.slider = QSlider(Qt.Orientation.Horizontal)
self.layout.addWidget(self.spin_box)
self.layout.addWidget(self.slider)
self.spin_box.valueChanged.connect(self.slider.setValue)
self.slider.valueChanged.connect(self.spin_box.setValue)
And a sample of the usage:
if __name__ == "__main__":
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
app = QApplication(sys.argv)
window = QMainWindow()
spin_slider_widget = QSpinSlider()
window.setCentralWidget(spin_slider_widget)
window.show()
sys.exit(app.exec())
The problem that I have is that by extending QWidget
I am losing all of the functionality of the QSpinBox
and QSlider
. I would still like to be able to use the standard properties, functions, signals, and slots of both widgets.
Here are just a few examples:
QSpinSlider.setValue(12)
- Sets the value of both the slider and the spin box
QSpinSlider.value()
- Returns the value of the widget
QSpinSlider.setOrientation(Qt.Orientation.Horizontal)
- Sets the orientation of the slider (and the layout of QSpinSlider)
QSpinSlider.setKeyboardTracking(False)
- Sets the keyboard tracking property of the spin box
QSpinSlider.setMinimum(3)
- Sets the minimum value of both the slider and the spin box
QSpinSlider.setMinimumSize(QSize(12, 50))
- Sets the minumum size of the QSpinSlider widget
- Etc, Etc…
What makes this even more challenging is that the QSpinBox
and QSlider
classes implement many properties and functions that are the same, and many that are unique to their functionality. I am trying to merge the two lists of properties, fucntions, signals, and slots…
I know that I can recreate all the functions and properties in my custom widget class and pass the changes on to the spin box and slider. But, this doesn’t seem very efficient to me (and it sounds really tedious, error prone, and hard to debug).
I’ve tried directly inheriting QAbstractSlider
instead of QWidget
but couldn’t get the results I was looking for.
Any ideas of how I can access all the individual properties, functions, signals, and slots of the two widgets inside the custom widget?