Difference between revisions of "Qt/Images/SemiTransparentPixels"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (→form.cpp) |
Daviddoria (Talk | contribs) (→form.cpp) |
||
Line 50: | Line 50: | ||
setupUi(this); | setupUi(this); | ||
− | + | QColor transparentRed(255,0,0, 0); | |
− | + | QColor semiTransparentRed(255,0,0, 127); | |
− | QColor | + | QColor opaqueRed(255,0,0, 255); |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | QImage image(100,100,QImage::Format_ARGB32); | |
− | + | image.fill(opaqueRed.rgba()); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
// Set the top left corner to totally transparent | // Set the top left corner to totally transparent | ||
− | |||
for(unsigned int i = 0; i < 50; ++i) | for(unsigned int i = 0; i < 50; ++i) | ||
{ | { | ||
for(unsigned int j = 0; j < 50; ++j) | for(unsigned int j = 0; j < 50; ++j) | ||
{ | { | ||
− | + | image.setPixel(i,j,transparentRed.rgba()); | |
} | } | ||
} | } | ||
// Set the top right corner to semi-transparent red | // Set the top right corner to semi-transparent red | ||
− | |||
for(unsigned int i = 50; i < 100; ++i) | for(unsigned int i = 50; i < 100; ++i) | ||
{ | { | ||
for(unsigned int j = 0; j < 50; ++j) | for(unsigned int j = 0; j < 50; ++j) | ||
{ | { | ||
− | + | image.setPixel(i,j,semiTransparentRed.rgba()); | |
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
+ | QPixmap pixmap = QPixmap::fromImage(image); | ||
QGraphicsScene* scene = new QGraphicsScene; | QGraphicsScene* scene = new QGraphicsScene; | ||
scene->addPixmap(pixmap); | scene->addPixmap(pixmap); |
Latest revision as of 11:41, 15 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); QColor transparentRed(255,0,0, 0); QColor semiTransparentRed(255,0,0, 127); QColor opaqueRed(255,0,0, 255); QImage image(100,100,QImage::Format_ARGB32); image.fill(opaqueRed.rgba()); // Set the top left corner to totally transparent for(unsigned int i = 0; i < 50; ++i) { for(unsigned int j = 0; j < 50; ++j) { image.setPixel(i,j,transparentRed.rgba()); } } // Set the top right corner to semi-transparent red for(unsigned int i = 50; i < 100; ++i) { for(unsigned int j = 0; j < 50; ++j) { image.setPixel(i,j,semiTransparentRed.rgba()); } } QPixmap pixmap = QPixmap::fromImage(image); 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(QPixmapOpacity) 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(QPixmapOpacity main.cpp form.h form.cpp ${MOCSrcs} ${UISrcs}) TARGET_LINK_LIBRARIES(QPixmapOpacity ${QT_LIBRARIES})