PyQt QTableView设置水平和垂直标题标签

时间:2021-07-26 23:04:07

using QTableWidget i can do

使用QTableWidget,我可以做到

table = QTableWidget()
table.setHorizontalHeaderLabels(QString("Name;Age;Sex;Add").split(";"))
table.horizontalHeaderItem().setTextAlignment(Qt.AlignHCenter)

how can i do same with QTableView ??

我怎么能用QTableView做同样的事情?

1 个解决方案

#1


13  

The Table/Tree/List Widgets are item-based. The Table/Tree/List Views are View/Model based (sometimes known as MVC, for Model/View/Controller). In the Model/View system, the data is set and manipulated on the model and the view just displays it. To use a View widget, you also have to create a model class. In many cases, people will create their own and subclass from QAbstractItemModel, but you don't have to. Qt provides a non-abstract model you can use with all the view classes - QStandardItemModel.

表/树/列表小组件是基于项目的。表/树/列表视图是基于视图/模型的(有时称为MV​​C,用于模型/视图/控制器)。在模型/视图系统中,数据在模型上设置和操作,视图只显示它。要使用View小部件,还必须创建一个模型类。在许多情况下,人们将从QAbstractItemModel创建自己的子类,但您不必这样做。 Qt提供了一个非抽象模型,可以与所有视图类一起使用 - QStandardItemModel。

model = QStandardItemModel()
model.setHorizontalHeaderLabels(['Name', 'Age', 'Sex', 'Add'])
table = QTableView()
table.setModel(model)

There are a couple ways you can do alignment. Alignment data is actually supported in the model, but the header view lets you set a default (I'm guessing it uses that if the alignment data isn't set in the model)

有几种方法可以做对齐。模型中实际支持对齐数据,但是标题视图允许您设置默认值(我猜它如果未在模型中设置对齐数据则使用它)

header = table.horizontalHeader()
header.setDefaultAlignment(Qt.AlignHCenter)

To get even more control, you can set the alignment data directly on the model.

要获得更多控制,您可以直接在模型上设置对齐数据。

# Sets different alignment data just on the first column
model.setHeaderData(0, Qt.Horizontal, Qt.AlignJustify, Qt.TextAlignmentRole)

But the power of the View/Model system is that the view can choose to display that data from the model any way it wants to. If you wanted to create your own custom view, you could have absolute control over how the text in each column is aligned and displayed.

但View / Model系统的强大之处在于视图可以选择以任何方式显示模型中的数据。如果要创建自己的自定义视图,则可以绝对控制每列中文本的对齐和显示方式。

#1


13  

The Table/Tree/List Widgets are item-based. The Table/Tree/List Views are View/Model based (sometimes known as MVC, for Model/View/Controller). In the Model/View system, the data is set and manipulated on the model and the view just displays it. To use a View widget, you also have to create a model class. In many cases, people will create their own and subclass from QAbstractItemModel, but you don't have to. Qt provides a non-abstract model you can use with all the view classes - QStandardItemModel.

表/树/列表小组件是基于项目的。表/树/列表视图是基于视图/模型的(有时称为MV​​C,用于模型/视图/控制器)。在模型/视图系统中,数据在模型上设置和操作,视图只显示它。要使用View小部件,还必须创建一个模型类。在许多情况下,人们将从QAbstractItemModel创建自己的子类,但您不必这样做。 Qt提供了一个非抽象模型,可以与所有视图类一起使用 - QStandardItemModel。

model = QStandardItemModel()
model.setHorizontalHeaderLabels(['Name', 'Age', 'Sex', 'Add'])
table = QTableView()
table.setModel(model)

There are a couple ways you can do alignment. Alignment data is actually supported in the model, but the header view lets you set a default (I'm guessing it uses that if the alignment data isn't set in the model)

有几种方法可以做对齐。模型中实际支持对齐数据,但是标题视图允许您设置默认值(我猜它如果未在模型中设置对齐数据则使用它)

header = table.horizontalHeader()
header.setDefaultAlignment(Qt.AlignHCenter)

To get even more control, you can set the alignment data directly on the model.

要获得更多控制,您可以直接在模型上设置对齐数据。

# Sets different alignment data just on the first column
model.setHeaderData(0, Qt.Horizontal, Qt.AlignJustify, Qt.TextAlignmentRole)

But the power of the View/Model system is that the view can choose to display that data from the model any way it wants to. If you wanted to create your own custom view, you could have absolute control over how the text in each column is aligned and displayed.

但View / Model系统的强大之处在于视图可以选择以任何方式显示模型中的数据。如果要创建自己的自定义视图,则可以绝对控制每列中文本的对齐和显示方式。