QTableWidget - placing multiple lines of text on one line

Is it possible to put several lines of text in one line of QTableWidget?

+3
source share
3 answers

I can think of 2 ways to get tablewidget to display multi-line text:

  • Set the QStyledItemDelegate delegate and create the text yourself in the delegate's paint method. Here you can find an example of how you could do the same with a list.

  • Another solution would be to install QTextEdit as a cell widget in a table widget via the setCellWidget method .

Below is an example for # 2:

QTableWidget* tableWidget = new QTableWidget(3, 2, this);
tableWidget->setGeometry(20, 20, 300, 300);

for (int row = 0; row<3; row++)
{
    for (int column=0; column<2; column++)
    {
        QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1 long long long long long long text").arg((row+1)*(column+1)));
        tableWidget->setItem(row, column, newItem);
    }
    QTextEdit *edit = new QTextEdit();
    edit->setText(tableWidget->item(row, 0)->text());
    tableWidget->setCellWidget(row, 0, edit);
}

hope this helps, believes

+2
source

\n : -)

:

    ui->tableWidget->insertRow(i);

    QTableWidgetItem *newItem = new QTableWidgetItem("Line 1 \n Line 2");
    ui->tableWidget->setItem(0,0,newItem);
+4

just create vertical headers to match the content, and then use the text as much as you want.

QTableWidget::verticalHeader()->resizeSections(QHeaderView::ResizeToContents);
0
source

All Articles