main.cpp:
int main(int argc, char *argv[])
{
GvHmiMainWindow Gvhmi;
/*~ or this way-nithya~*/
/* by with out using pointers~*/
Gvhmi.show();
return a.exec();
}
mainwindow.cpp
GvHmiMainWindow
::GvHmiMainWindow(QWidget *parent
) : QMainWindow(parent
), ui
(new Ui
::GvHmiMainWindowClass) {
ui->setupUi(this);
/*~either this way-nithya~*/
/* by using pointers~*/
viewGv = new GvHmiView();
setCentralWidget(viewGv);
QObject::connect(ui
->actionPan,
SIGNAL(triggered
( bool ) ),
this,
SLOT(slotactionPan
(bool)));
setWindowTitle(tr("Graphics View"));
}
void GvHmiMainWindow::slotactionPan(bool )
{
QMessageBox::warning(0,
"Graphics View",
"Pan action disabled");
viewGv->enablePan(false);
}
Scene.cpp:
{
setBackgroundBrush(Qt::blue);
setSceneRect(-30000, -30000, 60000, 60000);
}
GvHmiScene::~GvHmiScene()
{
}
{
qDebug("The cursor is at position %f and %f in the Scene",p.rx(),p.ry());
}
View.cpp:
GvHmiView::GvHmiView()
{
/*~ code added create scene inside view nithya */
GvHmiScene *scene = new GvHmiScene(this);
setCacheMode(CacheBackground);
// QGraphicsTextItem * item1 = scene->addText("Hello World");
// item1->setPos(0,0);
pen1.setColor(Qt::red);
item3->setPos(0,0);
// scene->addLine(0,0, 10,10, pen1);
// setAttribute (Qt::WA_SetCursor);
// setCursor(Qt::CrossCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setAlignment(Qt::AlignCenter);
// setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setScene(scene);
enablePan(true);
}
GvHmiView :: ~GvHmiView()
{
}
{
scaleView(pow((double)2, event->delta() / 240.0));
}
void GvHmiView::scaleView(qreal scaleFactor)
{
qreal factor
= matrix
().
scale(scaleFactor, scaleFactor
).
mapRect(QRectF(0,
0,
1,
1)).
width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}
[B]void GvHmiView::enableDisablePan(bool enable)
{
if(true == enable)
setDragMode(ScrollHandDrag);
else
{
setAlignment(Qt::AlignCenter);
setDragMode(NoDrag);
scale(1,1);
update();
}
}[/B]
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GvHmiMainWindow Gvhmi;
/*~ or this way-nithya~*/
/* by with out using pointers~*/
Gvhmi.show();
return a.exec();
}
mainwindow.cpp
GvHmiMainWindow::GvHmiMainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::GvHmiMainWindowClass)
{
ui->setupUi(this);
/*~either this way-nithya~*/
/* by using pointers~*/
viewGv = new GvHmiView();
setCentralWidget(viewGv);
QObject::connect(ui->actionPan,SIGNAL(triggered ( bool ) ),this,SLOT(slotactionPan(bool)));
setWindowTitle(tr("Graphics View"));
}
void GvHmiMainWindow::slotactionPan(bool )
{
QMessageBox::warning(0,"Graphics View","Pan action disabled");
viewGv->enablePan(false);
}
Scene.cpp:
GvHmiScene::GvHmiScene(QObject *parent):QGraphicsScene(parent)
{
setBackgroundBrush(Qt::blue);
setItemIndexMethod(QGraphicsScene::NoIndex);
setSceneRect(-30000, -30000, 60000, 60000);
}
GvHmiScene::~GvHmiScene()
{
}
void GvHmiScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPointF p = event->scenePos();
qDebug("The cursor is at position %f and %f in the Scene",p.rx(),p.ry());
QGraphicsScene::mouseMoveEvent(event);
}
View.cpp:
GvHmiView::GvHmiView()
{
/*~ code added create scene inside view nithya */
GvHmiScene *scene = new GvHmiScene(this);
setCacheMode(CacheBackground);
setRenderHint(QPainter::Antialiasing);
// QGraphicsTextItem * item1 = scene->addText("Hello World");
// item1->setPos(0,0);
QPen pen1;
pen1.setColor(Qt::red);
MyItem *item3 = new MyItem((QGraphicsItem *)0, scene, 0);
item3->setPos(0,0);
// scene->addLine(0,0, 10,10, pen1);
// setAttribute (Qt::WA_SetCursor);
// setCursor(Qt::CrossCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
setAlignment(Qt::AlignCenter);
setResizeAnchor(QGraphicsView::AnchorViewCenter);
// setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setScene(scene);
enablePan(true);
}
GvHmiView :: ~GvHmiView()
{
}
void GvHmiView::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, event->delta() / 240.0));
}
void GvHmiView::scaleView(qreal scaleFactor)
{
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}
[B]void GvHmiView::enableDisablePan(bool enable)
{
if(true == enable)
setDragMode(ScrollHandDrag);
else
{
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
setAlignment(Qt::AlignCenter);
setResizeAnchor(QGraphicsView::AnchorViewCenter);
setTransformationAnchor(QGraphicsView::AnchorViewCenter);
setDragMode(NoDrag);
scale(1,1);
update();
}
}[/B]
To copy to clipboard, switch view to plain text mode
Bookmarks