Couple of Qt Questions.

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!
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Couple of Qt Questions.

Post by keithsloan52 »

I am trying to control the size of a LineEdit box

I am trying

Code: Select all

self.value = QtGui.QLineEdit()
self.value.setMaxLength = len
self.value.setGeometry(QtCore.QRect(0,0,10,5))
But the setGeometry does not seem to have any effect.

I would also like to drive a FocusOutEvent on when the mouse moves outside of the dialog
the c++ doc has

Code: Select all

void QLineEdit::focusOutEvent(QFocusEvent *e)
But how do I do it in python

I tried adding to the class that defines the task and to. the panel widget

Code: Select all

       def focueOutEvent(self, event) :
              print('Went out of focus')
But did not seem to be driven.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Couple of Qt Questions.

Post by openBrain »

keithsloan52 wrote: Sun May 23, 2021 4:08 pm I am trying to control the size of a LineEdit box

I am trying

Code: Select all

self.value = QtGui.QLineEdit()
self.value.setMaxLength = len
self.value.setGeometry(QtCore.QRect(0,0,10,5))
But the setGeometry does not seem to have any effect.

setMaxLength is a function, not an attribute.
Also geometry is quite complex as real effects mainly depend on SizePolicy.

I would also like to drive a FocusOutEvent on when the mouse moves outside of the dialog
the c++ doc has

Code: Select all

void QLineEdit::focusOutEvent(QFocusEvent *e)
But how do I do it in python

I tried adding to the class that defines the task and to. the panel widget

Code: Select all

       def focueOutEvent(self, event) :
              print('Went out of focus')
But did not seem to be driven.
Did you try without making a typo in the function name?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Couple of Qt Questions.

Post by keithsloan52 »

openBrain wrote: Sun May 23, 2021 4:13 pm
keithsloan52 wrote: Sun May 23, 2021 4:08 pm I am trying to control the size of a LineEdit box

I am trying

Code: Select all

self.value = QtGui.QLineEdit()
self.value.setMaxLength = len
self.value.setGeometry(QtCore.QRect(0,0,10,5))
But the setGeometry does not seem to have any effect.

setMaxLength is a function, not an attribute.
Also geometry is quite complex as real effects mainly depend on SizePolicy.

Changed it but still no effect on the size of the box. Think MaxLength applies to amount of
text that can be entered. setGeometry did not seem to work.
added a function

Code: Select all

      def sizeHint(self) :
           return(Qtcore.QSize(10,5))
But that did not help either

I would also like to drive a FocusOutEvent on when the mouse moves outside of the dialog
the c++ doc has

Code: Select all

void QLineEdit::focusOutEvent(QFocusEvent *e)
But how do I do it in python

I tried adding to the class that defines the task and to. the panel widget

Code: Select all

       def focueOutEvent(self, event) :
              print('Went out of focus')
But did not seem to be driven.

Did you try without making a typo in the function name?
:-) Yes, cut and pasted from code this time

Code: Select all

def focusOutEvent(self, event) :
        print('Out of Focus')
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Couple of Qt Questions.

Post by openBrain »

Can you share full code?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Couple of Qt Questions.

Post by keithsloan52 »

Okay by reading the docs worked out that I needed a leaveEvent rather than a focusOutEvent .

Which gets driven when I Ieave the Task panel, but if I try closeDialog it crashed FreeCAD

Code: Select all

def leaveEvent(self, event) :
        print('Leave Event')
        FreeCADGui.Control.closeDialog()
Is there a way to close the panel from one of its events?
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Couple of Qt Questions.

Post by keithsloan52 »

openBrain wrote: Sun May 23, 2021 6:02 pm Can you share full code?
Full code https://github.com/KeithSloan/GDML branch Gmsh

Widget code

Code: Select all

class oField(QtGui.QWidget) :
    def __init__(self,label,len, value,parent=None) :
        super(oField, self).__init__(parent)
        self.label = QtGui.QLabel(label)
        self.value = QtGui.QLineEdit()
        self.value.setMaxLength(len)
        self.value.setGeometry(QtCore.QRect(0,0,10,5))
        self.value.setReadOnly(True)
        self.value.setText(value)
        self.label.setBuddy(self.value)
        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.value)
        self.setLayout(layout)

    def sizeHint(self) :
        return(QtCore.QSize(10,5))


openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Couple of Qt Questions.

Post by openBrain »

keithsloan52 wrote: Sun May 23, 2021 6:17 pm Okay by reading the docs worked out that I needed a leaveEvent rather than a focusOutEvent .

Which gets driven when I Ieave the Task panel, but if I try closeDialog it crashed FreeCAD

Code: Select all

def leaveEvent(self, event) :
        print('Leave Event')
        FreeCADGui.Control.closeDialog()
Is there a way to close the panel from one of its events?
I'm with a mobile now but something like

Code: Select all

QtCore.QMetaObject.invokeMethod(Gui.Control, 'closeDialog', QtCore.Qt.QueuedConnection)
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Couple of Qt Questions.

Post by keithsloan52 »

openBrain wrote: Sun May 23, 2021 7:10 pm
keithsloan52 wrote: Sun May 23, 2021 6:17 pm Okay by reading the docs worked out that I needed a leaveEvent rather than a focusOutEvent .

Which gets driven when I Ieave the Task panel, but if I try closeDialog it crashed FreeCAD

Code: Select all

def leaveEvent(self, event) :
        print('Leave Event')
        FreeCADGui.Control.closeDialog()
Is there a way to close the panel from one of its events?
I'm with a mobile now but something like

Code: Select all

QtCore.QMetaObject.invokeMethod(Gui.Control, 'closeDialog', QtCore.Qt.QueuedConnection)
Had to change the Gui.Control to FreeCADGui.Control to get further but then got

Code: Select all

09:56:54  Traceback (most recent call last):
09:56:54    File "/Users/keithsloan/Library/Preferences/FreeCAD/Mod/GDML/freecad/gdml/GDMLCommands.py", line 697, in leaveEvent
09:56:54      QtCore.QMetaObject.invokeMethod(FreeCADGui.Control, 'closeDialog', QtCore.Qt.QueuedConnection)
09:56:54  TypeError: 'PySide2.QtCore.QMetaObject.invokeMethod' called with wrong argument types:
  PySide2.QtCore.QMetaObject.invokeMethod(Control, str, ConnectionType)
Supported signatures:
  PySide2.QtCore.QMetaObject.invokeMethod(PySide2.QtCore.QObject, str, PySide2.QtCore.Qt.ConnectionType, PySide2.QtCore.QGenericReturnArgument, PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ...)
  PySide2.QtCore.QMetaObject.invokeMethod(PySide2.QtCore.QObject, str, PySide2.QtCore.QGenericReturnArgument, PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ...)
  PySide2.QtCore.QMetaObject.invokeMethod(PySide2.QtCore.QObject, str, PySide2.QtCore.Qt.ConnectionType, PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ...)
  PySide2.QtCore.QMetaObject.invokeMethod(PySide2.QtCore.QObject, str, PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ..., PySide2.QtCore.QGenericArgument = ...)
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Couple of Qt Questions.

Post by openBrain »

Hmm IIRC I had this in C++ too but I don't think we can qobject_cast in Python. I'll eventually have a look at this, but in between maybe the dirty method may work

Code: Select all

QtCore.QTimer.singleShot(0, FreeCADGui.Control, SLOT('closeDialog()'))
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Couple of Qt Questions.

Post by keithsloan52 »

openBrain wrote: Mon May 24, 2021 9:46 am Hmm IIRC I had this in C++ too but I don't think we can qobject_cast in Python. I'll eventually have a look at this, but in between maybe the dirty method may work

Code: Select all

QtCore.QTimer.singleShot(0, FreeCADGui.Control, SLOT('closeDialog()'))

Code: Select all

11:43:24  Leave Event
11:43:24  Traceback (most recent call last):
11:43:24    File "/Users/keithsloan/Library/Preferences/FreeCAD/Mod/GDML/freecad/gdml/GDMLCommands.py", line 698, in leaveEvent
11:43:24      QtCore.QTimer.singleShot(0, FreeCADGui.Control, SLOT('closeDialog()'))
11:43:24  NameError: name 'SLOT' is not defined
Not urgent but would be good to have a vanilla solution
Post Reply