How can I setFlags as I setItem?



  • Is there any way of setting item flags as I set the item for a table?

    Trying the code below will result in the item not being set at all:

    my_table.setItem(0,0,QtWidgets.QTableWidgetItem("John").setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled))
    

    Qt doesn't like this either:

    item = QtWidgets.QTableWidgetItem("John").setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
    my_table.setItem(0,0,item)
    

  • Moderators

    I'm not a Python expert but I believe it should be something like this:

    item = QtWidgets.QTableWidgetItem("John")
    item.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
    my_table.setItem(0,0,item)
    


  • @Chris-Kawa Yes, I know this way works. I was just wondering if it can be set at the same time to save lines declaring a variable and setting flags indivudally to the variable..

    In any case: I've found an answer that seems a lot more practical in my case (http://stackoverflow.com/questions/24024815/set-a-whole-column-in-qtablewidget-read-only-in-python/24026472#2402647) It uses setItemDelegateForColumn to make an entire column "read only"


  • Moderators

    item = QtWidgets.QTableWidgetItem("John").setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
    

    Again, not a Python expert, but this looks just wrong. It assigns a result of a setFlags function to item. But setFlags does not return any value so there is nothing to assign. In c++ at least this wouldn't even compile.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.