为QTableWidget中的单元格设置默认对齐方式 [英] Set default alignment for cells in QTableWidget

查看:1396
本文介绍了为QTableWidget中的单元格设置默认对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用以下各项设置每个项目的对齐方式:

I know you can set the alignment for each item using:

TableWidget->item(0,0)->setTextAlignment(Qt::AlignLeft);

但是我想为所有单元格设置默认对齐方式,而不必设置每当我创建一个新项目时,

However I would like to set a default alignment for all the cells in order to do not have to set it every time I create a new item. Is it possible?

推荐答案

是可以的。但是您需要了解,您不是在修改表窗口小部件的属性,而是在修改表窗口小部件项的属性。首先创建自己的项目,然后根据需要进行设置

Yes it is possible. But you need to understand you are not modifying a property of the table widget, but a property of the table widget item. First create your own item, and set it up as you want

 QTableWidgetItem * protoitem = new QTableWidgetItem();
 protoitem->setTextAlignment(Qt::AlignLeft);
 etc...

然后每次您想要创建新项目而不是使用您使用的构造函数

Then each time you want to create a new item rather than using the constructor you use

 QTableWidgetItem * newitem = protoitem->clone();
 tableWidget->setItem(0,0, newitem);

克隆(未经测试)的另一种方法是在您的tablewidget上设置原型

Another alternative to clone (untested) is to set a prototype on your tablewidget

QTableWidget::setItemPrototype ( const QTableWidgetItem * item )

如果您使用的是最后一个比较合适Ui或该项目是否可编辑。

This last one can be more appropriate if you are using a Ui or if the item is editable.

这篇关于为QTableWidget中的单元格设置默认对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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