0

I'm working on a QTableWidget with some QLabel inside it, but I need the QLabel to line-break automatically. I've tried setWordWrap from this question:

QVBoxLayout *newL = new QVBoxLayout;
QTableWidget * tab = new QTableWidget; tab->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
newL->addWidget(tab);

tab->setRowCount(1);
tab->setColumnCount(1);

QLabel *lb = new QLabel; lb->setText("testing");
lb->setWordWrap(true);
tab->setCellWidget(0,0,lb);

QWidget *window = new QWidget;
window->setLayout(newL);
window->show();

Problems

  1. If one word is really lengthy, the wordWrap doesn't move to a new line. Instead, it just expand into outer region:
lb->setText("testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestawd");

first

  1. If there're multiple lines (more than 2), the QTableWidget doesn't create more lines, so the lower lines are not shown:
lb->setText("test\ntest\ntest\ntest\ntest\ntest");

sec

  1. Even if there're only spaces between words, the QTableWidget still doesn't expand more than 2 lines:
lb->setText("test              test        test       test           test        test       test");

third

Questions

  1. For the first problem, I'd like the text to switch to new line exactly at the point that the text go to outer region, even if there're no \n character.

  2. For the second and third problem, I'd like the QWidgetTable to provide enough space to show all the text.

Any suggestions?

1 Answers1

0

Qt wordWrap documentation: link

If this property is true then label text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all.