PDA

View Full Version : How to word wrap text in the rows and columns of a QTableWidget?



TheIndependentAquarius
3rd March 2012, 07:01
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 get break down.
Currenty, the lengthy part of the sentence just doesn't get shown.

high_flyer
5th March 2012, 09:53
Please show how you populate an item in to the table.

TheIndependentAquarius
5th March 2012, 11:34
QTableWidget *j = new QTableWidget (1000, 5, centralWidget);
j->setColumnWidth (0, 450);
j->setColumnWidth (1, 50);
j->setColumnWidth (2, 55);
j->setColumnWidth (3, 320);
j->setColumnWidth (4, 310);
j->setHorizontalHeaderLabels (QString ("Sub-tasks;Time alloted;% complete;What's pending?;Reasons for the lag").split(";"));

QComboBox *p = new QComboBox [1000];
for (int i = 0; i < 1000; i++)
{
p[i].addItem ("20");
p[i].addItem ("30");
p[i].addItem ("40");
}
for (int i = 0; i < 1000; i++)
j->setCellWidget (i, 1, &p[i]);


QComboBox *p1 = new QComboBox [1000];
for (int i = 0; i < 1000; i++)
{
p1[i].addItem ("0");
p1[i].addItem ("10");
p1[i].addItem ("20");
p1[i].addItem ("30");
p1[i].addItem ("40");
p1[i].addItem ("50");
p1[i].addItem ("60");
p1[i].addItem ("70");
p1[i].addItem ("80");
p1[i].addItem ("90");
p1[i].addItem ("100");
}
for (int i = 0; i < 1000; i++)
j->setCellWidget (i, 2, &p1[i]);

MarekR22
5th March 2012, 12:33
WTF!
When you will have 100000 items will you create 200000 QComboBoxes? See QItemDelegate and QAbstractItemView::setItemDelegate.

@topic: I would try QTableWidgetItem::setTextAlignment with Qt::TextWordWrap (merge it with alignment flags if you need).
I know that documentation doesn't say anything about that, its only about text alignment, but I'm quite convince that it will work.
Remember also that you need enough cell hight for text warping so I expect in code some change in line hight.

wysota
5th March 2012, 13:13
ItemViews don't word-wrap elements by themselves. If you really want that, implement a custom delegate. It will probably kill your CPU with large amounts of items that change their contents but what the heck... it's doable. Not that those comboboxes have anything to do with it... They will kill your CPU on their own once you start scrolling with or without word-wrapping.

MarekR22
5th March 2012, 13:30
This is strange. It works without ANY line of code!
I just created simple project and it wraps text out of the box.
I mean I didn't wrote a single line of code I just clicked couple times in designer and filled table with data (I didn't change ANY property).

See this project 7468 when you manually extend line text is nicely warped. Tested on Linux Qt 4.8.

Maybe you should specify more precisely what is your problem.

wysota
5th March 2012, 13:37
Maybe it depends on the version of Qt? Seems to wrap on my 4.7 as well.

high_flyer
5th March 2012, 16:52
I think none of the above has to do with the problem of OP.
She was complaining about word wrapping in the view - while populating combo boxes.
Based on the documentation, pure text items should indeed be wrapped by the view its self.

@OP:
Where the text does not get wrapped?
In the combo boxes - or do you have "naked" text items in your table which do not word wrap?
Try populating only strings in to the table - with no combo boxes.
Does the text get wrapped then?

In order to reduce non related other problems as the posters above me mentioned, try working with a small table, 5x5 for example.

MarekR22
6th March 2012, 08:02
She filled combo boxes with short text (the longest is "100"), so it is not about text wrapping in combo boxes.
More probable is that she expects that line hight is growing when text is long and require warping ergo extra text lines.
I did something like that (not in the table view) and it is quite tricky.
Lets wait what author of this tread will say, our guessing leads nowhere.