module QWorkspace from PySide.QtGui

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
vito
Posts: 5
Joined: Wed Sep 15, 2021 6:07 pm

module QWorkspace from PySide.QtGui

Post by vito »

Hi.

Thanks again for the software and the forum in general. The answers are fast.

Today I was running a python script on a ubuntu machine (python 3.8) outside FreeCAD python console (that is, the ubuntu regular terminal) from this page https://wiki.freecadweb.org/Embedding_FreeCADGui

(the section called "Without even firing up the FreeCAD Gui")

I will show just the first lines

Code: Select all

import os, sys, FreeCAD, FreeCADGui
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QFileDialog, QApplication
from pivy.coin import SoInput, SoDB, sogui

Which I couldn't run without errors. I discovered that if I changed the PyQt4 for PySide like this

Code: Select all

PYSIDEPATH='/usr/share/freecad/Ext'
FREECADPATH= '/usr/lib/freecad-python3/lib'
PIVYPATH='/usr/lib/python3/dist-packages/'
import sys
sys.path.append(FREECADPATH)
sys.path.append(PYSIDEPATH)
sys.path.append(PIVYPATH)

import os, FreeCAD, FreeCADGui

from PySide import QtCore, QtGui
from PySide.QtGui import QMainWindow, QWorkspace, QAction, QFileDialog, QApplication
I could get almost with no errors: The terminal show me
ImportError: cannot import name 'QWorkspace' from 'PySide.QtGui' (/usr/share/freecad/Ext/PySide/QtGui.py)
I could check that all the other modules imported from PySide.QtGui DO exists, the only missing is 'QWorkspace'.

I imagine that maybe the name of the module was changed in an update (I'm using FreeCAD 0.19)

I check dir( PySide.QtGui) and got all this:

Code: Select all

>>> dir(PySide.QtGui)
['QAbstractButton', 'QAbstractGraphicsShapeItem', 'QAbstractItemDelegate', 'QAbstractItemView', 'QAbstractOpenGLFunctions', 'QAbstractScrollArea', 'QAbstractSlider', 'QAbstractSpinBox', 'QAbstractTextDocumentLayout', 'QAccessible', 'QAccessibleEditableTextInterface', 'QAccessibleEvent', 'QAccessibleInterface', 'QAccessibleObject', 'QAccessibleStateChangeEvent', 'QAccessibleTableCellInterface', 'QAccessibleTableModelChangeEvent', 'QAccessibleTextCursorEvent', 'QAccessibleTextInsertEvent', 'QAccessibleTextInterface', 'QAccessibleTextRemoveEvent', 'QAccessibleTextSelectionEvent', 'QAccessibleTextUpdateEvent', 'QAccessibleValueChangeEvent', 'QAccessibleValueInterface', 'QAccessibleWidget', 'QAction', 'QActionEvent', 'QActionGroup', 'QApplication', 'QBackingStore', 'QBitmap', 'QBoxLayout', 'QBrush', 'QButtonGroup', 'QCalendarWidget', 'QCheckBox', 'QClipboard', 'QCloseEvent', 'QColor', 'QColorDialog', 'QColormap', 'QColumnView', 'QComboBox', 'QCommandLinkButton', 'QCommonStyle', 'QCompleter', 'QConicalGradient', 'QContextMenuEvent', 'QCursor', 'QDataWidgetMapper', 'QDateEdit', 'QDateTimeEdit', 'QDesktopServices', 'QDesktopWidget', 'QDial', 'QDialog', 'QDialogButtonBox', 'QDirModel', 'QDockWidget', 'QDoubleSpinBox', 'QDoubleValidator', 'QDrag', 'QDragEnterEvent', 'QDragLeaveEvent', 'QDragMoveEvent', 'QDropEvent', 'QEnterEvent', 'QErrorMessage', 'QExposeEvent', 'QFileDialog', 'QFileIconProvider', 'QFileOpenEvent', 'QFileSystemModel', 'QFocusEvent', 'QFocusFrame', 'QFont', 'QFontComboBox', 'QFontDatabase', 'QFontDialog', 'QFontInfo', 'QFontMetrics', 'QFontMetricsF', 'QFormLayout', 'QFrame', 'QGesture', 'QGestureEvent', 'QGestureRecognizer', 'QGradient', 'QGraphicsAnchor', 'QGraphicsAnchorLayout', 'QGraphicsBlurEffect', 'QGraphicsColorizeEffect', 'QGraphicsDropShadowEffect', 'QGraphicsEffect', 'QGraphicsEllipseItem', 'QGraphicsGridLayout', 'QGraphicsItem', 'QGraphicsItemAnimation', 'QGraphicsItemGroup', 'QGraphicsLayout', 'QGraphicsLayoutItem', 'QGraphicsLineItem', 'QGraphicsLinearLayout', 'QGraphicsObject', 'QGraphicsOpacityEffect', 'QGraphicsPathItem', 'QGraphicsPixmapItem', 'QGraphicsPolygonItem', 'QGraphicsProxyWidget', 'QGraphicsRectItem', 'QGraphicsRotation', 'QGraphicsScale', 'QGraphicsScene', 'QGraphicsSceneContextMenuEvent', 'QGraphicsSceneDragDropEvent', 'QGraphicsSceneEvent', 'QGraphicsSceneHelpEvent', 'QGraphicsSceneHoverEvent', 'QGraphicsSceneMouseEvent', 'QGraphicsSceneMoveEvent', 'QGraphicsSceneResizeEvent', 'QGraphicsSceneWheelEvent', 'QGraphicsSimpleTextItem', 'QGraphicsTextItem', 'QGraphicsTransform', 'QGraphicsView', 'QGraphicsWidget', 'QGridLayout', 'QGroupBox', 'QGuiApplication', 'QHBoxLayout', 'QHeaderView', 'QHelpEvent', 'QHideEvent', 'QHoverEvent', 'QIcon', 'QIconDragEvent', 'QIconEngine', 'QImage', 'QImageIOHandler', 'QImageReader', 'QImageWriter', 'QInputDialog', 'QInputEvent', 'QInputMethod', 'QInputMethodEvent', 'QInputMethodQueryEvent', 'QIntValidator', 'QItemDelegate', 'QItemEditorCreatorBase', 'QItemEditorFactory', 'QKeyEvent', 'QKeyEventTransition', 'QKeySequence', 'QKeySequenceEdit', 'QLCDNumber', 'QLabel', 'QLayout', 'QLayoutItem', 'QLineEdit', 'QLinearGradient', 'QListView', 'QListWidget', 'QListWidgetItem', 'QMainWindow', 'QMatrix', 'QMatrix2x2', 'QMatrix2x3', 'QMatrix2x4', 'QMatrix3x2', 'QMatrix3x3', 'QMatrix3x4', 'QMatrix4x2', 'QMatrix4x3', 'QMatrix4x4', 'QMdiArea', 'QMdiSubWindow', 'QMenu', 'QMenuBar', 'QMessageBox', 'QMouseEvent', 'QMouseEventTransition', 'QMoveEvent', 'QMovie', 'QNativeGestureEvent', 'QOffscreenSurface', 'QOpenGLBuffer', 'QOpenGLContext', 'QOpenGLContextGroup', 'QOpenGLDebugLogger', 'QOpenGLDebugMessage', 'QOpenGLExtraFunctions', 'QOpenGLFramebufferObject', 'QOpenGLFramebufferObjectFormat', 'QOpenGLFunctions', 'QOpenGLPixelTransferOptions', 'QOpenGLShader', 'QOpenGLShaderProgram', 'QOpenGLTexture', 'QOpenGLTextureBlitter', 'QOpenGLTimeMonitor', 'QOpenGLTimerQuery', 'QOpenGLVersionProfile', 'QOpenGLVertexArrayObject', 'QOpenGLWidget', 'QOpenGLWindow', 'QPageLayout', 'QPageSize', 'QPagedPaintDevice', 'QPaintDevice', 'QPaintDeviceWindow', 'QPaintEngine', 'QPaintEngineState', 'QPaintEvent', 'QPainter', 'QPainterPath', 'QPainterPathStroker', 'QPalette', 'QPanGesture', 'QPdfWriter', 'QPen', 'QPicture', 'QPictureIO', 'QPinchGesture', 'QPixelFormat', 'QPixmap', 'QPixmapCache', 'QPlainTextDocumentLayout', 'QPlainTextEdit', 'QPointingDeviceUniqueId', 'QPolygon', 'QPolygonF', 'QProgressBar', 'QProgressDialog', 'QProxyStyle', 'QPushButton', 'QPyTextObject', 'QQuaternion', 'QRadialGradient', 'QRadioButton', 'QRasterWindow', 'QRawFont', 'QRegExpValidator', 'QRegion', 'QResizeEvent', 'QRubberBand', 'QScreen', 'QScrollArea', 'QScrollBar', 'QScrollEvent', 'QScrollPrepareEvent', 'QScroller', 'QScrollerProperties', 'QSessionManager', 'QShortcut', 'QShortcutEvent', 'QShowEvent', 'QSizeGrip', 'QSizePolicy', 'QSlider', 'QSpacerItem', 'QSpinBox', 'QSplashScreen', 'QSplitter', 'QSplitterHandle', 'QStackedLayout', 'QStackedWidget', 'QStandardItem', 'QStandardItemModel', 'QStaticText', 'QStatusBar', 'QStatusTipEvent', 'QStyle', 'QStyleFactory', 'QStyleHintReturn', 'QStyleHintReturnMask', 'QStyleHintReturnVariant', 'QStyleHints', 'QStyleOption', 'QStyleOptionButton', 'QStyleOptionComboBox', 'QStyleOptionComplex', 'QStyleOptionDockWidget', 'QStyleOptionFocusRect', 'QStyleOptionFrame', 'QStyleOptionGraphicsItem', 'QStyleOptionGroupBox', 'QStyleOptionHeader', 'QStyleOptionMenuItem', 'QStyleOptionProgressBar', 'QStyleOptionRubberBand', 'QStyleOptionSizeGrip', 'QStyleOptionSlider', 'QStyleOptionSpinBox', 'QStyleOptionTab', 'QStyleOptionTabBarBase', 'QStyleOptionTabWidgetFrame', 'QStyleOptionTitleBar', 'QStyleOptionToolBar', 'QStyleOptionToolBox', 'QStyleOptionToolButton', 'QStyleOptionViewItem', 'QStylePainter', 'QStyledItemDelegate', 'QSurface', 'QSurfaceFormat', 'QSwipeGesture', 'QSyntaxHighlighter', 'QSystemTrayIcon', 'QTabBar', 'QTabWidget', 'QTableView', 'QTableWidget', 'QTableWidgetItem', 'QTableWidgetSelectionRange', 'QTabletEvent', 'QTapAndHoldGesture', 'QTapGesture', 'QTextBlock', 'QTextBlockFormat', 'QTextBlockGroup', 'QTextBlockUserData', 'QTextBrowser', 'QTextCharFormat', 'QTextCursor', 'QTextDocument', 'QTextDocumentFragment', 'QTextDocumentWriter', 'QTextEdit', 'QTextFormat', 'QTextFragment', 'QTextFrame', 'QTextFrameFormat', 'QTextImageFormat', 'QTextInlineObject', 'QTextItem', 'QTextLayout', 'QTextLength', 'QTextLine', 'QTextList', 'QTextListFormat', 'QTextObject', 'QTextObjectInterface', 'QTextOption', 'QTextTable', 'QTextTableCell', 'QTextTableCellFormat', 'QTextTableFormat', 'QTileRules', 'QTimeEdit', 'QToolBar', 'QToolBarChangeEvent', 'QToolBox', 'QToolButton', 'QToolTip', 'QTouchDevice', 'QTouchEvent', 'QTransform', 'QTreeView', 'QTreeWidget', 'QTreeWidgetItem', 'QTreeWidgetItemIterator', 'QUndoCommand', 'QUndoGroup', 'QUndoStack', 'QUndoView', 'QVBoxLayout', 'QValidator', 'QVector2D', 'QVector3D', 'QVector4D', 'QWhatsThis', 'QWhatsThisClickedEvent', 'QWheelEvent', 'QWidget', 'QWidgetAction', 'QWidgetItem', 'QWindow', 'QWindowStateChangeEvent', 'QWizard', 'QWizardPage', 'Qt', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'qAlpha', 'qApp', 'qBlue', 'qGray', 'qGreen', 'qIsGray', 'qRed', 'qRgb', 'qRgba']
Couldn't figure if any of those is the new version.

Can someone help me please?
Syres
Veteran
Posts: 2893
Joined: Thu Aug 09, 2018 11:14 am

Re: module QWorkspace from PySide.QtGui

Post by Syres »

vito
Posts: 5
Joined: Wed Sep 15, 2021 6:07 pm

Re: module QWorkspace from PySide.QtGui

Post by vito »

Thanks for the fast answer.

I'm not sure if that is going to be the answer

The full code as appears here https://wiki.freecadweb.org/Embedding_FreeCADGui is this

Code: Select all

import os
import sys
 
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QMainWindow, QWorkspace, QAction, QApplication
 
from pivy.coin import SoInput, SoDB
from pivy.quarter import QuarterWidget
import FreeCADGui
 
 
class MdiQuarterWidget(QuarterWidget):
    def __init__(self, parent, sharewidget):
        QuarterWidget.__init__(self, parent=parent, sharewidget=sharewidget)
 
    def minimumSizeHint(self):
        return QtCore.QSize(640, 480)
 
 
class MdiMainWindow(QMainWindow):
    def __init__(self, qApp):
        QMainWindow.__init__(self)
        self._firstwidget = None
        self._workspace = QWorkspace()
        self.setCentralWidget(self._workspace)
        self.setAcceptDrops(True)
        self.setWindowTitle("Pivy Quarter MDI example")
 
        filemenu = self.menuBar().addMenu("&File")
        windowmenu = self.menuBar().addMenu("&Windows")
 
        fileopenaction = QAction("&Create Box", self)
        fileexitaction = QAction("E&xit", self)
        tileaction = QAction("Tile", self)
        cascadeaction = QAction("Cascade", self)
 
        filemenu.addAction(fileopenaction)
        filemenu.addAction(fileexitaction)
        windowmenu.addAction(tileaction)
        windowmenu.addAction(cascadeaction)
 
        self.connect(fileopenaction, QtCore.SIGNAL("triggered()"), self.createBoxInFreeCAD)
        self.connect(fileexitaction, QtCore.SIGNAL("triggered()"), QtGui.qApp.closeAllWindows)
        self.connect(tileaction, QtCore.SIGNAL("triggered()"), self._workspace.tile)
        self.connect(cascadeaction, QtCore.SIGNAL("triggered()"), self._workspace.cascade)
 
        windowmapper = QtCore.QSignalMapper(self)
        self.connect(windowmapper, QtCore.SIGNAL("mapped(QWidget *)"), self._workspace.setActiveWindow)
 
        self.dirname = os.curdir       
 
    def closeEvent(self, event):
        self._workspace.closeAllWindows()
 
    def createBoxInFreeCAD(self):
        d=FreeCAD.newDocument()
        o=d.addObject("Part::Box")
        d.recompute()
        s=FreeCADGui.subgraphFromObject(o)
        child = self.createMdiChild()
        child.show()
        child.setSceneGraph(s)
 
    def createMdiChild(self):
        widget = MdiQuarterWidget(None, self._firstwidget)
        self._workspace.addWindow(widget)
        if not self._firstwidget:
            self._firstwidget = widget
        return widget
 
 
def main():
    app = QApplication(sys.argv)
    FreeCADGui.setupWithoutGUI()        
    mdi = MdiMainWindow(app)   
    mdi.show()
    sys.exit(app.exec_())
 
 
if __name__ == '__main__':
    main()

The problem appears at the line

Code: Select all

class MdiMainWindow(QMainWindow):
when you try to assign to

Code: Select all

self._workspace = QWorkspace()
All the imports I replace them for this

Code: Select all

PYSIDEPATH='/usr/share/freecad/Ext'
FREECADPATH= '/usr/lib/freecad-python3/lib'
PIVYPATH='/usr/lib/python3/dist-packages/'
import sys
sys.path.append(FREECADPATH)
sys.path.append(PYSIDEPATH)
sys.path.append(PIVYPATH)

import os

import PySide
from PySide import QtCore, QtGui
from PySide.QtGui import QMainWindow, QAction, QApplication
from PyQt5 import QtWidgets  #QtWidgets.QMdiArea() replaces QWorkspace

from pivy.coin import SoInput, SoDB
from pivy.quarter import QuarterWidget
import FreeCADGui


And then replace to

Code: Select all

self._workspace = QtWidgets.QMdiArea()
But I receive this error
TypeError: 'PySide2.QtWidgets.QMainWindow.setCentralWidget' called with wrong argument types:
PySide2.QtWidgets.QMainWindow.setCentralWidget(QMdiArea)
Supported signatures:
PySide2.QtWidgets.QMainWindow.setCentralWidget(PySide2.QtWidgets.QWidget)
I could check that the self._worspace expects a PySide2.QtWidgets.QWidget object

from this https://wiki.freecadweb.org/PySide it states that
The only unusual aspect is that the PySide2.QtWidgets classes are placed in the PySide.QtGui namespace.
so I replace

Code: Select all

self._workspace = PySide.QtGui.QWidget()
But this error appears
File "pruebas_varias.py", line 90, in main
mdi = MdiMainWindow(app)
File "pruebas_varias.py", line 59, in __init__
self.connect(tileaction, QtCore.SIGNAL("triggered()"), self._workspace.tile)
AttributeError: 'PySide2.QtWidgets.QWidget' object has no attribute 'tile'

I'm still reviewing.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: module QWorkspace from PySide.QtGui

Post by onekk »

You are talking about Qt4 and you don't specify what version of FreeCAD you are using.

So every answer could be wrong, as far I remember Qt4 is almist old at least FreeCAD version 0.18 and 0.19, will use Qt5 as you could see clearly in the name of AppImages.

Maybe it will be interesting to have some information about your FreeCAD version.

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