PDA

View Full Version : How to compute blank space between text of a table Item and cell edge?



srazi
6th November 2010, 22:06
Please see attached image,
I want to compute blank space between left corner of text ( QFontMetrics::boundingRect (text).left() ) and left edge.
what is that?
Is there a function?
http://i56.tinypic.com/2z52713.jpg

srazi
7th November 2010, 08:12
I found "QTableView::horizontalOffset()"!
Is it the right function?
but it's protected! Should I subclass QTableWidget? :confused:
sorry for my english. :)

tbscope
7th November 2010, 08:29
Try the header view offset. I think, if I understand the documentation correctly, the items use the same offset as the one in the header view (both horizontal and vertical)

srazi
7th November 2010, 09:16
Try the header view offset. I think, if I understand the documentation correctly, the items use the same offset as the one in the header view (both horizontal and vertical)
header view offset ( horizontal and vertical ) returns zero!

tbscope
7th November 2010, 09:19
Then the documentation is not clear.

Check the source code of QTableView.

srazi
7th November 2010, 11:17
Then the documentation is not clear.

Check the source code of QTableView.
the "QTableView::horizontalOffset()" return "QHeaderView::Offset()", I think I need another thing!
I read Qt sorce code more and I found in "qcommonstyle.cpp":


void QCommonStyle::drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *widget) const
{
...
case CE_ItemViewItem:
...
QRect textRect = subElementRect(SE_ItemViewItemText, vopt, widget);
...
// draw the text
...
d->viewItemDrawText(p, vopt, textRect);
...
}


I try "subElementRect" (for item in first column) but:


subElementTextRect = tableStyle->subElementRect(QStyle::SE_ItemViewItemText , &option);
int textLeft=subElementTextRect .x(); //textLeft=0 <first row>
subElementFocusRectangle = tableStyle->subElementRect(QStyle::SE_ItemViewItemFocusRect, &option);
int focusLeft=subElementFocusRectangle.x();//focusLeft=1 <first row>

textLeft is less than focusLeft!!!
We know textLeft should be bigger than focusLeft because focus rectangle doesn't cross text. what is wrong with above code?

srazi
7th November 2010, 14:17
I think I found what I want, in the body of "QItemDelegate::drawDisplay" I found:


const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, 0, widget)+1;

I use it in reimplementation of "QItemDelegate::paint" in this way:


int textMargin = 0;
if (tableStyle)
{
textMargin = tableStyle->pixelMetric(QStyle::PM_FocusFrameHMargin, &option) + 1;
}

maybe it be the best solution. :)