PDA

View Full Version : QTableView: horizontal header "resize to contents", want resize to edit widget



hml
1st May 2010, 22:13
Hello,

I have a class derived from QTableView, the constructor of which is:

my_table_view::my_table_view(QWidget* parent)
: QTableView(parent)
{
setSelectionMode(QAbstractItemView::SingleSelectio n);
setModel(new my_model(this));
setItemDelegate(new my_delegate(this));
verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
}

For column 1, my_model returns a QString

QString pslist;
for (...) {
pslist += (i->first.c_str());
pslist += '=';
pslist += (i->second.c_str());
pslist += '\n';
}
return pslist;
which yields the cell containing a list of strings.
That cell/columnl is one with the most height out of the 3 columns in the table.

Double clicking on that cell, my delegate creates an editor:


QWidget* my_delegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
const int row = index.row();
if (row<0)
return 0;

switch(index.column()) {
///
case 1: {
QWidget* widget = ;////
widget->setAutoFillBackground(true);
return widget;
}
///
default:
return 0;
};
}

This returned widget is trivial. It has a QGridLayout taking care of 5 QLabels in front of 5 QLineEdits.
When I double click on the cell, the widget is created and returned.

The tableview ctor set the resizing mode of the horizontal header to resize to contents.
Because the row was sized according to the display widget (coming from QString), the edit widget (with the QLabels,QLineEdits) doesn't display nicely.

Can I resize the row of the table view after the delegate has created the widget to edit the cell ,so that the row holds enough space to display the edit widget properly?

Regards,

norobro
2nd May 2010, 00:03
Untested but here's something you might try:
Emit a signal in createEditor(). Connect it to a slot in my_table_view that resizes the row and/or column so the editor widget fits.
Then to change things back emit a signal in setModelData() that is connected to another my_table_view slot that calls resizeRowsToContents() and/or resizeColumnsToContents().