PDA

View Full Version : how to enable hot spotting in QTableWidget.



AviMittal
25th May 2009, 10:09
Hi

I am pretty new to Qt and using Qt 4.4.3 over linux. I am building an application where i am displaying the grid data in QtableWidget.
My application demands when ever a mouse pointer comes over a cell in QTableWidget some tool tip ( for the cell) should be displayed. Each row of table is having a unique set of User record. So i can achieve the same by adding a slot on signal QTableWidget::cellEntered(int,int). But for that i need to know the cell position i.e row,col. Which QT retrieves for signal "cellEntered()". So i wanted to know is there any way to get this row-col position from this signal?

Thanks

wysota
25th May 2009, 19:59
Take a look at QTableWidgetItem::setToolTip.

AviMittal
26th May 2009, 05:52
hey thnx for rplying. But my problem is how to get the cell position from QTableWidget::cellEntered(int, int) signal?

aamer4yu
26th May 2009, 09:50
You mean cordinates ?
Have a look at QAbstractItemView::visualRect

AviMittal
26th May 2009, 12:00
not the coordinates. By cell position i mean that ( row, column) position of cell.

faldzip
26th May 2009, 12:10
hmm isn't arguments passed by cellEntered(int, int) the row and column of the entered cell?

AviMittal
26th May 2009, 13:13
I think you didnt get my problem. Let me elaborate more:

QTableWidget has one signal cellEntered(int row, int col). This signal is emitted when the mouse cursor enters a cell. The cell is specified by row and column, that qt itself finds out. But it doesn't return that row-col value to user.

So my problem is when "cellEntered(int,int) signal is emitted i need to find the cell's row-col position so that i can attach appropriate tooltip with that.

aamer4yu
26th May 2009, 13:34
How are you connecting the signal to your slot ??
From the slot you can use the row/column...
Say you have connected -

connect(m_tableWidget,SIGNAL(cellEntered(int,int)) ,this,SLOT(handleCellEntered(int,int)) );
// Then in
MyClass::handleCellEntered(int aRow,int aCol)
{
// aRow is row
// aCol is column
}

faldzip
26th May 2009, 14:37
first of aamer4yu is right. you have the row and column from the signal (so you have it in your slot - or you can have it).
Other thing is that, why can't you set the toolTip in the same place where the other item data is set?

faldzip
26th May 2009, 14:51
Isn't it the thing you want?:

wysota
26th May 2009, 21:22
I think you didnt get my problem. Let me elaborate more:

QTableWidget has one signal cellEntered(int row, int col). This signal is emitted when the mouse cursor enters a cell. The cell is specified by row and column, that qt itself finds out. But it doesn't return that row-col value to user.

So my problem is when "cellEntered(int,int) signal is emitted i need to find the cell's row-col position so that i can attach appropriate tooltip with that.

The proper way of doing it is to either set the tooltip per index (aka cell) as I showed you in the very beginning of the thread or reimplement viewportEvent(), intercept QEvent::ToolTip there and show a proper tooltip based on QAbstractItemView::indexAt(). There is also a third way that involves subclassing the model and reimplementing QAbstractItemModel::data() for Qt::ToolTipRole but this requires that you use QTableView instead of QTableWidget.

AviMittal
27th May 2009, 05:13
Isn't it the thing you want?:

Ya.....how to do this ?

faldzip
27th May 2009, 07:53
I used QTableWidgetItem::setToolTip() :]


ui->tableWidget->setRowCount(25);
ui->tableWidget->setColumnCount(10);
for (int row = 0; row < 25; ++row)
{
for (int col = 0; col < 10; ++col)
{
QTableWidgetItem *item = new QTableWidgetItem(tr("Item (%1, %2)").arg(row).arg(col));
item->setToolTip(tr("ToolTip row = %1, col = %2").arg(row).arg(col));
ui->tableWidget->setItem(row, col, item);
}
}

Somewhere in your code you are setting some text to item and maybe other things like icon. In that place you can also set the appropriate tool tip like in my code: I create item with some text (passed to the constructor) and then I set tool tip and then I add the item to the TableWidget.