You have the wrong coordinates, probably. I don;t know if the coordinates you are receiving are relative to the QTableWidget, to the QMainWindow, or to the screen. When you click somewhere, do the coordinates make sense? If you click in the top left cell, do you get coordinates that are reasonable for being in that cell? If not, you might have to do some mapping between screen, table, or main window coordinates.
When I use x = 0 and y = 0, the QString is positioned on the top left corner of the top left cell of the table. The problem is, the QString corresponds to another cell. That's what I don't understand. Why isn't it positioned on the top left of the corresponding cell?
Let's take an example: I have 5 columns and 5 rows. Column 2 has a custom QItemDelegate that should display a QString on the top left corner of each cell in Column 2.
So I subclass QItemDelgate (or QStyledItemDelegate), reimplement the paint(..) function and use QPainter::drawText(QRect(0,0,50,20), "TestString"). So a string "TestString" should be positioned on each top left corner of the cells in column 2. BUT instead, all 5 TestStrings are positioned on the top left corner of the top left cell (index 0,0).
I don't see any sense in this behaviour. Why can data that is painted in the paint() function that is called for each table cell leave the cell frontiers?
So you aren't using a delegate for this? You're just painting from an override of QTableWidget:aintEvent()?
No, I do subclass, that is my code:
Table.h
{
Q_OBJECT
private:
public:
~test();
private:
void buildWindow();
};
class testDelegate : public QStyledItemDelegate
{
public:
testDelegate
(QWidget *widgetParent
= 0);
~testDelegate();
virtual void paint
(QPainter*,
const QStyleOptionViewItem
&,
const QModelIndex
&) const;
};
class test : public QWidget
{
Q_OBJECT
private:
QVBoxLayout *layoutMain;
QTableWidget *tableTest;
public:
test(QWidget *parent = 0);
~test();
private:
void buildWindow();
};
class testDelegate : public QStyledItemDelegate
{
public:
testDelegate(QWidget *widgetParent = 0);
~testDelegate();
virtual void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;
};
To copy to clipboard, switch view to plain text mode
Table.cpp
void test::buildWindow()
{
this->tableTest->setRowCount(5);
this->tableTest->setColumnCount(5);
QHeaderView *headerV
= this
->tableTest
->verticalHeader
();
QHeaderView *headerH
= this
->tableTest
->horizontalHeader
();
testDelegate *delegateTest = new testDelegate(this);
this->tableTest->setItemDelegateForColumn(2, delegateTest);
this->layoutMain->addWidget(this->tableTest);
this->setLayout(this->layoutMain);
this->setWindowTitle("TestTable");
this->setMinimumSize(500,300);
return;
}
{
pPainter
->drawText
(QRect(0,
0,
50,
20),
"TestString");
QStyledItemDelegate::paint(pPainter, soviOption, miIndex);
return;
}
void test::buildWindow()
{
this->layoutMain = new QVBoxLayout;
this->tableTest = new QTableWidget;
this->tableTest->setRowCount(5);
this->tableTest->setColumnCount(5);
QHeaderView *headerV = this->tableTest->verticalHeader();
QHeaderView *headerH = this->tableTest->horizontalHeader();
headerV->setSectionResizeMode(QHeaderView::Stretch);
headerH->setSectionResizeMode(QHeaderView::Stretch);
testDelegate *delegateTest = new testDelegate(this);
this->tableTest->setItemDelegateForColumn(2, delegateTest);
this->layoutMain->addWidget(this->tableTest);
this->setLayout(this->layoutMain);
this->setWindowTitle("TestTable");
this->setMinimumSize(500,300);
return;
}
void testDelegate::paint(QPainter *pPainter, const QStyleOptionViewItem &soviOption, const QModelIndex &miIndex) const
{
pPainter->drawText(QRect(0,0,50,20), "TestString");
QStyledItemDelegate::paint(pPainter, soviOption, miIndex);
return;
}
To copy to clipboard, switch view to plain text mode
Unfortunatelly, all TestString strings are positioned on the top left of the table instead of the top left corners of each cell in column 2... This behaviour is strange and makes window resize handling much more complicated, because the QItemDelegate doesn't store the data into the cells but lays the data over the cells without careing about the cell borders... While changing the window size, the table cells adjust but the data doesn't .... this shouldn't go this way, should it??
EDIT:
The most interesting and confusing fact is, that when I do it like anda_skoa mentioned and use the QRect() that is given when calling the option.rect member for positioning, then everything works as I expected! The strings are applied correctly on each top left corner of the cells in column 2 AND they correctly move while resizing the window! But where is the difference to my manually created QRect() ?? I mean, I could also check in a if-else condition which cell of column 2 has index and create a corresponding QRect() to position the strings. But they will not move correctly while resizing the window, they are not stored into the cell but only hover it... Can you tell me why?
Bookmarks