small GUI creation with pySide

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Lookid
Posts: 44
Joined: Wed Sep 12, 2018 7:39 pm
Location: France

small GUI creation with pySide

Post by Lookid »

Hello,

not sure I am on the right section in the forum

I am lost in GUI creation, my problem is that I don't understand classes and OOP in general I suppose.
I followed the wiki tutorial for GUI creation with PySide, they create the GUI with Qt, and then translate the .ui file to .py, or they don't translate it and load it.

I want this second approach (load directly the .ui), and try a very simple thing: Enter 3 values and make a simple operation by clicking a button. If that works, the core thing is OK and I can make more complicated stuff.

The GUI looks like this:
ui_png.PNG
ui_png.PNG (7.5 KiB) Viewed 1451 times

and my .py file like this:

Code: Select all

from PySide import QtCore, QtGui, QtUiTools

# path to .ui
path_to_ui = r"C:\Users\Lilian\AppData\Roaming\FreeCAD\Macro\testUI.ui"

class MyButtons(QtGui.QDialog):
    def __init__(self, MainWindow):
        super(MyButtons, self).__init__()
        self.window = MainWindow

        # load .ui
        loader = QtUiTools.QUiLoader()
        uifile = QtCore.QFile(path_to_ui)
        uifile.open(QtCore.QFile.ReadOnly)
        self.ui = loader.load(uifile, None)
        uifile.close()

        self.ui.button_main_dimensions.clicked.connect(self.calc_nq()) #connection radioButton

    def calc_nq(self):
    # calculate the specific speed and writes the result in the label
        # get entries
        N = self.ui.doubleSpinBox_N.value()
        Q = self.ui.doubleSpinBox_Q.value()
        H = self.ui.doubleSpinBox_H.value()

        nq = N*Q**0.5/H**0.75
        self.ui.label_value_nq.setText(str(round(nq, 2)))
        
MainWindow = QtGui.QMainWindow()
ui = MyButtons(MainWindow)
MainWindow.show()
I know it is wrong. Since I didn't find a sample code that works, I tried copy pasting things from here and there and ended up overcooking my brain without understanding anything.
I bet the solution is simple though :D
I join the .ui file also if you wish to try that.

Lookid_.

OS: Windows 10 (10.0)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.21125 (Git)
Build type: Release
Branch: master
Hash: 40600a55c2fe71ff589be677f6e427ccc937d003
Python version: 3.8.2
Qt version: 5.12.5
Coin version: 4.0.0
OCC version: 7.4.0
Locale: English/United Kingdom (en_GB)
Attachments
testUI.ui
(4.76 KiB) Downloaded 66 times
User avatar
Markymark
Posts: 228
Joined: Sun Nov 03, 2019 4:54 pm

Re: small GUI creation with pySide

Post by Markymark »

Which tutorial did you try? What is the problem?

https://wiki.freecadweb.org/Manual:Crea ... face_tools

If you follow the above link you will find a working solution. Tried just a few days ago.

Rgrds, m.
Lookid
Posts: 44
Joined: Wed Sep 12, 2018 7:39 pm
Location: France

Re: small GUI creation with pySide

Post by Lookid »

Thank you! I was rushing everything while not reading anything properly.

For those interested, I joined the .py and .ui files, and it looks like this:
Capture.PNG
Capture.PNG (6 KiB) Viewed 1433 times
pysideTest3.py

Code: Select all

import FreeCAD,FreeCADGui,Part
from PySide import QtCore, QtGui, QtUiTools

# CHANGE THE LINE BELOW
path_to_ui = r"C:\Users\Lilian\AppData\Roaming\FreeCAD\Macro\testUI.ui"
 
class MySmallGUI:
    def __init__(self):
        # this will create a Qt widget from our ui file
        self.form = FreeCADGui.PySideUic.loadUi(path_to_ui)

        # define what button does what
        self.form.button_main_dimensions.clicked.connect(lambda: self.calc_nq())


    def calc_nq(self):
    # calculate the specific speed and writes the result in the label
        # get entries
        N = self.form.doubleSpinBox_N.value()
        Q = self.form.doubleSpinBox_Q.value()
        H = self.form.doubleSpinBox_H.value()

        nq = N*Q**0.5/H**0.75
        self.form.label_value_nq.setText(str(round(nq, 2)))

MySmallGUI().form.show()

testUI.ui

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>264</width>
    <height>171</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <family>Times New Roman</family>
    <pointsize>10</pointsize>
    <italic>true</italic>
   </font>
  </property>
  <property name="windowTitle">
   <string>A cool GUI</string>
  </property>
  <layout class="QFormLayout" name="formLayout">
   <item row="0" column="0" colspan="2">
    <widget class="QLabel" name="label_setMainParameters">
     <property name="font">
      <font>
       <weight>75</weight>
       <bold>true</bold>
      </font>
     </property>
     <property name="text">
      <string>Set Main Parameters</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item row="1" column="0">
    <widget class="QLabel" name="label_N">
     <property name="text">
      <string>N</string>
     </property>
     <property name="textFormat">
      <enum>Qt::AutoText</enum>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item row="1" column="1">
    <widget class="QDoubleSpinBox" name="doubleSpinBox_N">
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
     <property name="prefix">
      <string/>
     </property>
     <property name="suffix">
      <string> RPM</string>
     </property>
     <property name="decimals">
      <number>0</number>
     </property>
     <property name="maximum">
      <double>1000000.000000000000000</double>
     </property>
     <property name="singleStep">
      <double>100.000000000000000</double>
     </property>
     <property name="value">
      <double>2900.000000000000000</double>
     </property>
    </widget>
   </item>
   <item row="2" column="0">
    <widget class="QLabel" name="label_Q">
     <property name="text">
      <string>Q</string>
     </property>
     <property name="textFormat">
      <enum>Qt::AutoText</enum>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item row="2" column="1">
    <widget class="QDoubleSpinBox" name="doubleSpinBox_Q">
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
     <property name="suffix">
      <string> m3/s</string>
     </property>
     <property name="decimals">
      <number>4</number>
     </property>
     <property name="maximum">
      <double>1000000.000000000000000</double>
     </property>
     <property name="singleStep">
      <double>0.010000000000000</double>
     </property>
     <property name="value">
      <double>0.030000000000000</double>
     </property>
    </widget>
   </item>
   <item row="3" column="0">
    <widget class="QLabel" name="label_H">
     <property name="text">
      <string>H</string>
     </property>
     <property name="textFormat">
      <enum>Qt::AutoText</enum>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item row="3" column="1">
    <widget class="QDoubleSpinBox" name="doubleSpinBox_H">
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
     <property name="suffix">
      <string> m</string>
     </property>
     <property name="decimals">
      <number>4</number>
     </property>
     <property name="maximum">
      <double>1000000.000000000000000</double>
     </property>
     <property name="singleStep">
      <double>1.000000000000000</double>
     </property>
     <property name="value">
      <double>30.000000000000000</double>
     </property>
    </widget>
   </item>
   <item row="4" column="0">
    <widget class="QLabel" name="label_nq">
     <property name="text">
      <string>nq</string>
     </property>
    </widget>
   </item>
   <item row="4" column="1">
    <widget class="QLabel" name="label_value_nq">
     <property name="text">
      <string>-</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
   <item row="5" column="1">
    <widget class="QPushButton" name="button_main_dimensions">
     <property name="font">
      <font>
       <weight>75</weight>
       <bold>true</bold>
      </font>
     </property>
     <property name="text">
      <string>Calculate nq</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>
Attachments
testUI.ui
(4.76 KiB) Downloaded 52 times
pysideTest3.py
(849 Bytes) Downloaded 51 times
Last edited by Lookid on Tue May 26, 2020 10:15 am, edited 1 time in total.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: small GUI creation with pySide

Post by vocx »

Lookid wrote: Mon May 25, 2020 2:57 pm For those interested, I joined the .py and .ui files, and it looks like this:
You should just paste the code inside code tags. It's much simpler to visualize it, instead of having to download the files.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Lookid
Posts: 44
Joined: Wed Sep 12, 2018 7:39 pm
Location: France

Re: small GUI creation with pySide

Post by Lookid »

vocx wrote: Mon May 25, 2020 4:49 pm
Lookid wrote: Mon May 25, 2020 2:57 pm For those interested, I joined the .py and .ui files, and it looks like this:
You should just paste the code inside code tags. It's much simpler to visualize it, instead of having to download the files.
True, post is edited.
Post Reply