PDA

View Full Version : Custom QTableView width



Cotlone
12th July 2011, 02:08
Hey Everyone,

I have made a QTableView subclass which I wanted to resize to fit the columns within it. Basically, I cannot for the life of me figure out what I am missing here. There are an extra 11 pixels that I need to add from some header or border or something, which remains independent of the number of columns in the QTableView. There is no vertical header

I can, live the having the 11 pixels hard coded in here if I have to, but does anyone know what these pixels are accounting for?



int TableViewCopy::maximumWidthHint()
{
int colWidth = 0;
int count = model()->columnCount();

for(int i = 0; i < count; i++)
if(!isColumnHidden(i))
colWidth += columnWidth(i);

int tableWidth =
(colWidth + // total column width
verticalScrollBar()->height() + // Need room for the vertical scrollbar
11); // Why do I need 11 pixels to make it view correctly? (Windows7x64 Aero)

return tableWidth;
}


Thanks in advance for your help everyone!

- Michael

Santosh Reddy
12th July 2011, 03:30
Use horizontal header view to calculate the column width, then no need to account for scroll bar, use this way


int TableViewCopy::maximumWidthHint()
{
//QHeaderView * view = header(); // use this for QTreeView
QHeaderView * view = horizontalHeader(); // use this for QTableView

int sections = view->count();
int tableWidth = 0;

for(int i = 0; i < sections; i++)
tableWidth += view->sectionSize(i);

return tableWidth;
}

Cotlone
12th July 2011, 05:59
Thanks Santosh Reddy,

Implementing in this way appears to be the same as accessing the length property, as below.



int TableViewCopy::maximumWidthHint()
{
//QHeaderView * view = header(); // use this for QTreeView
QHeaderView * view = horizontalHeader(); // use this for QTableView

return view->length();
}


Either way this is not producing the desired result. The size of the QTableView is still smaller than the content by a different number of pixels (maybe 30ish). Is it likely that elsewhere my code is effecting the size that this QTableView should take? Or am I missing something simple here?

Santosh Reddy
12th July 2011, 15:13
I am able set the TreeView's width using this method, the width exatly fits the treeview, (I expect it to be same with tableview), may be there is somthing else in you code, which is causing this.

d_stranz
12th July 2011, 15:44
Does the column width include the pixels used for the grid and/or resize handles between columns? Does it include any padding between columns? Does it include the viewport margins? Frame width? Each of the base classes for QTableView applies some formatting to the view, so that could account for the missing pixels.

Cotlone
14th July 2011, 05:15
Santosh: Hmm, that is strange because I don't think I have really changed anything that would effect the QTableView's width.

Here is an example of how I have implemented it one of my tables.



TableViewCopy *chanDataTable = new TableViewCopy;
chanDataTable->setModel(channelData->model);
chanDataTable->setShowGrid(false);
chanDataTable->setEditTriggers(QAbstractItemView::NoEditTriggers) ; // table can not be editted
chanDataTable->resizeColumnsToContents();
chanDataTable->setSortingEnabled(true);


d_stranz: I've tried to find a relationship between the column count and the excess number of pixels, but it seems not to be existent. I had a bit of a look at ViewPorts but will probably need to investigate this further.

Thanks for the replies,
Michael

Santosh Reddy
14th July 2011, 07:11
looks like there is no simple straight forward solution for this, I am trying to create a custom QTreeView to find a solution for this, will post updates soon

Santosh Reddy
15th July 2011, 07:32
check this out

int TableViewCopy::maximumWidthHint()
{
// Add 2 Pixels to conpensate for the view port's border rectangle
// if custom view port is used then change this value accordingly
return horizontalHeader()->length() + verticalHeader()->width() + 2;
}

Cotlone
19th July 2011, 00:27
Hmm, this doesn't seem to work for me either. It is strange indeed.
I'll keep looking into it when I get a little more time on my hands.

srazi
19th July 2011, 01:39
Oh I had this problem!
This 11px comes from default layout margin, for me 'table->x()' gives this 11px and I change it by 'QLayout::setContentsMargins(int,int,int,int)'
edit: a typo in function name!

Cotlone
8th August 2011, 04:40
Hi everyone, thanks for the help.

Sorry about the latency of this response, it been pretty busy here.

Anyway, I had a look for the table->x() value and it wasn't what I was after. For me, it has a value of zero.

My search continues...

Cheers,
Michael