#include <QtWidgets>
#include <QGraphicsView>
#include <QWheelEvent>
#include <QApplication>
{
public:
GraphicsView
( QWidget* parent
= 0 ) ,m_scale(1.0)
{
}
protected:
{
if(ev->delta() > 0) {
m_scale *= 1.5;
qDebug() << "+1.5" << m_scale;
scale(1.5, 1.5);
} else {
m_scale /= 1.5;
qDebug() << "-1.5" << m_scale;
scale(1/1.5, 1/1.5);
}
}
private:
qreal m_scale;
};
int main(int argc, char **argv)
{
if (!fileName.isEmpty())
{
if( fi.exists())
{
bool err = px.load( fi.absoluteFilePath());
if (!err)
return 0;
}
}
scene.addPixmap(px);
pen.setCosmetic( true );
line->setPen( pen );
scene.addItem( line );
GraphicsView view;
view.setScene(&scene);
view.show();
return app.exec();
}
#include <QtWidgets>
#include <QGraphicsView>
#include <QWheelEvent>
#include <QApplication>
class GraphicsView : public QGraphicsView
{
public:
GraphicsView ( QWidget* parent = 0 )
:QGraphicsView( parent )
,m_scale(1.0)
{
setDragMode( QGraphicsView::ScrollHandDrag );
}
protected:
void wheelEvent(QWheelEvent *ev)
{
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setResizeAnchor(QGraphicsView::AnchorUnderMouse);
if(ev->delta() > 0) {
m_scale *= 1.5;
qDebug() << "+1.5" << m_scale;
scale(1.5, 1.5);
} else {
m_scale /= 1.5;
qDebug() << "-1.5" << m_scale;
scale(1/1.5, 1/1.5);
}
}
private:
qreal m_scale;
};
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
QPixmap px;
QString fileName = QFileDialog::getOpenFileName(0, QString("Open File"), QDir::currentPath(), "Image Files (*.png *.jpg *.bmp)");
if (!fileName.isEmpty())
{
QFileInfo fi( fileName );
if( fi.exists())
{
bool err = px.load( fi.absoluteFilePath());
if (!err)
return 0;
}
}
scene.addPixmap(px);
QGraphicsLineItem* line = new QGraphicsLineItem( px.width()-10,px.height()-1,px.width(),px.height()-1 );
QPen pen(Qt::red, 1.0 );
pen.setCosmetic( true );
line->setPen( pen );
scene.addItem( line );
GraphicsView view;
view.setScene(&scene);
view.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks