Qt/Images/RubberBand

From ProgrammingExamples
< Qt
Jump to: navigation, search

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:
    Form(QWidget *parent = 0);
 
};
 
#endif

form.cpp

#include <QtGui>
#include <QImage>
#include <QRubberBand>
 
#include "form.h"
 
#include <iostream>
 
Form::Form(QWidget *parent)
    : QWidget(parent)
{
  setupUi(this);
 
  QGraphicsScene* scene = new QGraphicsScene();
  //QRubberBand* rubberBand = new QRubberBand(QRubberBand::Rectangle, scene);
  //QRubberBand* rubberBand = new QRubberBand(QRubberBand::Rectangle);
  QRubberBand* rubberBand = new QRubberBand(QRubberBand::Rectangle, this->graphicsView);
 
  QPalette palette;
  palette.setBrush(QPalette::Foreground, QBrush(Qt::green));
  palette.setBrush(QPalette::Base, QBrush(Qt::red));
 
  rubberBand->setPalette(palette);
  rubberBand->resize(30, 30);
 
  //scene->addItem(rubberBand);
 
  this->graphicsView->setScene(scene);
  //this->graphicsView->setDragMode(QGraphicsView::RubberBandDrag);
  this->graphicsView->show();
}

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 class="QPushButton" name="btnOpen">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>10</y>
     <width>121</width>
     <height>41</height>
    </rect>
   </property>
   <property name="text">
    <string>Open Image</string>
   </property>
  </widget>
  <widget class="QGraphicsView" name="graphicsView">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>80</y>
     <width>521</width>
     <height>361</height>
    </rect>
   </property>
   <property name="verticalScrollBarPolicy">
    <enum>Qt::ScrollBarAlwaysOff</enum>
   </property>
   <property name="horizontalScrollBarPolicy">
    <enum>Qt::ScrollBarAlwaysOff</enum>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
PROJECT(Rubberband)
 
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(Rubberband main.cpp form.h form.cpp ${MOCSrcs} ${UISrcs})
TARGET_LINK_LIBRARIES(Rubberband ${QT_LIBRARIES})