本文介绍了QTableWidget - 将多行文本放在一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在QTableWidget的一行中放置多行文本?
Is it possible to put multiple lines of text in one row of QTableWidget?
推荐答案
强制tablewidget呈现多行文本:
I can think about 2 ways to force tablewidget to render multi-line text:
-
设置项委托并在代理绘制方法中自己渲染文本。 ,您可以找到您的示例
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.
另一个解决方案是通过方法。
Another solution would be to set QTextEdit as a cell widget to the table widget via setCellWidget method.
以下是#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);
}
希望这有助于您,
这篇关于QTableWidget - 将多行文本放在一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!