QTableWidget - putting multiple lines of text in one row

前端 未结 3 1705
天命终不由人
天命终不由人 2021-02-06 11:10

Is it possible to put multiple lines of text in one row of QTableWidget?

相关标签:
3条回答
  • 2021-02-06 11:35

    just make vertical headers to fit the contents then use a text as long as you want.

    QTableWidget::verticalHeader()->resizeSections(QHeaderView::ResizeToContents);
    
    0 讨论(0)
  • 2021-02-06 11:51

    I can think about 2 ways to force tablewidget to render multi-line text:

    1. Setup QStyledItemDelegate item delegate and render text yourself in the delegates paint method. Here you can find an example of you could do the same thing to a listview.

    2. Another solution would be to set QTextEdit as a cell widget to the table widget via 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, regards

    0 讨论(0)
  • 2021-02-06 11:52

    You can also simply use \n to start new line in a cell :-)

    For example:

        ui->tableWidget->insertRow(i);
    
        QTableWidgetItem *newItem = new QTableWidgetItem("Line 1 \n Line 2");
        ui->tableWidget->setItem(0,0,newItem);
    
    0 讨论(0)
提交回复
热议问题