PDA

View Full Version : Tooltips for QListViewItems (QT3)?



gadnio
13th March 2006, 11:05
Hi, is there any way to implement tooltips for QListViewItems (hints, etc)?
In general, I want to do the following thing:
I have a QListView with columns that cannot fit in the viewport and thus a horizontal scroll bar is displayed. When the user hovers an item, I want to be able to show all the text in the column in a tooltip if and only if that text is not shown in its whole (there are parts of the text that have not been displayed because the scrolling area moved in such a way that it hides parts of the text)

wysota
13th March 2006, 12:39
Use QListView::itemAt() locate an item under cursor and QToolTip for handling "tipping". See the example that comes with Qt (http://doc.trolltech.com/3.3/tooltip-example.html) for more info.

gadnio
13th March 2006, 16:30
OK, and can someone tell me why this approach does not work?

(header file)


#ifndef N_LIST_VIEW_TOOLTIP__H
#define N_LIST_VIEW_TOOLTIP__H

#include <qtooltip.h>
#include <qlistview.h>
#include <qguardedptr.h>

class n_list_view_tooltip: public QToolTip
{
private:
QGuardedPtr< QListView > f_list_view;
protected:
virtual void maybeTip( const QPoint& p );
public:
n_list_view_tooltip( QListView * widget, QToolTipGroup * group = 0 );
~n_list_view_tooltip();
QListView * list_view() const;
void set_list_view( QListView * value );
};//n_list_view_tooltip

#endif


(source file)


#include "n_list_view_tooltip.h"

#include <qheader.h>

n_list_view_tooltip::n_list_view_tooltip( QListView * widget, QToolTipGroup * group ):
QToolTip( widget, group ), f_list_view( widget )
{
}

n_list_view_tooltip::~ n_list_view_tooltip( )
{
if ( f_list_view )
remove( f_list_view );
}

QListView * n_list_view_tooltip::list_view( ) const
{
return f_list_view;
}

void n_list_view_tooltip::set_list_view( QListView * value )
{
f_list_view = value;
}

void n_list_view_tooltip::maybeTip( const QPoint & p )
{
if ( !f_list_view )
{
qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): f_list_view is NULL", p.x(), p.y() );
return;
}

if ( ! f_list_view->viewport()->rect().contains( p ) )
{
qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): viewport does not contain p!", p.x(), p.y() );
return;
}

QPoint point( f_list_view->viewport()->mapFromParent( p ) );
QListViewItem * item = f_list_view->itemAt( point );
if ( !item )
{
qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): item is NULL", p.x(), p.y() );
return;
}
QString s = item->text( f_list_view->header()->sectionAt( point.x() ) );

QRect r( f_list_view->itemRect( item ) );
r.setTop( std::max( r.top(), 0 ) );
r.setLeft( std::max( r.left(), 0 ) );
r.setHeight( std::max( f_list_view->viewport()->height(), r.height() ) );
r.setWidth( std::min( f_list_view->viewport()->width(), r.width() ) );
point = f_list_view->viewport()->mapToParent( r.topLeft() );
r.setTopLeft( point );
r.setHeight( std::max( f_list_view->height(), r.height() ) );
r.setWidth( std::min( f_list_view->width(), r.width() ) );

qDebug( "void n_list_view_tooltip::maybeTip( %d, %d ): rect = (%d,%d,%d,%d); tip='%s'", p.x(), p.y(), r.top(), r.left(), r.width(), r.height(), ( const char * ) s.utf8() );
tip( r, s );
}


In my QListView I create an instance of this class and delete it after destruction of the class.

wysota
13th March 2006, 17:02
Does maybeTip get called at all? You might have some bugs in those series of if statements.

Could you show how you create that QToolTip derived object?