3

I'm trying to draw numbers on QRubberBand object. I have class widget with QRubberBand object rectangleRubberBand;

I can show this area etc, but I'm trying to draw on area not on the widget some numbers, for example width and height of this area. How can I do this? It's for measure purpose of charts.

ascripter
  • 5,665
  • 12
  • 45
  • 68
Shepherd
  • 33
  • 1
  • 5

1 Answers1

7

It is not drawing on rubber band but it does what you need :

void MyButton::mouseMoveEvent(QMouseEvent *event)
{
    rubberBand->setGeometry(QRect(mypoint, event->pos()).normalized());//Area Bounding
    QToolTip::showText( event->globalPos(), QString("%1,%2")
                                             .arg(rubberBand->size().width())
                                             .arg(rubberBand->size().height()),this );
}

QToolTip is shown near the cursor. It dynamically changes and shows actual information about size of rubber band.

Result (black area is a cursor) :

enter image description here

Harder solution: subclass QRubberBand and reimplement paintEvent. For example:

Header :

#ifndef RUBBERBAND_H
#define RUBBERBAND_H

#include <QRubberBand>
#include <QPaintEvent>

class RubberBand : public QRubberBand
{
    Q_OBJECT
public:
    explicit RubberBand(Shape s, QWidget * p = 0);


signals:

protected:
    void paintEvent(QPaintEvent *event);

public slots:

};

#endif // RUBBERBAND_H

cpp :

#include "rubberband.h"
#include <QPainter>
RubberBand::RubberBand(QRubberBand::Shape s, QWidget *p) :
    QRubberBand(s,p)
{
}

void RubberBand::paintEvent(QPaintEvent *event)
{
    QRubberBand::paintEvent(event);
    QPainter p(this);
    p.setPen(QPen(Qt::black,2));
    if(size().width() >10 && size().height() >10)
    {
        p.drawText(20,20,QString("%1,%2").arg(size().width()).arg(size().height()));
    }
}

Result:

enter image description here

Choose the best approach for you.

Nejat
  • 31,784
  • 12
  • 106
  • 138
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • there is any chance to see this numbers in the right middle of rubberband? not bottom right? – Shepherd Nov 25 '14 at 20:29
  • @Shepherd instead of 20,20 use center of rubber band for example, it is simple drawing, you can draw this text anywhere on widget. – Jablonski Nov 25 '14 at 20:31