Solved TableView Spinbox HAlignment
-
Hi,
I have a table view to which I provide values as doubles and I get the following.
I would like the values centered in the column. So I created a new delegate and attached it to the TableView.
Now when I edit the values they are centered in the Spinbox, but when they are not edited they are not.
Why are the values only centered in edit mode and how can I keep the values centered?
-
Hi
I have written the following:
model->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);and it works.
Thank you
-
@Bert59 please provide a MWE(minimal workable example)
It seems that your delegate only sets the editor alignment but not the unedited text, a possible solution is to override the initStyleOption method:
class StyledItemDelegate: public QStyledItemDelegate{ public: using QStyledItemDelegate::QStyledItemDelegate; protected: void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{ QStyledItemDelegate::initStyleOption(option, index); option->displayAlignment = Qt::AlignCenter; } };
-
@Bert59 said in TableView Spinbox HAlignment:
but when they are not edited they are not.
You should take a look at the Qt::TextAlignmentRole
-
Hi
I have written the following:
model->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);and it works.
Thank you
-
I just have an additionnal question.
How can I make the Spinbox to be displayed even if not in edition mode?
-
I found openPersistenEditor which seems to work.
Maybe there are othe solutions?
-
@Bert59 said in TableView Spinbox HAlignment:
How can I make the Spinbox to be displayed even if not in edition mode?
Through a
QStyledItemDelegate
.