Integrating Gui::InputField into a Delegate class. Qt Model/View

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
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Integrating Gui::InputField into a Delegate class. Qt Model/View

Post by damian »

Hi

We are working to improve the editor material https://forum.freecadweb.org/viewtopic. ... 2&start=40

The idea is to use the FreeCADGui UiLoader with Gui::InputField, into the Delegate class, but is raising an error

Any wrong or idea?

Thank in advanced

Code: Select all


from PySide import QtCore, QtGui
import FreeCADGui
ui = FreeCADGui.UiLoader()

class MaterialDelegate(QtGui.QStyledItemDelegate):

    def createEditor(self, parent, option, index):
        if index.column() == 1:
            # editor = QtGui.QLineEdit()
            editor = ui.createWidget("Gui::InputField")
            editor.setParent(parent)
            return editor

    def setEditorData(self, editor, index):
        data = index.data()
        editor.setText(data)

    def setModelData(self, editor, model, index):
        data = editor.text()
        model.setData(index, data)

model = QtGui.QStandardItemModel()
tableView = QtGui.QTableView()
tableView.setModel(model)
delegate = MaterialDelegate()
tableView.setItemDelegate(delegate)
item = QtGui.QStandardItem("MyProperty")
it = QtGui.QStandardItem()
it.setText("10 m")
model.appendRow([item, it])
tableView.setWindowTitle("MaterialDelegate")
tableView.show()

Traceback (most recent call last):
File "<input>", line 15, in setModelData
AttributeError: 'PySide.QtGui.QWidget' object has no attribute 'text'
damian
Posts: 583
Joined: Sun May 31, 2015 6:16 pm

Re: Integrating Gui::InputField into a Delegate class. Qt Model/View

Post by damian »

Code: Select all

data = editor.property('text')
User avatar
yorik
Founder
Posts: 13665
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Integrating Gui::InputField into a Delegate class. Qt Model/View

Post by yorik »

Ah I was going to say, I just solved it, but I see you found it yourself :)
In case you want to see, I just did a Delegate class where the editor changes depending on the type of data, that might be exactly what we need for the mat editor..

https://github.com/FreeCAD/FreeCAD/blob ... t.py#L1355
Post Reply