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

你说的曾经没有我的故事 提交于 2019-12-07 05:05:03

问题


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.


回答1:


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.




回答2:


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

connect(
    tableWidget->horizontalHeader(),
    SIGNAL(sectionResized(int, int, int)),
    tableWidget,
    SLOT(resizeRowsToContents()));



回答3:


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.



来源:https://stackoverflow.com/questions/9544122/how-to-word-wrap-text-in-the-rows-and-columns-of-a-qtablewidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!