Created
August 11, 2012 10:01
-
-
Save Mashpy/3323319 to your computer and use it in GitHub Desktop.
2dPaintingNativeQt by Mashpy Says
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HEADERS += \ | |
helper.h \ | |
widget.h \ | |
window.h | |
SOURCES += \ | |
helper.cpp \ | |
widget.cpp \ | |
window.cpp \ | |
main.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui> | |
#include "helper.h" | |
//! [0] | |
Helper::Helper() | |
{ | |
QLinearGradient gradient(QPointF(50, -20), QPointF(80, 20)); | |
// ছবিতে ভাল করে খেয়াল করলে দেখবেন ভেতরের অংশটুকু লাল আর একটি নির্দিষ্ট অংশ পর এটা সবুজ। | |
// QLinearGradient ক্লাস শুরু বিন্দু এবং শেষ বিন্দু নির্দেশ করে দিচ্ছে যে কোন অংশটুকু লাল হবে | |
// আর কোনগুলো অন্যকালার। | |
gradient.setColorAt(0.0, Qt::red); | |
gradient.setColorAt(1.0, QColor(0xa6, 0xce, 0x39)); | |
// আপনি ইচ্ছে করলে এটাকে ভিন্ন কোন রঙ যেমন নীল রঙ এভাবে দিতে পারেন- | |
// gradient.setColorAt(1.0, Qt:: blue)); | |
background = QBrush(QColor(64, 32, 64)); | |
// এটা পেছনের ব্যাকগ্রাউন্ড কালার নির্ধারন করছে। | |
circleBrush = QBrush(gradient); | |
circlePen = QPen(Qt::black); | |
circlePen.setWidth(1); | |
textPen = QPen(Qt::white); | |
// Qt লেখা টেক্সটি কাল বর্ণের হবে এবং ফন্টের আকার ৫০ হবে। | |
textFont.setPixelSize(50); | |
} | |
//! [0] | |
//! [1] | |
void Helper::paint(QPainter *painter, QPaintEvent *event, int elapsed) | |
{ | |
painter->fillRect(event->rect(), background); | |
painter->translate(100, 100); | |
//! [1] | |
// এখন আমরা যে উইন্ডোটি বানাচ্ছি সেটা (২০০,২০০) পিক্সেলের। আপনি যদি এর আকার বাড়ান | |
// তাহলে উপরের ট্রান্সলেট মানটিও এমনভাবে বসাতে হবে যাতে পজিশনিং ঠিকভাবে হয়। | |
//! [2] | |
painter->save(); | |
painter->setBrush(circleBrush); | |
painter->setPen(circlePen); | |
painter->rotate(elapsed * 0.030); | |
// এই মানটি যদি আপনি পরিবর্তন করেন তাহলে বলগুলো অসম্ভব দ্রুত গতিতে ঘুরবে। | |
qreal r = elapsed/1000.0; | |
int n = 30; | |
// ৩০ টি বল নিয়ে আমরা এখন কাজ করছি। | |
for (int i = 0; i < n; ++i) { | |
painter->rotate(30); | |
qreal radius = 0 + 120.0*((i+r)/n); | |
qreal circleRadius = 1 + ((i+r)/n)*20; | |
painter->drawEllipse(QRectF(radius, -circleRadius, | |
circleRadius*2, circleRadius*2)); | |
// ক্রমবৃদ্ধিকারে ঘুরানোর জন্য এখানেই আসল কাজটি সম্পন্ন হয়। কোডগুলো ভাল করে খেয়াল করুন। | |
} | |
painter->restore(); | |
//! [2] | |
//! [3] | |
painter->setPen(textPen); | |
painter->setFont(textFont); | |
painter->drawText(QRect(-50, -50, 100, 100), Qt::AlignCenter, "Qt"); | |
// Qt লেখাটি কোথায় হবে তার পজিশন নির্দেশ করছে। | |
} | |
//! [3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HELPER_H | |
#define HELPER_H | |
#include <QBrush> | |
#include <QFont> | |
#include <QPen> | |
QT_BEGIN_NAMESPACE | |
class QPainter; | |
class QPaintEvent; | |
QT_END_NAMESPACE | |
//! [0] | |
class Helper | |
{ | |
public: | |
Helper(); | |
public: | |
void paint(QPainter *painter, QPaintEvent *event, int elapsed); | |
private: | |
QBrush background; | |
QBrush circleBrush; | |
QFont textFont; | |
QPen circlePen; | |
QPen textPen; | |
}; | |
//! [0] | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include "window.h" | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc, argv); | |
Window window; | |
window.show(); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui> | |
#include "widget.h" | |
#include "helper.h" | |
//! [0] | |
Widget::Widget(Helper *helper, QWidget *parent) | |
: QWidget(parent), helper(helper) | |
{ | |
elapsed = 0; | |
setFixedSize(200, 200); | |
} | |
//! [0] | |
//! [1] | |
void Widget::animate() | |
{ | |
elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000; | |
repaint(); | |
} | |
//! [1] | |
//! [2] | |
void Widget::paintEvent(QPaintEvent *event) | |
{ | |
QPainter painter; | |
painter.begin(this); | |
painter.setRenderHint(QPainter::Antialiasing); | |
helper->paint(&painter, event, elapsed); | |
painter.end(); | |
} | |
//! [2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WIDGET_H | |
#define WIDGET_H | |
#include <QWidget> | |
//! [0] | |
class Helper; | |
QT_BEGIN_NAMESPACE | |
class QPaintEvent; | |
QT_END_NAMESPACE | |
class Widget : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
Widget(Helper *helper, QWidget *parent); | |
public slots: | |
void animate(); | |
protected: | |
void paintEvent(QPaintEvent *event); | |
private: | |
Helper *helper; | |
int elapsed; | |
}; | |
//! [0] | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui> | |
#include "widget.h" | |
#include "window.h" | |
//! [0] | |
Window::Window() | |
: QWidget() | |
{ | |
Widget *native = new Widget(&helper, this); | |
QLabel *nativeLabel = new QLabel(tr("Native")); | |
nativeLabel->setAlignment(Qt::AlignHCenter); | |
QGridLayout *layout = new QGridLayout; | |
layout->addWidget(native, 0, 0); | |
layout->addWidget(nativeLabel, 1, 0); | |
setLayout(layout); | |
QTimer *timer = new QTimer(this); | |
connect(timer, SIGNAL(timeout()), native, SLOT(animate())); | |
timer->start(50); | |
setWindowTitle(tr("2D Painting on Native Widgets")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WINDOW_H | |
#define WINDOW_H | |
#include <QWidget> | |
#include "helper.h" | |
QT_BEGIN_NAMESPACE | |
class QLabel; | |
class QWidget; | |
QT_END_NAMESPACE | |
//! [0] | |
class Window : public QWidget | |
{ | |
Q_OBJECT | |
public: | |
Window(); | |
private: | |
Helper helper; | |
}; | |
//! [0] | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment