QT - Accessing QSlider within a QGroupBox

201 views Asked by At

I am a newbie to QT. I'm using the Qt Designer to create a ui file then writing Python code to make things happen. I have a QSlider, and in the python code I can act when the value of the slider changes with this:

widget = QWidget()
widget.mySlider.valueChanged[int].connect(myChangeMethod)

or I use this to access the value of the QSlider

widget.mySlider.value()

If I add a QGroupBox around the QSlider, then I cannot slide the QSlider in the GUI. When surrounded by a QGroupBox, how can I do get QSliders to work meaning I can move the slider and use .valueChanged or .value() in my code?

Thanks in advance!

UPDATE: Here is the .ui file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QSlider" name="mySlider">
   <property name="geometry">
    <rect>
     <x>90</x>
     <y>100</y>
     <width>221</width>
     <height>16</height>
    </rect>
   </property>
   <property name="toolTip">
    <string>No Comment</string>
   </property>
   <property name="minimum">
    <number>0</number>
   </property>
   <property name="maximum">
    <number>10</number>
   </property>
   <property name="singleStep">
    <number>1</number>
   </property>
   <property name="orientation">
    <enum>Qt::Horizontal</enum>
   </property>
   <property name="tickPosition">
    <enum>QSlider::TicksBelow</enum>
   </property>
   <property name="tickInterval">
    <number>1</number>
   </property>
  </widget>
  <widget class="QGroupBox" name="groupBox">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>20</y>
     <width>381</width>
     <height>171</height>
    </rect>
   </property>
   <property name="title">
    <string>MyGroup</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
1

There are 1 answers

1
user3450148 On

You know, the reason so many are asking for a complete example is that driving blind isn't fun.

I don't "do python" as I prefer static typed languages.

A quick search with DuckDuckGo yields the following:

https://pythonbasics.org/pyqt-groupbox/

https://realpython.com/qt-designer-python/#building-main-windows-with-qt-designer-and-python

Your UI file looks horribly busted. What version of Qt are you using???

Everything should have been added to the groupbox after you added the groupbox. What is showing in your UI file is the GroupBox covers everything and your non-statically typed language cannot get to the stuff that is now "logically hidden" under the GroupBox. Doesn't matter if you can "see" it because it isn't "visible" according to the layers and engine.

the path should be

widget.groupBox.mySlider

But according to your UI file groupBox isn't parenting anything yet it is most of your widget and added last so "should" be "on top" of everything else.

Read through the tutorials please.