how to make a cell in a QTableWidget read only?

后端 未结 3 1742
闹比i
闹比i 2021-02-13 10:04

i have the following code defining the gui of my app

class Ui (object):
    def setupUi():
        self.tableName = QtGui.QTableWidget(self.layoutWidget_20)
             


        
3条回答
  •  暖寄归人
    2021-02-13 10:50

    I played a little with the code and read some more documentation the answer to the problem is

    def createtable(self):
         rows = self.tableName.rowCount()
         columns = self.tableName.columnCount()
         for i in range(rows):
             for j in range(columns):
                 item = self.cell("text")
                 # execute the line below to every item you need locked
                 item.setFlags(QtCore.Qt.ItemIsEnabled)
                 self.ui.tableName.setItem(i, j, item)
    

    The solution is the line "item.setFlags(QtCore.Qt.ItemIsEnabled)", you use it to set the cell property QtCore.Qt.ItemIsEnabled to disabled, so you can't select or edit the cell

    You can change a number of other properties this way at runtime as per documentarion on http://doc.qt.io/archives/qt-4.8/qt.html under the section Qt::ItemFlag

    as mentioned in a comment by Sven on the second answer to this question, if you have a static number of rows and columns in your QTableWidgetItem you can select the properties of the cells with Qtdesigner if you use it to create the screens for your application

提交回复
热议问题