create a FreeCADGui Qt widget instead of QtGui Qt widget

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
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by bernd »

I know how to create QtGui widgets like QLabel and QDoubleSpinBox directly by Python for a Taskpanel without ui file.

Code: Select all

from PySide import QtCore, QtGui
import FreeCADGui
class MyTaskPanel(QtGui.QWidget):
    def __init__(self):
        super(MyTaskPanel, self).__init__()
        self.myspinbox = QtGui.QDoubleSpinBox(self)
        self.setLayout(QtGui.QGridLayout().addWidget(self.myspinbox, 1, 0))


mw = FreeCADGui.getMainWindow()
awidget = QtGui.QDockWidget("MyTaskPanel", mw)
awidget.setWidget(MyTaskPanel())
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea, awidget)

But how do I get a "Gui::PrefFileChooser" by Python. All PrefFileChooser I found are implemented in c++ or by the use of a ui file.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by wmayer »

Code: Select all

ui = Gui.UiLoader()
w = ui.createWidget("Gui::PrefFileChooser")
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by bernd »

:D

Code: Select all

from PySide import QtCore, QtGui
import FreeCADGui
class MyTaskPanel(QtGui.QWidget):
    def __init__(self):
        super(MyTaskPanel, self).__init__()
        ui = FreeCADGui.UiLoader()
        self.myfilechooser = ui.createWidget("Gui::PrefFileChooser")
        self.myfilechooser.setParent(self)
        self.setLayout(QtGui.QGridLayout().addWidget(self.myfilechooser, 1, 0))


mw = FreeCADGui.getMainWindow()
awidget = QtGui.QDockWidget("MyTaskPanel", mw)
awidget.setWidget(MyTaskPanel())
mw.addDockWidget(QtCore.Qt.RightDockWidgetArea, awidget)

User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by bernd »

wmayer wrote: Thu Oct 22, 2020 7:53 am

Code: Select all

ui = Gui.UiLoader()
w = ui.createWidget("Gui::PrefFileChooser")

how do I set a path or file? How do I get a file or path?

Code: Select all

ui = Gui.UiLoader()
w = ui.createWidget("Gui::PrefFileChooser")
w.__dir__()
type(w)
I can not find anything in this regard in there ...
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by wmayer »

In C++ it has the property "Mode" but I don't know how to set it from Python because Mode is a C++ enum. I have tried a few things but nothing of them worked.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by bernd »

Should I try to use the QFileDialog? What is the advantage of the Gui::PrefFileChooser?

Or is the PrefFileChooser only for FreeCAD preferences?

I need it for a taskpanel widget, nothing to do with FreeCAD preferences. In the task panel I would like to retrieve a file path and a directory path for further processing.

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

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by openBrain »

bernd wrote: Thu Oct 22, 2020 6:27 pm Should I try to use the QFileDialog? What is the advantage of the Gui::PrefFileChooser?

Or is the PrefFileChooser only for FreeCAD preferences?

I need it for a taskpanel widget, nothing to do with FreeCAD preferences. In the task panel I would like to retrieve a file path and a directory path for further processing.

bernd
Pref widgets basically offers methods to be saved to and restored from the user settings. They also have a kind a call back when the setting is changed while the widget is active.
If you don't need these functions, using a QFileDialog is totally fine.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by wmayer »

Should I try to use the QFileDialog? What is the advantage of the Gui::PrefFileChooser?
It's a compact widget which saves you some extra code. But if it's not possible to use it from Python then it's best to use a file dialog instead.
I need it for a taskpanel widget, nothing to do with FreeCAD preferences. In the task panel I would like to retrieve a file path and a directory path for further processing.
Use standard line edit and a browse button. With the browse button open the file dialog which sets the path to the line edit.
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: create a FreeCADGui Qt widget instead of QtGui Qt widget

Post by bernd »

found a example in the code I maintain. I should have known about it ... :shock: 8-)

https://github.com/FreeCAD/FreeCAD/blob ... #L216-L221
Post Reply