Some findings about Qt.

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Some findings about Qt.

Post by onekk »

Hello, I'm strugglig in obtaining a rather complex interface, and I'm driving mad as come buttons won't work as expected.

Some background:

- Dialog is done "by hand"
- It uses a QtGui.QTabWidget containin some Tabs under GridLayouts
- Button are created in different Tabs using QPushButton

Two major problem:

- when the interface is created the command associated with the button using: .clicked.connect() is executed.
- some button was not working.

Solution found:

- using a "boolean flag" set to False and checked in the method invoked by .clicked.connect() wil prevent the trigger of the action during the Initialization of the interface, once init is finished, set it to True to make the execution happen.
- using a lamba: <method invocation> has solved the problem of not working buttons.


Some developers has noted these behaviour or I have something wrong, here some info about my FC version:

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.28765 (Git)
Build type: Release
Branch: (HEAD detached at 795f86c)
Hash: 795f86c18c22260cfc3627bdd5b71504b9dca4ef
Python 3.9.12, Qt 5.12.9, Coin 4.0.0, OCC 7.5.3
Locale: Italian/Italy (it_IT)
Installed mods: 
  * fcgear 1.0.0
  * freecad-antenna
  * Curves 0.4.1
Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Some findings about Qt.

Post by openBrain »

A link to some code?
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Some findings about Qt.

Post by onekk »

I will try to put together an example, it is a code for a customer, so I can't spread it "as is".

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Some findings about Qt.

Post by onekk »

I have found during my search about Qt this comment on some code around, sadly I have copied the code, but no the url of the site.

Code: Select all

class USlideIDsListDialog(QDialog):
  # If your not going to use parent do not include but if you include then use it
    def __init__(self, parent):
      # One should not use super( ) in Python as it introduces 4 known issues that
      # must be handled properly. Further there were still actual bugs within the
      # usage of super( ) when used in Python. Yes while super( ) works fine  within
      # C++ it does not work as seamlessly within Python due to the major 
      # differences between these 2 languages. Next the reason it was created was 
      # to handle a rather rare issue and unless you are doing some complicated 
      # inheritance you will most likely never run into this extremely rare issue
      # However the 4 major issues that get included by using super( ) you are much 
      # more likely to occur than that rare issue its meant to solve. Of course using
      # the basic explicit method, as follows, does not cause these issues and is as
      # just as simple as using `super( )` further you do not actually gain anything
      # useful by using `super( )` in Python that could not be done in a much safer
      # manner.
        QDialog.__init__(self)


Some expert is knowing more about this matter.

Found this after some searching.

https://forum.qt.io/topic/112731/super-in-python/2

It appears that the question has raised some debate, and the user was banned, but it seems that his argumentation are not "superficial" so I'm asking here if someone more skilled could make some light on it.


Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Some findings about Qt.

Post by adrianinsaval »

so were you usign super in your code and that caused an issue or what are we talking about?
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Some findings about Qt.

Post by onekk »

adrianinsaval wrote: Thu May 05, 2022 9:11 pm so were you usign super in your code and that caused an issue or what are we talking about?
Not exactly, but something is not working as expected, probably is my style of coding that has some flaws, due mainly to the absence of decent examples around, (at least the official Qt documentation is not very Python Friendly).

The code from which I've copied the comment above was illustrating a way to create a modal sub dialog on which buttons will launch code from the "invoking class" and this code is working perfectly, and will do the job I need.

EDIT here the code:

Code: Select all

from PySide2.QtCore    import Slot
from PySide2.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QVBoxLayout
from PySide2.QtWidgets import QStyleFactory, QDialog, QWidget, QPushButton


class DiagDsply(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)
        self.Parent = parent
        
        self.btnDone = QPushButton('Done')
        self.btnDone.clicked.connect(self.DoDone)

        self.btnCancel = QPushButton('Cancel')
        self.btnCancel.clicked.connect(self.DoCancel)
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.btnDone)
        HBox.addWidget(self.btnCancel)
        HBox.addStretch(1)

        self.setLayout(HBox)

    @Slot()
    def DoDone(self):
        self.Parent.DoDone()
    
    @Slot()
    def DoCancel(self):
        self.Parent.DoCancel()

class USlideIDsListDialog(QDialog):
  # If your not going to use parent do not include but if you include then use it
    def __init__(self, parent):
      # One should not use super( ) in Python as it introduces 4 known issues that
      # must be handled properly. Further there were still actual bugs within the
      # usage of super( ) when used in Python. Yes while super( ) works fine  within
      # C++ it does not work as seamlessly within Python due to the major 
      # differences between these 2 languages. Next the reason it was created was 
      # to handle a rather rare issue and unless you are doing some complicated 
      # inheritance you will most likely never run into this extremely rare issue
      # However the 4 major issues that get included by using super( ) you are much 
      # more likely to occur than that rare issue its meant to solve. Of course using
      # the basic explicit method, as follows, does not cause these issues and is as
      # just as simple as using `super( )` further you do not actually gain anything
      # useful by using `super( )` in Python that could not be done in a much safer
      # manner.
        QDialog.__init__(self)

        self.Parent = parent
        Left=700; Topp=300; Width=300; Hight=100
        self.setGeometry(Left, Topp, Width, Hight)
        self.setWindowTitle('Dialog')

        self.DsplydDiag = DiagDsply(self)

        HBox = QHBoxLayout()
        HBox.addWidget(self.DsplydDiag)
        
        self.setLayout(HBox)

    def DoDone(self):
      # Code to save some stuff to the settings goes here...
        self.Parent.Results('Accept')
        self.close()

    def DoCancel(self):
      # Do nothing, no changes to settings
        self.Parent.Results('Reject')
        self.close()


class CenterPanel(QWidget):
    def __init__(self, parent):
        QWidget.__init__(self)
        self.Parent = parent

        self.btnDsplyr = QPushButton('Display It')
        self.btnDsplyr.clicked.connect(self.DoSlideListDialog)
 
        HBox = QHBoxLayout()
        HBox.addStretch(1)
        HBox.addWidget(self.btnDsplyr)
        HBox.addStretch(1)

        VBox = QVBoxLayout()
        VBox.addStretch(1)
        VBox.addLayout(HBox)
        VBox.addStretch(1)

        self.setLayout(VBox)

        self.SlideListDialog = USlideIDsListDialog(self)
        
    @Slot()
    def DoSlideListDialog(self):
        TxtMsg = 'Get Results'
        self.Parent.SetStatus(TxtMsg)

        self.SlideListDialog.show()
        self.SlideListDialog.exec_()

    def Results(self, Reslt):
        TxtMsg = 'Dialog Results :' + str(Reslt)
        self.Parent.SetStatus(TxtMsg)


class MainDisply(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        Top=300; Left=700; Width=300; Hight=100
        self.setGeometry(Left, Top, Width, Hight)
        self.setWindowTitle('Main Display Area')
        self.setStyle(QStyleFactory.create('Cleanlooks'))

        self.CenterPane = CenterPanel(self)
        self.setCentralWidget(self.CenterPane)

        self.StatBar = self.statusBar()
        self.SetStatus('Ready')

    def SetStatus(self, StatusMsg):
        self.StatBar.showMessage(StatusMsg)


if __name__ == "__main__":
    MainEventHandler = QApplication([])

    MainApplication = MainDisply()
    MainApplication.show()
    
    MainEventHandler.exec()

So my question about the correctness of the comment.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply