Difference between revisions of "Qt/Images/Pixmap"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (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.show(); ret…') |
(No difference)
|
Latest revision as of 11:17, 4 November 2011
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 <QGraphicsScene> #include <QGraphicsView> #include <QPixmap> #include "form.h" Form::Form(QWidget *parent) : QWidget(parent) { setupUi(this); //QPixmap pixmap; //pixmap.load("test.jpg"); // Manual: //pixmap = pixmap.scaled(20,20); // Works // QPixmap pixmap(20,20); // QColor black(0,0,0); // pixmap.fill(black); //QPixmap pixmap; // Doesn't work (probably because the default size is (0,0) so scaling 0 to anything doesn't make sense) QPixmap pixmap(1,1); // Works pixmap = pixmap.scaled(20,20); QColor black(0,0,0); pixmap.fill(black); QGraphicsScene* scene = new QGraphicsScene; scene->addPixmap(pixmap); this->graphicsView->setScene(scene); }
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>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <widget class="QGraphicsView" name="graphicsView"> <property name="geometry"> <rect> <x>60</x> <y>50</y> <width>256</width> <height>192</height> </rect> </property> </widget> </widget> <resources/> <connections/> </ui>
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(QPixmap) 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(QPixmap main.cpp form.h form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(QPixmap ${QT_LIBRARIES})