6

I tried:

    QTableWidget *j = new QTableWidget (10000, 5, centralWidget);
    j->setColumnWidth (0, 500);
    j->setColumnWidth (1, 30);
    j->setColumnWidth (2, 30);
    j->setColumnWidth (3, 320);
    j->setColumnWidth (4, 310);

    j->setWordWrap (true);

Also tried resizeColumnsToContents and resizeRowsToContents, but failed.

If the text is longer than the set width, I want the sentence to break down.
Currenty, the lengthy part of the sentence just doesn't get shown.

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • Have you tried setting the row height explicitly? If that doesn't work , you might have to implement a subclass of QStyledItemDelegate to be able to change sizeHint(). Do you want to be able to edit the cell contents? – kossmoboleat Mar 03 '12 at 15:41

3 Answers3

9

setWordWrap defines the behaviour of the text, without altering column size. If you need to keep column width fixed, call resizeRowsToContents after the insertion of the item to the cell (I assume you're adding text to the table via QTableWidgetItem).

Please notice that if any of the words contained in the item are wider than column size, text will be elided from that point on (by default you will see ellipses: ...). If you want to change such behaviour you need to reimplement item's painting function or stretch your columns.

Masci
  • 5,864
  • 1
  • 26
  • 21
8

This will adjust the word wrapping automatically every time a column resizes:

connect(
    tableWidget->horizontalHeader(),
    SIGNAL(sectionResized(int, int, int)),
    tableWidget,
    SLOT(resizeRowsToContents()));
Robert Wloch
  • 243
  • 4
  • 9
0

As mentioned in the question comment, setting the row size explicitly to some value seems to work:

 tableWidget->resizeRowsToContents();
 tableWidget->verticalHeader()->setDefaultSectionSize(50);

I note that for my code, I did not have to explicitly call setWordWrap in order to have cell contents be word wrapped.

user7797
  • 337
  • 3
  • 8
  • The documentation says that by default `wordWrap` is `true`. So yes, you do not have to call it. However, I do not see anything wrapping. It may be elided if the width of the column is too small, but I would like wrapping as in height of the cell growing and the text appearing on any number of lines as required... Is that possible? – Alexis Wilke Apr 26 '15 at 05:45