What PyQt widget best suited if I'm trying to recreate a .Net listView

Hi,

We’re migrating our custom toolset to Maya and I’m trying to get my head around some of the PyQT/Pyside way of doing things. I come from a 3dsMax background and used .Net extensively (often wrapped in Maxscript).

I’m a little confused as to how to get similar results within PyQt. For instance, I wanted to create the equivalent of a .Net listView in Qt, and it seems my only alternative is a QTableWidget. Having put in basic code to add elements to my tableWidget, I realised that if I lost focus on the table - the selection returned is empty. More importantly, in Maya I can’t seem to see any selection other than a dashed line around the selected cell even if tableWidget’s selectionMode is ExtendedSelection, and the selectionBehavior is SelectRows.

Hopefully someone with both experience in winForms and Qt can enlighten me or point out what I’m doing wrong. Here’s what I’d like to do:

[ul]
[li]Be able to select row(s) by simply selection a cell of a given row, but also be able to edit specific cells.[/li][li]Retain row or cell selection when the widget loses focus. This is default behavior in a .Net listView.[/li][/ul]

I’ve added a snippet of my code to add a row which connected to the clicked signal of a pushbutton. This is really all I have so far. The window was built with qDesigner and I have horizontalHeaders but disabled verticalHeaders to more closely reflect the existing tools from the .Net listView.



from PyQt4 import QtCore, QtGui, uic
import sip

# example of dataList format, the second element of the tuple determines whether the item is editable
dataList = [('some string', True), (45, True), (0.25, False)]

def addRow(self, table, dataList):      
      row = table.rowCount()
      table.insertRow(row)
      for index, data in enumerate(dataList):
          item = QtGui.QTableWidgetItem()
          flags = QtCore.Qt.ItemIsEnabled
          if data[1]:
              flags = (flags | QtCore.Qt.ItemIsEditable)
          if not index:
              flags = (flags | QtCore.Qt.ItemIsUserCheckable)
              item.setCheckState(QtCore.Qt.Checked)
          item.setFlags(flags)
          item.setData(QtCore.Qt.EditRole, data[0])
          table.setItem(row, index, item)


Am I doing something that overrides the selectionMode or focus settings of the tableWidget? Should I be using the a QStandardItemModel instead of adding directly into the tableWidget? If so, how does that differ from entering data directly into a tableWidget…

More to the point, should I be using a tableWidget as the equivalent of a listView? I just noticed that a treeWidget also displays columns…

Any insight would be greatly appreciated.

CL Audio

a QTreeView would probably be better. Use that and a QStandardItemModel then just add QStandardItem(s) to your model. The difference between the view and model vs just adding directly to a tablewidget i maintainability and separation of responsibilities. If you use the model, you’ll still need to add rows to it just as youre doing with the tablewidget.

Thanks for the insight Maxx.

For the record, I already figured out what was going with my selection issue: my item flags didn’t include QtCore.Qt.ItemIsSelectable.

It’s now working as expected and selectedItems is returning what I expected.

If you have a specific need that only .NET can solve, there are a bunch of threads on here regarding Pythonnet. You can use all your favorite UI components as well as all the other .NET stuff. So, for example, you can have a WPF UI populate its data from a WCF IIS backend with “no code” in between. The big downside is that is is much more difficult to include Maya native widgets in your UI and the interfaces seem bolted on since they don’t inherit the look and feel. Also, you probably have to compile it yourself for your version of Maya’s particular flavor of Python.

You probably don’t want to get too distracted by this, but I thought you might want to know it’s out there.

Thanks for the info btribble. I found some references to .Net within Maya as well, but I’m digging the model-based approach Qt uses. In fact, coming from .Net and 3dsMax I’m having a harder time with Maya’s command based scripting than Qt itself.

Yeah, Maya is pretty old, and OO programming was still a new thing. MEL is pretty old school and the Python API is derived from that. There are rumors that the guy that implemented the Python stuff wanted to be much more object oriented, but the man-hour estimates killed that idea. Most people would rather do object1.x = 11 or vert_red = object1.mesh.vtx[0].colors.red than use setaddr/getaddr. It is pretty clunky. PyMel improves a bit of this, but it brings it’s own baggage as well. For instance you can use object pointers in PyMel rather than names, but that doesn’t mean that you can now have 30 objects at the root of the scene that all have the same name. (That’s a good thing right?) Having used a fair amount of Max scripting, you will find that the system as a whole is generally more uniform and complete. I don’t know how many times I wondered why one MaxScript function took a named argument, and a very similar function took a string. Worse, tons of stuff in Max isn’t exposed at all, especially when you’re crawling in the deep corners of a little used stack object. I know they cleaned a bunch of this up when they added the .NET API, but I have less familiarity with that.

ya in Maya I just ignore than Maya.cmds and Mel exist, and work all with pymel or the api.