QTableView::resizeRowsToContents
-
Hi,
I am new to Qt programming, so this might be a stupid question, but I simply cannot get it to work.
I have a QTableSubview which contains data from a custom model derived from QAbstractTableModel. The data in the last column might be empty or contain a text with (possibly multiple) line feeds.
When I try to use resizeRowsToContents it seems only the first column of every row is considered for the new row height, meaning I always get a one-text-line-high row.
I created a short example (using QtCreator's Gui Application) showing the problem.
The Model:
@
class MyModel : public QAbstractTableModel
{
Q_OBJECT
public:
int rowCount (const QModelIndex& /* parent / = QModelIndex()) const {return 3;}
int columnCount(const QModelIndex& / parent */ = QModelIndex()) const {return 5;}QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const { if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); if (index.row() > index.column()) return QVariant("One line"); else if (index.row() == index.column()) return QVariant("Two\nlines"); else return QVariant("Three\nlines\nhere"); };
};
@In the Dialog's constructor:
@
MyModel* aModel = new MyModel();// Create the table for the contents QTableView* aTableView = new QTableView(this); aTableView->setModel(aModel); aTableView->resizeRowsToContents(); QDialogButtonBox* aButtonBox = new QDialogButtonBox(Qt::Vertical); aButtonBox->addButton(QDialogButtonBox::Ok); connect(aButtonBox, SIGNAL(accepted()), this, SLOT(accept())); QHBoxLayout* aMainLayout = new QHBoxLayout(); aMainLayout->addWidget(aTableView); aMainLayout->addWidget(aButtonBox); setLayout(aMainLayout);
@
What I would expect is, that all rows are high enough to show three lines of text, what I get is a first row, that shows two lines of text and two more lines that show only one line.
Would you be able to help me out on this?
Thanks in advance,
Martin -
I have a similar problem with row heights not being adjusted properly. I have set a timer at 1Hz to call resizeRowToContents(index) or resizeRowsToContents() periodically and this works. If you find how to solve your problem, I would be interested to know.
-
I guess you are just supposed to call resizeRowsToContents() every time you change the item height? Can anyone confirm that this is correct?
-
resizeRowsToContents does it one time.
If you want to do it automatically, you have to do:@
pView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
@written from brain, no check done
-
Gerolf that worked perfectly for me thanks!
does anyone know if its possible to keep the text in multiple lines when editing it? When I double click a cell to edit it the text all goes into 1 line so some of the text isn't visible or the edit box expands over the cells to the right. Its not a big deal if this isn't do able or if it is really complicated though.
-
By default, a QLineEdit is used as the editor for an item view for QStrings. You'll have to convince the item view to use a QTextEdit instead. There are multiple ways to do that, but one is to provide your own [[doc:QItemDelegateFactory]] to the delate that handles your view.
-
Perfect,
[quote author="Gerolf" date="1337851944"]resizeRowsToContents does it one time.
If you want to do it automatically, you have to do:@
pView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
@written from brain, no check done[/quote]
works for me as well when my QSQlQueryModel updates !
I use it together with pView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); to resize my columns automatically as well. -
QTimer::singleShot(1, ui->tableView, SLOT(resizeRowsToContents()));