Unsolved QToolTip::showText() for tree view
-
I need to add the functionality that when user tries to enter some invalid value in a tree view
I need to show some tool tip to the user.I am trying to achieve it using
QToolTip::showText() but not able to succeed. -
@Ayush-Gupta said in QToolTip::showText() for tree view:
I am trying to achieve it using
QToolTip::showText() but not able to succeed.Well, then you should show how you're trying to use it, else we can only guess...
-
I am trying below code for my tree view
QToolTip::showText(QPoint(0,0),"Test"); -
@Ayush-Gupta This is clear, but where and when do you call showText()?
-
@Ayush-Gupta said in QToolTip::showText() for tree view:
QToolTip::showText(QPoint(0,0),"Test");
That will show a tool tip up in top corner of the screen.
Is that the issue ?
If yes, please understand that ToolTip operates in screen coordinates so
you have to translate the rect of the cell to global coordinates for the tooltip to be place correctly.Here is a sample for when you click on cell. you will will most likely use
another signal to trigger it.void MainWindow::on_treeView_clicked(const QModelIndex &index) { // get the rect of the cell from index QRect cellRect = ui->treeView->visualRect(index); // then use mapToGlobal to convert it to globals and show tool tip QToolTip::showText(ui->treeView->viewport()->mapToGlobal( cellRect.topLeft() ), "the text", this); }
-
@mrjj said in QToolTip::showText() for tree view:
cellRect
I tried with this code but still the text is showing in wrong position i.e in upper left corner of treeview
-
@Ayush-Gupta
Confirm (debug out) what thecellRect.topLeft()
is returning for you? -
it shows
QPoint(0,0) -
I would guess you then use an invalid index.
so please check your index before use with
index.isValid()If you ask visualRect(index); for an invalid index, you get invalid rect and hence 0,0
So that is most likely what you are seeing.
-
@mrjj I tried
index.isValid() it returns true for me. -
This post is deleted! -
Without your actual code and details
i cannot guess why it does not work for you
since it worked well in a sample. -
I am using QSortFilterProxyModel also in my tree view with my custom model?
Would it be a cause ? -
Yes it might
and would explain the index be valid but has no
VisualRect we can count on.I did not test it.
Please see if using
proxy->mapToSource(Index);Does return a valid cell rect.
Look the returned QRect and see if it has valid values or not. -
doing that I got
ASSERT: "!"QSortFilterProxyModel: index from wrong model passed to mapToSource -
ok, please consult the docs and make sure you are using it as intended. :)
also check the index you use is valid etc.