[Qt-interest] Ruler widget not repainting
Anton Chernov
mechernov at gmail.com
Mon Mar 21 21:54:20 CET 2011
Hi!
Very nervous bug, try already hard for very long time - i am making a
ruler widget with showing current cursor position feature and a
offseted origin, just like the standard way. The problem is that the
current mouse position guids are drawn only when the mouse is over the
ruler widget and not when the button is over the widget that i want to
"measure" with my ruler.
At first this is my Ruler:
class Ruler : public QWidget {
Q_OBJECT
...
public:
void setCursorPos( const QPoint& cursorPos )
{
if( mCursorPos == cursorPos ) { return; }
mCursorPos = cursorPos;
update();
}
bool eventFilter( QObject* obj, QEvent* event )
{
if( event->type() == QEvent::MouseMove )
{
QMouseEvent* mevent = static_cast( event );
QWidget* widget = qobject_cast( obj );
if( widget ) { setCursorPos( mevent->pos() ); }
/* handle mouse events further */
return false;
}
return QWidget::eventFilter( obj, event );
}
protected:
void mouseMoveEvent( QMouseEvent* event )
{
setCursorPos( event->pos() );
if( !( event->buttons() | Qt::RightButton ) ) { return; }
QDrag* drag = new QDrag( this );
QMimeData* mimeData = new QMimeData;
drag->setMimeData( mimeData );
drag->start( Qt::MoveAction );
}
void paintEvent( QPaintEvent* )
{
...
/* mouse guids */ drawMouseGuid( &painter );
}
void drawMouseGuid( QPainter* painter )
{
QPoint start, end;
switch( mOrientation )
{
case Horizontal: { start.rx() = mCursorPos.x(); start.ry() = 0;
end.rx() = mCursorPos.x(); end.ry() = rect().height(); } break;
case Vertical: { start.rx() = 0; start.ry() = mCursorPos.y();
end.rx() = rect().width(); end.ry() = mCursorPos.y(); } break;
}
painter->save();
QPen pen;
pen.setStyle( Qt::DotLine ); pen.setColor( Qt::black );
pen.setWidth( 1 ); painter->setPen( pen ); painter->setOpacity( 0.4 );
painter->drawLine( start, end );
painter->restore();
}
I have tried 2 ways:
1. in the widget that holds my ruler and my measurable area i
implemented the mouseMoveEvent
void Preview::mouseMoveEvent( QMouseEvent* event )
{
ui->hRuler->setCursorPos( ui->hRuler->mapFromGlobal( event->globalPos() ) );
QWidget::mouseMoveEvent( event );
}
2. installed on the widget that i want to measure a event filter
ui->previewArea->installEventFilter( ui->hRuler );
Neither off them worked. The positions of the cursor seem to be valid
and updated, but the ruler widget itself is repainted only when the
mouse is over it or by reactivating the application (alt+tab,
alt+tab). Please help! :)
PS: mouseTracking is enabled for all widgets.
More information about the Qt-interest-old
mailing list