#include "cursortest.h" #include #include #include #include #include #include #include CursorTest::CursorTest(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags) { // Setup class. this->view = new QGraphicsView(); QHBoxLayout * layout = new QHBoxLayout(); QPushButton * button_1 = new QPushButton("cursor 1"); button_1->setCheckable(true); QPushButton * button_2 = new QPushButton("cursor 2"); button_2->setCheckable(true); QPushButton * button_3 = new QPushButton("cursor 3"); button_3->setCheckable(true); QPushButton * pan_button = new QPushButton("pan"); pan_button->setCheckable(true); QButtonGroup * group = new QButtonGroup(); group->addButton(button_1); group->addButton(button_2); group->addButton(button_3); group->addButton(pan_button); group->setExclusive(true); // Create scene and add example item. QGraphicsScene * scene = new QGraphicsScene(); this->view->setScene(scene); //// Widget in the scene. //QPushButton * proxy_button = new QPushButton("proxy"); //QGraphicsProxyWidget * proxy = new QGraphicsProxyWidget(); //proxy->setWidget(proxy_button); //scene->addItem(proxy); // Create 2D item. QGraphicsRectItem * rect = new QGraphicsRectItem(); rect->setPos(0, 50); rect->setRect(0.0f, 0.0f, 30.0f, 30.0f); rect->setFlag(QGraphicsItem::ItemIsMovable, true); scene->addItem(rect); // Signals & Slots. connect(button_1, SIGNAL(clicked()), this, SLOT(button1Clicked())); connect(button_2, SIGNAL(clicked()), this, SLOT(button2Clicked())); connect(button_3, SIGNAL(clicked()), this, SLOT(button3Clicked())); connect(pan_button, SIGNAL(toggled(bool)), this, SLOT(panButtonClicked(bool))); // Setup layout. layout->addWidget(this->view); layout->addWidget(button_1); layout->addWidget(button_2); layout->addWidget(button_3); layout->addWidget(pan_button); this->setLayout(layout); } CursorTest::~CursorTest() { } void CursorTest::button1Clicked() { //this->view->viewport()->unsetCursor(); // Solution. this->view->viewport()->setCursor(Qt::PointingHandCursor); } void CursorTest::button2Clicked() { //this->view->viewport()->unsetCursor(); // Solution. this->view->viewport()->setCursor(Qt::ForbiddenCursor); } void CursorTest::button3Clicked() { //this->view->viewport()->unsetCursor(); // Solution. this->view->viewport()->setCursor(Qt::CrossCursor); } void CursorTest::panButtonClicked(bool on) { if (on) { this->view->setDragMode(QGraphicsView::ScrollHandDrag); } else { this->view->setDragMode(QGraphicsView::NoDrag); } }