QTableWidget - Word Wrap



  • What does exactly setWordWrap(bool) do on QTableWidget? I would like to display text in a table at index(line,column) that wraps when resizing the window that depends on the space (width) available in a column. Thanks for your comments/answers.



  • from "http://doc.qt.nokia.com/4.7/qtableview.html#wordWrap-prop":http://doc.qt.nokia.com/4.7/qtableview.html#wordWrap-prop
    If this property is true then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is true by default.
    Note that even of wrapping is enabled, the cell will not be expanded to fit all text. Ellipsis will be inserted according to the current textElideMode.



  • You can use QTableView::resizeRowToContents(row) or ::resizeRowsToContents() to make the text fit the cells in your table.



  • I would like to have the text wrapped to the next item line (having the table line height increased) instead of displaying "..." when the window size is too small. I will try the QTableView::resizeRowToContents(row) option.



  • It seems that the resizeRowToContents(row) does work (somewhat).

    However when I create the row like this :

    @
    TableWidgetItem *descriptionItem = new QTableWidgetItem("String");
    descriptionItem->setFlags(Qt::ItemIsEnabled);
    setItem (row, column,descriptionItem);
    resizeRowToContents(index);
    @

    It has no effet on the row.

    I have to manually call resizeRowToContents(index) with a timer to make it work.

    Any thoughts?

    Edit: Use tag @ for code



  • In this case row=index...



  • doumdi did you ever figure this out? I am having a similar problem, the rows will resize some but not enough to show the text on a second line



  • try it to adjust one of the columns to adapt to the size of QTableWdiget :

    @
    int col =2; // chose the column 2 for adjust;
    QTableWdiget *tw = new QTableWdiget(this);
    tw->horizontalHeader()->setResizeMode(col,QHeaderView::Stretch);
    @



  • Thanks francomartins, I figured out about resizing the rows in another post but what you suggested will also be helpful with some of my other columns.



  • Although this is an old thread I think I came up with a very simple solution:
    @
    connect(
    tableWidget->horizontalHeader(),
    SIGNAL(sectionResized(int, int, int)),
    tableWidget,
    SLOT(resizeRowsToContents()));
    @
    This will adjust the word wrapping automatically every time a column resizes.


Log in to reply
 

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