Victor
Victor

Reputation: 8480

QTableView with multiline cell

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);

Upvotes: 2

Views: 15248

Answers (4)

mabg
mabg

Reputation: 2092

tableView->resizeRowsToContents();

Upvotes: 0

Victor
Victor

Reputation: 8480

I only add to my code:

ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

Upvotes: 6

Phlucious
Phlucious

Reputation: 3843

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();

Upvotes: 11

Lol4t0
Lol4t0

Reputation: 12547

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.

Upvotes: 3

Related Questions