如何使 QTableWidget 中的单元格只读? [英] how to make a cell in a QTableWidget read only?

查看:74
本文介绍了如何使 QTableWidget 中的单元格只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码定义我的应用程序的 gui

i have the following code defining the gui of my app

class Ui (object):
    def setupUi():
        self.tableName = QtGui.QTableWidget(self.layoutWidget_20)
        self.tableName.setObjectName(_fromUtf8("twHistoricoDisciplinas"))
        self.tableName.setColumnCount(4)
        self.tableName.setRowCount(3)

以及我的应用程序中的以下代码

and the following code in my app

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        self.ui = Ui()
        self.ui.setupUi(self)
        self.createtable()

    #creating a tw cell
    def cell(self,var=""):
            item = QtGui.QTableWidgetItem()
            item.setText(var)
            return item

    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")
                 self.ui.tableName.setItem(i, j, item)

我希望能够添加新的行和列并对其进行编辑,但我想锁定某些单元格.(我已经有了扩展表格的代码)如何在保持其他单元读写的同时使某些单元格只读?我找到了这个链接 How to make a column in QTableWidget read only? 用 C++ 中的问题解决方案,python 解决方案是否类似?

I want to be able to add new rows and columns and edit them but i want to lock some of the cells. ( i already have code that expand the table ) how can i make some cells read only while keeping the others read write? i found this link How to make a column in QTableWidget read only? with a solution for the problem in C++, is python solution similar ?

从帖子中删除答案并粘贴为答案

Removed the answer from the post and pasted as an answer

推荐答案

我玩了一下代码并阅读了更多文档问题的答案是

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)

<小时>

解决办法是item.setFlags(QtCore.Qt.ItemIsEnabled)"这一行,你用它来设置单元格属性QtCore.Qt.ItemIsEnabled为disabled,这样就不能选择或编辑单元格了


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

根据 上的文档,您可以在运行时以这种方式更改许多其他属性http://doc.qt.io/archives/qt-4.8/qt.html 在 Qt::ItemFlag

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

正如 Sven 在对这个问题的第二个答案的评论中提到的,如果您的 QTableWidgetItem 中有静态数量的行和列,您可以使用 Qtdesigner 选择单元格的属性,如果您使用它来创建屏幕您的申请

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

这篇关于如何使 QTableWidget 中的单元格只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆