-1

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
    Please provide [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so we can run and test it. How did you add `QGroupBox`? what you say and codes you add didn't help us to understand your bug – Parisa.H.R Feb 02 '22 at 17:42
  • The groupbox is not around the slider in the .ui file... – hyde Feb 02 '22 at 19:03
  • Also, what does *"none of that code works"* mean in this case? Please add all the relevant details by editing the question. – hyde Feb 02 '22 at 19:04
  • @hyde In regards to groupbox not being around the slider. In QT Designer, I drag and drop a QGroupBox and position it around my QSlider. So at least visually it is around the QSlider. I'm not familiar with understanding ui files themselves. I'll google about this though. – Henry Helen Feb 02 '22 at 19:23
  • The Designer shows the widgets in a tree view in upper right corner. You can see the hierarchy there. – hyde Feb 02 '22 at 19:29
  • If slider isn't inside the group box (they just overlap on screen), then presence of the group box shouldn't make any difference... Well, except the group box may cover the slider, of course. – hyde Feb 02 '22 at 19:30
  • You nailed it, @hyde. The order of dragging and dropping widgets matters. If I first drag a groupbox and secondly drag a slider into the groupbox, the hierarchy is different than if I drag a slider and then drag a groupbox over the top of it. Groupbox then slider creates a groupbox around a slider in the .ui file and it is shown in the hierarchy in the upper right too. Thanks for the help. – Henry Helen Feb 02 '22 at 20:09
  • Don't use overlapping widgets, if you can avoid it. Just put the slider in the group box, as it's been designed to be used. And use layouts. If you need overlapping widgets, `QGridLayout` can do that (but not with Designer, you have to do it with code). – hyde Feb 02 '22 at 20:11

1 Answers1

0

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.

user3450148
  • 851
  • 5
  • 13
  • Thanks to Hyde, I did figure out the GroupBox should have been added first and the widgets secondly. I have read through various docs, but being a newbie, I was unaware the order of dragging and dropping mattered. – Henry Helen Feb 03 '22 at 13:56