Nominate our 2022 Qt Champions!

How to wrap a QTableWidget?

  • Hi all,

    I have a QTableWidget with three columns and any number of rows, e.g. four rows:

    index(0,0) index(0,1) index(0,2)
    index(1,0) index(1,1) index(1,2)
    index(2,0) index(2,1) index(2,2)
    index(3,0) index(3,1) index(3,2)

    Unfortunatelly the table is nested in a widget which height is less than the height of the table and I have to avoid scrollbars.
    Therefore I need to wrap the table after few rows and paint the rest of the table next to it:

    index(0,0) index(0,1) index(0,2) index(2,0) index(2,1) index(2,2)
    index(1,0) index(1,1) index(1,2) index(3,0) index(3,1) index(3,2)

    How can I do that?
    It is possible to move cells in the paintEvent()?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    I haven't done it so it's just a wild idea but you could add a proxy model between your table view and model. That proxy model would then be in charge to redo the geometry (more or less columns and rows so nothing directly gui) of your model to accomodate the new size of your QTableView

    Hope it helps

  • Thanks for your reply!

    Actually I do not want to create a table with 6 columns and 2 rows.

    The rows are of different heights. Creating a table with 6 columns will result in same height for row0 and row2 ... and row1 and row3.

    Is there a possibility to move the position of the cell-rect? Maybe somewhere in the paint() method ... So I would set the position of row2 and row3 next to the other rows.

  • To change the position of a cell I have overwritten the paint() method of the ItemDelegate class:

    @class MyDelegate: public QStyledItemDelegate
    MyDelegate( QObject* object = 0 )
    : QStyledItemDelegate(object)

    void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
    QStyleOptionViewItem opt = option;
    initStyleOption( &opt, index );

      // move all rows except the first one
      if( index.row() != 1 )
         opt.rect.translate( 50,50);
      QStyledItemDelegate::paint( painter, opt , index );


    I initialized the ItemDelegate in the constructor of my QTableWidget:

    @setItemDelegate( new MyDelegate() );@

    But nothing happens ...

  • Lifetime Qt Champion

    The delegate is responsible to paint the cell but not its position.

    Have a look at the "TiledItemView": from Mark Summerfield (the book is worth buying)
    It should help you get started

Log in to reply