sdzell18
27th January 2012, 17:06
Hi Everyone.
I have taken the Scribble Example from Qt 4.8 and done some modifications to it in hope to later do it to my own, similar project. For those of you not familar with the Scribble Example it lets you draw, basically like the pencil tool in windows paint.
Anyway, have used QScrollArea to make the window scrollable which works fine when the scribbleArea is at a its original scale (1.0).
I increased the scale, to enlarge the graphics using the QPainter::scale() function and change the scale to 1.25. When I scroll at this new scale the window gets lines like the widget is not re-painting properly until I use the pencil again at which point the lines go away and the scribbleArea looks normal again.
7329
Notice the lines on the top and left side of the white scribbleArea.
Here is what I have modified to achieve this:
the ScribbleArea:: paintEvent() function for scaling
void ScribbleArea::paintEvent(QPaintEvent *event)
//! [13] //! [14]
{
QPainter painter(this);
painter.scale(1.25, 1.25); //ADDED LINE FOR SCALING
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect);
}
The ScribbleArea::drawLineTo() function to make it update the whole scribble area rather than just a small section
void ScribbleArea::drawLineTo(const QPoint &endPoint)
//! [17] //! [18]
{
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
//int rad = (myPenWidth / 2) + 2;
//update(QRect(lastPoint, endPoint).normalized()
// .adjusted(-rad, -rad, +rad, +rad));
update(); //ADDED LINE
lastPoint = endPoint;
}
In the MainWindow constructor I added QScrollArea widget
MainWindow::MainWindow()
{
scribbleArea = new ScribbleArea;
scribbleArea->setMaximumHeight(500);
scribbleArea->setMaximumWidth(500);
scribbleArea->setMinimumSize(500, 500);
//setCentralWidget(scribbleArea);
scrollArea = new QScrollArea;
scrollArea->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
scrollArea->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
scrollArea->setWidget(scribbleArea);
setCentralWidget(scrollArea);
createActions();
createMenus();
setWindowTitle(tr("Scribble"));
resize(500, 500);
}
All other code is unchanged from the original example. Does anyone know what could be causing this?
Scribble example documentation here: http://developer.qt.nokia.com/doc/qt-4.8/widgets-scribble.html
I have taken the Scribble Example from Qt 4.8 and done some modifications to it in hope to later do it to my own, similar project. For those of you not familar with the Scribble Example it lets you draw, basically like the pencil tool in windows paint.
Anyway, have used QScrollArea to make the window scrollable which works fine when the scribbleArea is at a its original scale (1.0).
I increased the scale, to enlarge the graphics using the QPainter::scale() function and change the scale to 1.25. When I scroll at this new scale the window gets lines like the widget is not re-painting properly until I use the pencil again at which point the lines go away and the scribbleArea looks normal again.
7329
Notice the lines on the top and left side of the white scribbleArea.
Here is what I have modified to achieve this:
the ScribbleArea:: paintEvent() function for scaling
void ScribbleArea::paintEvent(QPaintEvent *event)
//! [13] //! [14]
{
QPainter painter(this);
painter.scale(1.25, 1.25); //ADDED LINE FOR SCALING
QRect dirtyRect = event->rect();
painter.drawImage(dirtyRect, image, dirtyRect);
}
The ScribbleArea::drawLineTo() function to make it update the whole scribble area rather than just a small section
void ScribbleArea::drawLineTo(const QPoint &endPoint)
//! [17] //! [18]
{
QPainter painter(&image);
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap,
Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
//int rad = (myPenWidth / 2) + 2;
//update(QRect(lastPoint, endPoint).normalized()
// .adjusted(-rad, -rad, +rad, +rad));
update(); //ADDED LINE
lastPoint = endPoint;
}
In the MainWindow constructor I added QScrollArea widget
MainWindow::MainWindow()
{
scribbleArea = new ScribbleArea;
scribbleArea->setMaximumHeight(500);
scribbleArea->setMaximumWidth(500);
scribbleArea->setMinimumSize(500, 500);
//setCentralWidget(scribbleArea);
scrollArea = new QScrollArea;
scrollArea->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
scrollArea->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
scrollArea->setWidget(scribbleArea);
setCentralWidget(scrollArea);
createActions();
createMenus();
setWindowTitle(tr("Scribble"));
resize(500, 500);
}
All other code is unchanged from the original example. Does anyone know what could be causing this?
Scribble example documentation here: http://developer.qt.nokia.com/doc/qt-4.8/widgets-scribble.html