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

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

问题描述

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

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 )

最后一个可能更适合,如果你使用a Ui或如果项目是可编辑的。

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

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

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