Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to word wrap text in the rows and columns of a QTableWidget?

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.

like image 420
Aquarius_Girl Avatar asked Mar 03 '12 06:03

Aquarius_Girl


People also ask

How do you wrap text in a project?

Right-click the heading of the column you want to wrap, and click Wrap Text. Project wraps text in all cells of the column if it is longer than the column width and has at least one space. It doesn't recognize manual line breaks. To turn text wrapping off, right-click the column heading and click Wrap Text again.

How do you wrap text in two columns?

To wrap text in a column in a table, right-click to view the column dropdown menu and select the "Wrap text" option: You can wrap text in multiple columns at once by selecting multiple columns, right-clicking, and then selecting the option to wrap text. If you want to wrap your column headers check out this article.


1 Answers

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.

like image 159
Masci Avatar answered Sep 18 '22 00:09

Masci