QTableView with multiline cell

2019-05-31 02:28发布

How can I create a QTableView multiline cell?

I'm filling the table using the code bellow. But Whem GetDescription() returns a long string, the content is terminated with ...

There is some way to automatic break the line?

QStandardItemModel * model = new QStandardItemModel(logos.size(), 2, this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Nome")));
model->setHorizontalHeaderItem(2, new QStandardItem(QString("Descrição")));

int row = 0;
foreach(Item * item, items)
{
    QStandardItem* check = new QStandardItem(true);
    check->setCheckable(true);
    model->setItem(row, 0, check);

    QStandardItem *nameItem = new QStandardItem(QString(item->GetName()));
    nameItem->setEditable(false);
    model->setItem(row, 1, nameItem);

    QStandardItem *descriptionItem = new QStandardItem(item->GetDescription());
    descriptionItem->setEditable(false);
    descriptionItem->setToolTip(logo->GetDescription());
    model->setItem(row, 2, descriptionItem);
    row++;
}

ui->tableView->setModel(model);
ui->tableView->resizeColumnToContents(0);
ui->tableView->resizeColumnToContents(1);
ui->tableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->tableView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

4条回答
来,给爷笑一个
2楼-- · 2019-05-31 03:05

As far as I know, the only way to implement multiline text drawing in cells is implementing own delegate.

You can derive from QItemDelegate.

You'll have to

  • implement own sizeHint function, based on QFontMetrics
  • and override drawDisplay function to actually display text. You can use QPainter::drawText to display multiline text. So, you don't have to care about drawing focus and selection rectangles and own painting function will be simple.
查看更多
可以哭但决不认输i
3楼-- · 2019-05-31 03:06

tableView->resizeRowsToContents();

查看更多
Lonely孤独者°
4楼-- · 2019-05-31 03:14

I only add to my code:

        ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
查看更多
甜甜的少女心
5楼-- · 2019-05-31 03:25

I think word wrapping is what you're looking for. Make sure that you've enabled wordwrap for the QTableView, then manually resize the rows to fit their contents. That will replace the ellipse with the text.

As you mentioned in your answer, you can set the QHeaderView to resize to contents automatically, but if you do a lot of adding and removing this will slow things down. I prefer to manually resize with a large addition/subtraction, particularly since the user might find it annoying to be unable to resize it themselves.

Here's some example code that enables word wrap, sets the ellipse to appear in the middle (my preference), and then manually resizes the row height to fit the contents at word boundaries:

ui->tableView->setWordWrap(true);
ui->tableView->setTextElideMode(Qt::ElideMiddle);
ui->tableView->resizeRowsToContents();
查看更多
登录 后发表回答