简体   繁体   中英

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

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

我只添加我的代码:

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

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.
tableView->resizeRowsToContents();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM