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.
scribbleBad.JPG
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
//! [13] //! [14]
{
painter.scale(1.25, 1.25); //ADDED LINE FOR SCALING
QRect dirtyRect
= event
->rect
();
painter.drawImage(dirtyRect, image, dirtyRect);
}
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);
}
To copy to clipboard, switch view to plain text mode
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]
{
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;
}
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;
}
To copy to clipboard, switch view to plain text mode
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->setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
scrollArea->setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOn );
scrollArea->setWidget(scribbleArea);
setCentralWidget(scrollArea);
createActions();
createMenus();
setWindowTitle(tr("Scribble"));
resize(500, 500);
}
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);
}
To copy to clipboard, switch view to plain text mode
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...-scribble.html
Bookmarks