How to change the position of the text of a spanned Cell in a QTableView
-
I have a QTableView with big cells (I use span). The cells are so big, that the text of them is not visible unless I reach the middle of them with the scrollbars. So I need to change the position of the text of a cell.
I think that I have 2 options here.
-
Create QLabels and set them somehow over the QTableview at certain positions (How to do that???)
-
Since I have span, can I place the cell's text not at the center of the cell, but at a certain position that I want?
Thanks in advance!
-
-
1st option doesn't look good since I want the text to be placed at the center of the visible part of a cell.
As for the 2nd can you give me some more info about how to do that?
I suppose that I have to reimplement paint(), but how could I set the text position there?And getting Labels over the QTableView is impossible?
-
if you only need static content then QAbstractItemView::setIndexWidget ( const QModelIndex & index, QWidget * widget ) would be enough ... even in this case you should use a subclass of QLabel and reimplement paintEvent in order to draw text at a certain position
-
Is there an example to help me with that?
Lets say that I only have one row that has rowSpan=4 and I want its text to be shown at the index=(2,0)
Something like that:[..........]
[..text..]
[..........]
[..........]Is there really no other way to change the current cells text position, except for setting an alignment?
-
Hi,
I think you would have to modify the option passed to your paintEvent and setup the rectangle where the text is drawn, then call the base implementation.
-
Hi,
beside setting item's widget and QStyledItemDelegate and overriding their painting methods, I think a style sheet is also an option, and by ::item and padding you can change the text's position: afaik testing ::item in designer does not work but at runtime would not be a problem as I use them a lot
with QStyledItemDelegate you can use option.rect directly in paint(), in widget's paintEvent accessing style option is trickier