QT-Dialog convert to python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

QT-Dialog convert to python

Post by Chri1 »

Hello
I made a dialog in QT-Creator and tried to convert it for Win7 like in www.freecadweb.org/wiki/Dialog_creation :
"@C:\Python27\python" "C:\Python27\Lib\site-packages\PySide\scripts\uic.py" %1.ui > %1.py"

But on the whole PC I cannot find uic.py and also no folder Python27
(QT is installed and of course FreeCad)

Where can I get it / What is to do?

Thanks for your help
Chr1
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: QT-Dialog convert to python

Post by vocx »

Chri1 wrote: Sun Apr 28, 2019 4:25 pm ...
Where can I get it / What is to do?
...
How experienced are you at using Python, Qt, compiling from source, and software development in general?

That page is pretty old, so the information is outdated. The last major change, that was not about typos and translations, occurred in 2016. Since then FreeCAD has transitioned to using Qt5 instead of Qt4, and also prefers Python 3 rather than Python 2.

The Dialog creation topic is intended for power users or developers, so more experience with programming is assumed by the reader. You can probably find yourself what needs to be changed to get the dialog working, but as I said, you need to have more experience with Qt, and going online to read the reference documentation and find answers.

Unfortunately, the wiki documentation intended for developers is quite outdated. Until it is brought up to date, you have to fend for yourself. I suggest you take a look at the actual source code of FreeCAD (https://github.com/FreeCAD/FreeCAD) and try to see how the workbenches do what they do. You should visit macros recipes and external workbenches, as they may show you how to produce the dialogs that you want. If you use a macro, look at its source code, copy it, and base your own code on that.
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.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: QT-Dialog convert to python

Post by mario52 »

hi

you must install Python27 in "C:\Python27" and pyQt for use this .bat (now PySide is used)

i have install Python 3.6 and PyQt5 for the actual FC and always error error with pyuic.py

i tray with pyuic5.exe (C:\Python36\Scripts) i use

Code: Select all

@"C:/Python36/Scripts/pyuic5.exe" %1.ui > %1.py
and work fine but more light just the class Ui_MainWindow(object) without "démarrage"

(i upgrade the wiki later)

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

Re: QT-Dialog convert to python

Post by Chri1 »

Hi

Hi

Tried many versions of phython, but finaly it worked like this – thanks Mario:

I installed Python:
https://www.python.org/ftp/python/3.6.8 ... -amd64.exe
and from cmd:
pip3 install PyQt5-sip
pip3 install PyQt5
then Cpyuic5.exe bkneu.ui bkneu.py worked and I mad a .bat:
@" C:\Users\...\Scripts\pyuic5.exe" %1.ui > %1.py

But now I have the problem to run the dialog:

Code: Select all

from PySide import QtGui
import bkneu
d = QtGui.QWidget()
d.ui = bkneu.Ui_Dialog()
d.ui.setupUi(d)  -> Makes Error
d.show()
Error:
QLabel(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'PySide2.QtWidgets.QWidget'
QLabel(str, parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'PySide2.QtWidgets.QWidget'

bkneu.py:

Code: Select all

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 262)
        Dialog.setStyleSheet("background-color: rgb(206, 220, 233);")
        self.label = QtWidgets.QLabel(Dialog)   -> Here seems to be the error
What is now wrong?

Chri1
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: QT-Dialog convert to python

Post by mario52 »

hi

and this (single)

Code: Select all

# -*- coding: utf-8 -*-
#import bkneu
#from PyQt5 import QtCore, QtGui, QtWidgets

# FreeCAD use PySide and not PyQt5
import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        self.window = MainWindow ## adding

        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 262)
        Dialog.setStyleSheet("background-color: rgb(206, 220, 233);")
#        self.label = QtWidgets.QLabel(Dialog)#  -> Here seems to be the error
        self.label = QtGui.QLabel(Dialog)     #
        self.label.setText("tyty")
        self.label.setGeometry(QtCore.QRect(200, 100, 50, 20))

        ## adding begin
        self.PB_1_Quit = QtGui.QPushButton(Dialog)
        self.PB_1_Quit.setGeometry(QtCore.QRect(150, 200, 91, 28))
        self.PB_1_Quit.setText("Quit")
        self.PB_1_Quit.clicked.connect(self.on_PB_1_Quit) # connection to "on_PB_1_Quit"

    def on_PB_1_Quit(self):                               # "on_PB_1_Quit"
        App.Console.PrintMessage(str("FreeCAD the Best")+"\n")
        self.window.hide()
        ## adding end



#d = QtGui.QtWidgets()
#d.ui = bkneu.Ui_Dialog()
#d.ui.setupUi(d)# Makes Error
#d.show()
MainWindow = QtGui.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

Re: QT-Dialog convert to python

Post by Chri1 »

Hi
It works!

Code: Select all

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'bkneu.ui'
#
# Created by: PyQt5 UI code generator 5.12.1
#
# WARNING! All changes made in this file will be lost!


import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *

from PySide2.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 262)
        QtWidgets = PySide.QtGui
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(30, 10, 151, 16))
        font = QtGui.QFont()
        font.setPointSize(12)
        ….
The important Thing was: QtWidgets = PySide.QtGui

And start from FreeCad-PythonConsole:

Code: Select all

import PySide
from PySide import QtGui ,QtCore
from PySide.QtGui import *
from PySide.QtCore import *
import bkneu
MainWindow = QtGui.QMainWindow() 
ui = bkneu.Ui_Dialog() 
ui.setupUi(MainWindow) 
MainWindow.show()
Probably not all lines are really necessary …

Chri1
Chri1
Posts: 86
Joined: Wed Oct 17, 2018 9:00 am

Re: QT-Dialog convert to python

Post by Chri1 »

Additional.
After some try and error I added this Code:

Code: Select all

class doit():
   def __init__(self):
       self.d = QQtGui.QMainWindow() 
       self.d.ui = bkneu.Ui_Dialog()
       self.d.ui.setupUi(self.d)
       self.d.show()
And now it works also with: bkneu.doit()

Chr1
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: QT-Dialog convert to python

Post by Jee-Bee »

Code: Select all

class doit():
   def __init__(self):
       self.d = QtGui.QMainWindow() 
       self.d.ui = bkneu.Ui_Dialog()
       self.d.ui.setupUi(self.d)
       self.d.show()
you have 2 Q's in Qt
Post Reply