PySide tut does not work for me

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
teobo
Posts: 410
Joined: Fri Feb 21, 2014 11:23 am

PySide tut does not work for me

Post by teobo »

Hi,
http://freecadweb.org/wiki/index.php?title=PyQt
I tried out this tut:

Code: Select all

from PySide import QtGui
app = QtGui.qApp
mw = app.activeWindow()
for child in mw.children():
   print 'widget name = ', child.objectName(), ', widget type = ', child

myWidget = QtGui.QDockWidget()
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'PySide.QtGui.QWidget' object has no attribute 'addDockWidget'
>>>

Any idea why is that? Flaws in the tutorials, documentation: are they to be reportet? Where#? In the bug list?
Tia
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PySide tut does not work for me

Post by wmayer »

Any idea why is that?
Yes, because PySide works slightly different to PyQt. Thus, you cannot just replace PyQt with PySide. But besides that the given PyQt code is also a bit fragile because the "active window" is not necessarily the main window, e.g. if a dialog is open.

The only proper way to access the main window is:

Code: Select all

mw = FreeCADGui.getMainWindow()
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: PySide tut does not work for me

Post by yorik »

Hm indeed I had that with the Draft module too, dock widgets behave a bit differently in PySide and PyQt. That wiki page needs a good rework...
User avatar
teobo
Posts: 410
Joined: Fri Feb 21, 2014 11:23 am

Re: PySide tut does not work for me

Post by teobo »

Code: Select all

>>> mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: 'PySide.QtGui.QWidget' object has no attribute 'addDockWidget'
>>> QtCore.Qt.RightDockWidgetArea
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'QtCore' is not defined
so the line in the freecad tutorial is broken, can anybody help to fix it. It has two flaws as seen.
Tia
Last edited by teobo on Sun May 25, 2014 2:16 pm, edited 1 time in total.
wmayer
Founder
Posts: 20324
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PySide tut does not work for me

Post by wmayer »

Code: Select all

from PySide import QtCore
User avatar
teobo
Posts: 410
Joined: Fri Feb 21, 2014 11:23 am

Re: PySide tut does not work for me

Post by teobo »

Thanks, it is running now without error message.

Tried out the first part of it. There were some things odd.
1. As the screen shoot (of exactly the first code block pasted below) shows: I could not make out, where to print it. When I execute in the console line by line: exactly nothing is printed.
2. referring to:
myWidget.resize(QtCore.QSize(300,100)) # sets size of the widget
label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
I can not recognize that these lines do anything. See screen shot. They did not change the geometry at least as it seems.
3. When I block-execute 2.dode block in the console (c&p) only a empty window appairs.

Alltogether: I lack a clear expectation of, what should I expect from the code? What explicitly wants to demonstrate this tutorial?
Tia

Code: Select all

import time
import Draft
import sys
from PySide import QtGui
from PySide import QtCore  #changed

app = QtGui.qApp
mw = FreeCADGui.getMainWindow()
myWidget = QtGui.QDockWidget()#changed
#mw = app.activeWindow() 
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myWidget)
myWidget.setObjectName("my Nice New Widget")
#Where do I notice a difference?
myWidget.resize(QtCore.QSize(300,100)) # sets size of the widget
#Where do I notice a difference?
label = QtGui.QLabel("Hallo World", myWidget) # creates a label
label = QtGui.QLabel("Hllo Worlds", myWidget) # creates a label
##debug
#Gui.SendMsgToActiveView("ViewFit")
#Gui.activeDocument().activeView().viewAxometric()
#time.sleep(2)
#Gui.SendMsgToActiveView("ViewFit")
#Gui.activeDocument().activeView().viewAxometric()
##debug sleep

label = QtGui.QLabel("Hello Worldsss", myWidget) # creates a label
##W can not change it in the console
label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size

Code: Select all

class myWidget_Ui(object):
  def setupUi(self, myWidget):
    myWidget.setObjectName("my Nice New Widget")
    myWidget.resize(QtCore.QSize(300,100).expandedTo(myWidget.minimumSizeHint())) # sets size of the widget

    self.label = QtGui.QLabel(myWidget) # creates a label
    self.label.setGeometry(QtCore.QRect(50,50,200,24)) # sets its size
    self.label.setObjectName("label") # sets its name, so it can be found by name

  def retranslateUi(self, draftToolbar): # built-in QT function that manages translations of widgets
    myWidget.setWindowTitle(QtGui.QApplication.translate("myWidget", "My Widget", None, QtGui.QApplication.UnicodeUTF8))
    self.label.setText(QtGui.QApplication.translate("myWidget", "Welcome to my new widget!", None, QtGui.QApplication.UnicodeUTF8))

myNewFreeCADWidget = QtGui.QDockWidget() # create a new dckwidget
myNewFreeCADWidget.ui = myWidget_Ui() # load the Ui script
myNewFreeCADWidget.ui.setupUi(myNewFreeCADWidget) # setup the ui
FCmw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myNewFreeCADWidget) # add the widget to the main window
#Error message tried instead: 
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea,myNewFreeCADWidget) # add the widget to the main window
#what has happened, what is it for?
#can not change the label afterwards in the console
Attachments
pyside_odds.png
pyside_odds.png (31.66 KiB) Viewed 3947 times
Post Reply