Qt/Images/RubberBandSignal
From ProgrammingExamples
< Qt
main.cpp
#include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); return app.exec(); }
form.h
#ifndef FORM_H #define FORM_H #include "ui_form.h" class Form : public QWidget, private Ui::Form { Q_OBJECT public slots: public: Form(QWidget *parent = 0); }; #endif
form.cpp
#include <QtGui> #include <QImage> #include <QRubberBand> #include <QGraphicsView> #include "form.h" #include <iostream> class MyGraphicsView : public QGraphicsView { public: MyGraphicsView( QWidget * parent = 0 ) : QGraphicsView(parent){} void mouseReleaseEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); }; void MyGraphicsView::mousePressEvent(QMouseEvent *event) { QGraphicsView::mousePressEvent(event); QPoint point = event->pos(); std::cout << "mousePressEvent: " << point.x() << " " << point.y() << std::endl; } void MyGraphicsView::mouseMoveEvent(QMouseEvent *event) { QGraphicsView::mouseMoveEvent(event); QPoint point = event->pos(); std::cout << "mouseMoveEvent: " << point.x() << " " << point.y() << std::endl; } void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event) { QGraphicsView::mouseReleaseEvent(event); QPoint point = event->pos(); std::cout << "mouseReleaseEvent: " << point.x() << " " << point.y() << std::endl; } Form::Form(QWidget *parent) : QWidget(parent) { setupUi(this); MyGraphicsView* graphicsView = new MyGraphicsView(this ); graphicsView->show(); QGraphicsScene* scene = new QGraphicsScene(); graphicsView->setScene(scene); graphicsView->setDragMode(QGraphicsView::RubberBandDrag); graphicsView->show(); QPixmap pixmap(100,100); pixmap.fill(QColor(255,0,0)); QGraphicsPixmapItem* item = scene->addPixmap(pixmap); }
form.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>812</width> <height>565</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(RubberbandSignal) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_UI(UISrcs form.ui) QT4_WRAP_CPP(MOCSrcs form.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(RubberbandSignal main.cpp form.h form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(RubberbandSignal ${QT_LIBRARIES})