C++ 在 QTableWidget 中设置单元格的默认对齐方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15827886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:47:53  来源:igfitidea点击:

Set default alignment for cells in QTableWidget

c++qtqtablewidgettext-alignment

提问by jsalvador

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?

但是,我想为所有单元格设置默认对齐方式,以便不必每次创建新项目时都进行设置。是否可以?

回答by UmNyobe

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);

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

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

QTableWidget::setItemPrototype ( const QTableWidgetItem * item )

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

如果您使用的是 Ui 或者项目是可编辑的,那么最后一个可能更合适。

回答by Tim Meyer

I don't think there is an existing method for this, but here's two approaches that work:

我不认为有一个现有的方法,但这里有两种有效的方法:



1.) Subclass QTableWidgetItem

1.) 子类 QTableWidgetItem

MyTableWidgetItem::MyTableWidgetItem() :
    QTableWidgetItem()
{
    setTextAlignment( Qt::AlignLeft );
}

However, this is probably a bit overkill for just a single setting + you might want to overload all four constructors of QTableWidgetItem.

但是,这对于单个设置来说可能有点矫枉过正+您可能想要重载QTableWidgetItem 的所有四个构造函数。



2.) Another approach is using a factory instead of calling new:
Note: The linked article talks about unit testing, but there are many more advantages by doing that.

2.) 另一种方法是使用工厂而不是调用 new
注意:链接的文章讨论了单元测试,但这样做有更多好处。

QTableWidgetItem* MyTableWidgetFactory::createTableWidgetItem( const QString& text ) const
{
    QTableWidgetItem* item = new QTableWidgetItem( text );
    item->setTextAlignment( Qt::AlignLeft );
    return item;
}

Then instead of

然后代替

QTableWidgetItem* myItem = new QTableWidgetItem( "foo" );
item->setTextAlignment( Qt::AlignLeft );

you can do

你可以做

QTableWidgetItem* myItem = myFactory->createTableWidgetItem( "foo" );

where myFactoryis an object of MyTableWidgetFactory.

myFactory的对象在哪里MyTableWidgetFactory