Unsolved QTreeWidgetItem sizeHint returning QSize(-1,-1)
-
Ok, after having re-check the documentation and the code. That method returns the value you set for it thus since you never gave a size hint to start with you'll get the default value you are currently getting.
-
Do you know if there's any way to get the ideal width for a QTreeWidgetItem so I can tell if it doesn't fit in the column? I just want to have a popup that displays the full column contents when the contents don't fit in the column.
-
Here it says:
Sets the size hint for the tree item in the given column to be size. If no size hint is set, the item delegate will compute the size hint based on the item data.
http://doc.qt.io/qt-5/qtreewidgetitem.html#setSizeHint
Doesn't that sound like it should return an ideal sizehint based on the size of the column data or text?
-
The delegate will do the math, but it won't change the content of the model.
-
Doesn't that imply sizehint should return an ideal size for the column of a qtreewidgetitem? Which should be adequate for my purposes.
-
What is exactly your purpose ?
-
I just want to set a tooltip for qtreewidgetitems whose text does not fit in the column.
-
So you would like the column to resize so your tooltip fits in ?
-
A tooltip is where you hover the mouse over an item and a text window pops up right? So if the text doesn't fit in the column the user can hover over the qtreewidgetitem column and see the full text without resizing the column.
-
So, in fact, you would like to show the full content of a cell in a QToolTip ?
-
Exactly. And only for cells that dont fit the entire text in the column.
-
so what you want is probably to use
setData
on the widget item to set the Qt::ToolTipRole at the same value as main cell content. this will solve the " you would like to show the full content of a cell in a QToolTip" partfor the "only for cells that dont fit the entire text in the column" part you have to subclass QTreeWidget, reimplement
viewportEvent(QEvent *event)
so that if the event type is Qt::ToolTip you check if it fits usingsizeHintForIndex
if it does reject the event otherwise just call the base class implementation -
Correct me if I'm wrong but if I understand the situation correctly, you would like to resize the column to show a tooltip for something that doesn't fit in the cell within that cell. So in the end, the cell would be large enough to show the complete data, no ?
-
I interpreted as he wants something like this (cursor is in sizePolicy row, Value column)
-
VRonin you are sport on :) Thanks guys
-
A small speed bump... how do I obtain the index from the viewportEvent reimplementation? Also what's up with the treewidgetitem sizehint giving -1?
-
I tried indexAt(QCursor::pos())... The code I used is on the bottom. For the qDebug() I get this:
QVariant(Invalid) -1 100
So it looks like the same freakin problem with sizehint... I also tried this->currentIndex() instead of indexAt with the same results. Time to throw the computer out of the window!
bool TreeFileDir::viewportEvent(QEvent *event) { if (event->type() == QEvent::ToolTip) { qDebug() << indexAt(QCursor::pos()).data(Qt::DisplayRole) << sizeHintForIndex(indexAt(QCursor::pos())).width() << columnWidth(0); if (sizeHintForIndex(indexAt(QCursor::pos())).width() <= columnWidth(0)) return true; else return false; } QTreeWidget::viewportEvent(event); }