Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

c++ - Set default alignment for cells in 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 prototype on your 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...