Qt enum access from Slicer using Python?

I’m working on a custom module and want to use a QTableWidget filled with QTableWidgetItems. I have that working, but as I work on it, I keep coming across Qt enums being used to set various policies or flags. For example, to control how QTableWidgetItems can be interacted with, I need to use QTableWidgetItems.setFlags(). Qt documentation examples explain that this function takes a logical combination of enums like this

QTableWidgetItem *item = new QTableWidgetItem();
item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);

Is there any way to access these enums from a python slicer module? My workaround is currently to dig in the Qt documentation to find out what integers the enums correspond to (e.g. https://doc.qt.io/qt-5/qt.html#ItemFlag-enum) and use that number. So far, that works fine, but is much uglier and less clear than the named enum approach.

The equivalent version I have working is

item = qt.QTableWidgetItem()
item.setFlags(33) # 33 because selectable is 1 and enabled is 32 and to combine them you add them up

I’ve tried searching the slicer source on github and exploring the autocomplete methods and properties under qt.Q… and a couple qt.QAbstract… classes, but have not found any leads. Some of these enums show up in .ui files, but not in anything python that I’ve found. Thanks for any help you can provide.

1 Like

Yes, you can access the enums in the Qt namespace like this:

qt.Qt.ItemIsEnabled
3 Likes

Thank you! I felt like there should be something like that, but did not find it!