[Qt-interest] Ruler widget not repainting

Anton Chernov mechernov at gmail.com
Tue Mar 22 09:10:59 CET 2011


This is from qt help:

The common use of QPainter is inside a widget's paint event: Construct
and customize (e.g. set the pen or the brush) the painter. Then draw.
Remember to destroy the QPainter object after drawing. For example:
void SimpleExampleWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::blue);
    painter.setFont(QFont("Arial", 30));
    painter.drawText(rect(), Qt::AlignCenter, "Qt");
}

QPaintEvent has only the information which region should be repainted.

Public Functions
QPaintEvent ( const QRegion & paintRegion )
QPaintEvent ( const QRect & paintRect )
const QRect &	rect () const
const QRegion &	region () const

2011/3/21 Malyushytsky, Alex <alex at wai.com>
>
> Check which painter you are using.
> I would expect painter pointer paintEvent receive as a aparameter to be passed to drawMouseGuid. Like below:
>
> void paintEvent( QPaintEvent*  painter)
>  {
>    ...
>     drawMouseGuid( &painter );
>  }
>
> You seems create painter inside paint Event  instead.
>
> Alex
>
> -----Original Message-----
> From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of Anton Chernov
> Sent: Monday, March 21, 2011 1:54 PM
> To: qt-interest at qt.nokia.com
> Subject: [Qt-interest] Ruler widget not repainting
>
> 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.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
> ---------------------------------------------------------------------------------------------------
> Weidlinger Associates, Inc. made the following annotations.
>
> “This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”
>
> “Please consider our environment before printing this email.”
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest



More information about the Qt-interest-old mailing list