Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying tool tip in QTableWidget

Tags:

qt

I have a QTableWidget in my Qt application. I have QTableWidgetItem set in the cells of the table widget. I need to display a tool tip when the user hovers over the cells of the table. I have overridden the mouseMoveEvent() in my class and I am calling the method of tool tip to display it. But it does not work. I have also set the mouse tracking for the widget to true. The following is my code:

void TableDialog::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();
    QTableWidgetItem *item = ui.tableWidget->itemAt(pos);
    if(!item)
        return;
    QToolTip::showText(ui.tableWidget->viewport()->mapToGlobal(pos), "Sample tool tip using mouseMoveEvent in TableDialog class");
}

Am I missing something here? Please let me know how to display this tool tip.

like image 227
Rakesh K Avatar asked Jul 18 '12 09:07

Rakesh K


1 Answers

You just have to set a tool tip for each of your QTableWidgetItem using QTableWidgetItem::setToolTip(). It is probably a good idea to to so when you create the widget item and set its name. Then QTableWidget will show the item's tool tip when the user moves the mouse over it.

If you switch to a model based data view you will have to return the tool tip from Model::data() with role = Qt::ToolTipRole.

like image 161
Claudio Avatar answered Oct 01 '22 07:10

Claudio