Difference between revisions of "Qt/Events/Resize"

From ProgrammingExamples
< Qt
Jump to: navigation, search
(Created page with '==main.cpp== <source lang="cpp"> #include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.sho…')
(No difference)

Revision as of 09:58, 12 September 2012

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

</source>

  1. ifndef FORM_H
  2. define FORM_H
  1. include "ui_form.h"

class QGraphicsPixmapItem;

class Form : public QWidget, public Ui::Form { Q_OBJECT

public:

 Form(QWidget *parent = 0);

protected:

 void resizeEvent ( QResizeEvent * event );
 QGraphicsPixmapItem* BlackItem;

};

  1. endif

</source>

form.cpp

#include <QtGui>
 
#include <iostream>
 
#include "form.h"
 
Form::Form(QWidget *parent) : QWidget(parent)
{
  this->setupUi(this);
 
  QGraphicsScene* scene = new QGraphicsScene;
 
  //// Add a qpixmap to the scene.
  QPixmap pixmap(40, 40);
  QColor black(0,0,0);
  pixmap.fill(black);
  this->BlackItem = scene->addPixmap(pixmap);
  //this->graphicsView->fitInView(blackItem);
 
  //Add another qpixmap to the scene. Drag one of them around and watch the scene change.
  QPixmap pixmap2(30, 30);
  QColor red(255,0,0);
  pixmap2.fill(red);
  QGraphicsPixmapItem * redItem = scene->addPixmap(pixmap2);
  redItem->setFlag(QGraphicsItem::ItemIsMovable);
 
  scene->setSceneRect(pixmap.rect());
  this->graphicsView->setScene(scene);
}
 
void Form::resizeEvent ( QResizeEvent * event )
{
  std::cout << "ResizeEvent" << std::endl;
  this->graphicsView->fitInView(this->BlackItem);
}

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>312</width>
    <height>243</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <widget class="QGraphicsView" name="graphicsView">
     <property name="backgroundBrush">
      <brush brushstyle="NoBrush">
       <color alpha="255">
        <red>0</red>
        <green>0</green>
        <blue>0</blue>
       </color>
      </brush>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

CMakeLists.txt

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