PDA

View Full Version : QToolTip with an item of a QVBoxLayout



gilou
28th March 2010, 18:03
Hello to all of you.
In the process of porting an application from Qt3 to Qt4, I met a serious problem concerning the use of a QToolTip object associated to an item of a QVBoxLayout made of a QLineEdit widget and a QTreeWidget ( where I want to see a list of files ( extension, name & path ))
I built a small piece of code extracted from the real application and aiming that point.
What happens ? : the creation of the main window with a menubar, a toolbar & a status bar seems to be correct; the creation of the QVBoxLayout and the insertion of the 2 items ( QLineEdit & QTreeWidget ) too : I can use both of them ( entering text in QLineEdit, selecting ( by clicking or double-clicking an item in QTreeWidget )) and connection between signal & slots seems to work fine.
But the QToolTip object attached to QTreeWidget doesn't give me what I expect : it appears only when the mouse pointer enters LineEdit area ( the first item of QTreeWidget is tooltip'ed ) or a little bellow ( the the second item is shown ). It seems to me that the QToolTip "migrates" from QTreeWidget to QVBoxLayout. By inserting debug code in the QEvent::ToolTip I wrote to intercept the QEvent, I can see the position of the pointer when event occurs : the coordinates are ALWAYS relative to the QVBoxLayout and NEVER to the QTreeWidget. Of course I cannot use these coordinates.
I was unable to get the position of each QLayoutItem in the layout ( I always got the position of the outer layout ! ) or their size or their geometry ( same kind of result ! )
So I think that something is wrong in the way I create either the layout or insert the items ( QLineEdit & QTreeWidget ) : some attributes or parameter missing or even the order in which these operations are performed ( or ... I dont know what ).

Following is a part of the code I use to setup the objects :


SearchList::SearchList(int nSearchCol, QWidget* pParent, const char* szName) :
QWidget(pParent),
m_nSearchCol(nSearchCol)
{
Q_UNUSED(szName);

// Create the child widgets
QVBoxLayout *pLayout = new QVBoxLayout;
m_pEdit = new SearchLineEdit();
m_pList = new QTreeWidget();
// Set up the tooltip generator
m_pList->setToolTip(QString());
m_pToolTip = QString();
pLayout->addWidget(m_pEdit);
pLayout->addWidget(m_pList);
setLayout(pLayout);

to build the QTreeWidget

FileList::FileList(QWidget* pParent, const char* szName) :
SearchList(1, pParent, szName),
m_sRoot("/")
{
QSize headerItemSize;
QHeaderView *pHeaderView;
// Set the list's columns
QTreeWidgetItem *headerItem = new QTreeWidgetItem();

headerItem->setText(0, "");
headerItem->setText(1, "File");
headerItem->setText(2, "Path");
m_pList->setHeaderItem(headerItem);
m_pList->setAllColumnsShowFocus(true);
m_pList->setRootIsDecorated(false);
m_pList->setAlternatingRowColors(true);
m_pList->setMouseTracking(true);
m_pList->setColumnCount(3);
pHeaderView = m_pList->header();
headerItemSize = pHeaderView->size();
}
to handle QEvent::ToolYip event


bool SearchList::event(QEvent *e)
{
if (e->type() == QEvent::ToolTip) {
QHelpEvent *hEvent = static_cast<QHelpEvent *>(e);
QPoint pt = hEvent->pos();
QPoint globalPt = hEvent->globalPos();

fprintf(stderr, "SearchList::event: pos: X: %d Y: %d globalPos: X: %d Y: %d\n", pt.x(), pt.y(), globalPt.x(), globalPt.y());

// QTreeWidgetItem* pItem = m_pList->itemAt(pt - QPoint(0, 60));
QTreeWidgetItem* pItem = m_pList->itemAt(pt);
if ( pItem ) {
QString sTip;
getTip(pItem, sTip);
QToolTip::showText(hEvent->globalPos(), sTip);
} else {
QToolTip::hideText();
e->ignore();
}
return true;
}
return QWidget::event(e);
}

If I change

QTreeWidgetItem* pItem = m_pList->itemAt(pt);
into

QTreeWidgetItem* pItem = m_pList->itemAt(pt - QPoint(0,60));
the behaviour is as expected. The "60" value is approximatly the height of QLineEdit + the height of QTreeWidget header. So it's obviously a matter of relative position of the pointer to the wrong object.

Here are the classes I use ( only the first line of each of them )


class SearchLineEdit : public QLineEdit
{
Q_OBJECT
public:
SearchLineEdit(QWidget* pParent = 0) : QLineEdit(pParent) {};

class SearchList : public QWidget
{
Q_OBJECT

public:
SearchList(int nSearchCol, QWidget* pParent = 0, const char* szName = 0);

class FileList : public SearchList, public FileListTarget
{
Q_OBJECT

public:
FileList(QWidget* pParent = 0, const char* szName = 0);
~FileList();


Of course I can send the whole code if needed.
Any help on that point, any suggestion, will very much appreciated.
And above all, many thanks for reading that post and for any answer.