PDA

View Full Version : QTableView sizeHint() issues



akos.maroy
11th July 2008, 12:34
I'm working off the itemviews/pixelator example that is provided by Qt, and I'm having trouble making the subclassed QTableView appear in it's proper size. My impression was that changing line 144 of mainwindow.cpp to adjustSize(); would make everything fit right, but it doesn't. This is despite the delegate returns a 12x12 pixel size for its sizeHint(), and the model returns proper column and row counts.

I searched on the issue, and saw that one would need to implement the sizeHint() method for the subclasses QTableView object. Fair enough, I did so. While the situation is better now, for some reason the QTableView widget is shown smaller that it should be, and is thus surrounded by scrollbars still. for example when the delegate returns 12x12 for sizeHint(), and the model reports 40 rows and 40 columns, the overloaded sizeHint() returns 480x480 pixels appropriately - but the widget itself is actually crancked into what seems to be about 480x455 (including the scrollbars), or 460x440 visible inside the scrollbars.

what is going wrong here? what else should be done, besides overriding sizeHint()?

jpn
11th July 2008, 12:43
What if you return the same size in minimumSizeHint() instead? Oh, and you might also have to add frames to that calculation.

akos.maroy
11th July 2008, 12:55
What if you return the same size in minimumSizeHint() instead? Oh, and you might also have to add frames to that calculation.

it seems minimumSizeHint() has the same result

as for frames - how do I get frame sizes to add to the calculation?

also, my table view doesn't have anything but its contents really - the real size of the widget is nothing but the sizes of the cells added together. if I manually resize the window, this is what I get as well (e.g. the widget is really 480x480 for the above example of 12x12 pixel cells and 40x40 cells overall)

yuriry
20th September 2008, 22:42
I also needed to layout table views without displaying their scrollbars. sizeHint() and minimumSizeHint() did the work for me. Below is the code of a sub-class of the QTableView:



MyTableView::MyTableView(QWidget* parent)
: QTableView(parent)
{
}

QSize MyTableView::sizeHint() const
{
return minimumSizeHint();
}

QSize MyTableView::minimumSizeHint() const
{
QAbstractItemModel* m = model();

int colCount = m->columnCount();
int w = 0;
for(int i=0; i<colCount; ++i) {
w += columnWidth(i);
}

int rowCount = m->rowCount();
int h = 0;
for(int i=0; i<rowCount; ++i) {
h += rowHeight(i);
}

int doubleFrame = 2 * frameWidth();

w += verticalHeader()->width() + doubleFrame;
h += horizontalHeader()->height() + doubleFrame;

return QSize(w, h);
}


Here is alternative code for minimumSizeHint() which also does the work:



QSize MyTableView::minimumSizeHint() const
{
QHeaderView* vHeader = verticalHeader();
QHeaderView* hHeader = horizontalHeader();

int doubleFrame = 2 * frameWidth();

int w = hHeader->length() + vHeader->width() + doubleFrame;
int h = vHeader->length() + hHeader->height() + doubleFrame;

return QSize(w, h);
}


I like this second implementation better because it is shorter but I'm not sure if it'll work in all cases. I'm actually not sure if the first implementation will also work in all cases, so if someone with more Qt experience could give an advice I would really appreciate it.

Thank you in advance,
Yuri