[Qt-interest] How to scroll text drawn with Qpainter in QGraphicsview
John McClurkin
jwm at nei.nih.gov
Mon Aug 24 16:15:42 CEST 2009
_h_ wrote:
> Hi John,
>
> > If the scene is larger than the view's viewport, the view displays
> scrollbars.
> As I am drawing text using QPainter it is not resizing the
> QGraphicsView, but if I use the QGraphicsTextItem the view gets resize
> accordingly.
Exactly. When you add graphics items to a graphics scene, the scene will
be resized to fit the items, but if you just draw using painter, the
scene will not be resized. Therefore, using graphcis items will cause
the view to display scrollbars as needed, but just drawing will not.
>
> > You re> placed the viewport with a QGLWidget. QGLWidgets don't resize
> when drawing goes past the borders so the view doesn't display scrollbars.
>
> No I am not using QGLWidget as viewport.
Yes, I see now that you commented out the code using QGLWidget. In the
future, you shouldn't post code that is commented out. It makes it
harder to see what is going on.
>
> > You can try manually resizing the viewport to accomodate the text..
>
> Any suggestion.
You will need to the QFontMetrics class to determine the size of the
text and resize accordingly. See the docs for QFont and QFontMetric.
>
> Thank you.
>
>
> On Fri, Aug 21, 2009 at 10:39 PM, John McClurkin <jwm at nei.nih.gov
> <mailto:jwm at nei.nih.gov>> wrote:
>
> _h_ wrote:
> > Hi,
> >
> > I have implemented QGraphicsView as follow...
> >
> > 3 class GraphicsView: public QGraphicsView
> > 4 {
> > 5 public:
> > 6 GraphicsView(QGraphicsScene *s): QGraphicsView(s) {};
> > 7
> > 8 GraphicsView(QWidget *parent): QGraphicsView(parent) {
> > 9 //setViewport(new QGLWidget);
> > 10 //setViewportUpdateMode(FullViewPortUpdate);
> > 11 //setOptimizationFlags(DontClipPainter);
> > 12 //setCacheMode(CacheNone);
> > 13 };
> > 14 ~GraphicsView(){};
> > 15 protected:
> > 16 /*void drawBackground(QPainter *painter, const QRectF
> &rect) {
> > 17 //painter->save();
> > 18 //paintGL();
> > 19 //painter->restore();
> > 20 //painter->fillRect(rect, Qt::blue);
> > 21 }*/
> > 22 void drawForeground(QPainter *painter, const QRectF
> &rect) {
> > 23 QColor c(0, 255, 127);
> > 24 painter->fillRect(rect, c);
> > 25 QFont f;
> > 26 f.setPointSize(25);
> > 27 painter->setFont(f);
> > 28 painter->drawText(0,0, "help would required to scoll
> > this text");
> > 29 }
> > 30 };
> > The problem here is that the text drawn with painter (at line 28) is
> > getting cut and QGraphicsView doesn't shows the scrollbars.
> >
> > In my application user can set any font size, and based on the
> font size
> > view should display scrollbars.
> >
> > Thank you in advance.
> >
> > Thank you.
> A QGraphicsView object is usually used to show a QGraphicsScene object.
> If the scene is larger than the view's viewport, the view displays
> scrollbars. You replaced the viewport with a QGLWidget. QGLWidgets don't
> resize when drawing goes past the borders so the view doesn't display
> scrollbars. You can try manually resizing the viewport to accomodate the
> text.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com <mailto:Qt-interest at trolltech.com>
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
More information about the Qt-interest-old
mailing list