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.

    1. Create QLabels and set them somehow over the QTableview at certain positions (How to do that???)

    2. 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!



  • Hi,

    I think you have two options:

    1.) give the cell an alignment
    2.) write a QStyledItemDelegate derived class



  • 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?



  • I don't know how the widgets are positioned, but adding widgets is possible.
    Yes, you have to reimplement paint of the delegate, how to find the position, I don't know. I never did such stuff.



  • 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



  • If you reimplement paint, you can even use a QWidget subclass as painting there is the same effort, you only need an additional property text.



  • 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?



  • Labels worked for me, but I think just changing the position of the cells' text would be a much more efficient implementation.


  • Lifetime Qt Champion

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.