bitwise or on class objects

298 views Asked by At

How using Bitwise OR operator in item.flags() | QtCore.Qt.ItemIsEditableQtCore in the following code prevents item.flags from being overwritten? I know how Bitwise OR works on numbers but i can't make sense of its use here:

import sys
from PySide import QtCore, QtGui

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    app.setStyle("cleanLooks")

    #Data
    dataList = ["one", "two", "three", "four", "five"]

    #Creating item based QlistWidget
    listWidget = QtGui.QListWidget()
    listWidget.addItems(dataList)
    listWidget.show()

    #Make all items of the listWidget editable
    count = listWidget.count()
    for i in range(count):
        item = listWidget.item(i)
        item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)

    #Creating item based QComboBox
    comboBoxWidget = QtGui.QComboBox()
    comboBoxWidget.addItems(dataList)
    comboBoxWidget.show()

    sys.exit(app.exec_())
1

There are 1 answers

0
zimmerrol On BEST ANSWER

These flags are stored as powers of 2, so they are for example 1, 2, 4, 8, .... If you convert these into the dual base you see them as 0001, 0010, 0100, 1000, ....

If you now have the flag value item.flags() and combine it with the bitwise or | with QtCore.Qt.ItemIsEditable you are creating a new value, which has (as an example) this dual base representation:

flag:           10001100 
ItemIsEditable: 01000000
------------------------
result:         11001100

So it is just the normal bitwise or operator, between an integer number and the integer values of the Qt enum

This means especially, that if the item.flags() value has stored some flags already, you can add more flags with this | operator, without unsetting/overwriting the information of the previous set flags; this allows you to store information about multiple flags in just one integer value.