From 8bb265fd94f72dd7d298072c46b5a098a0e9bbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D1=80=D0=BE=D1=81?= =?UTF-8?q?=D0=BA=D1=83=D1=80=D0=BD=D1=91=D0=B2?= Date: Fri, 1 Jan 2021 05:35:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80.=20=D0=98=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B0=20=D1=81=D1=82=D1=80=D1=83?= =?UTF-8?q?=D0=BA=D1=82=D1=83=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/example.pro | 25 +++++++++++++++++++++++++ example/main.cpp | 39 +++++++++++++++++++++++++++++++++++++++ main.cpp | 21 +++++++-------------- qtSimpleGraph.pro | 3 +-- qtsgraph.cpp | 41 +++++++++++++++++++++++++++++------------ qtsgraph.h | 19 +++++++++++++------ qtsgraph.ui | 22 ---------------------- 7 files changed, 114 insertions(+), 56 deletions(-) create mode 100644 example/example.pro create mode 100644 example/main.cpp delete mode 100644 qtsgraph.ui diff --git a/example/example.pro b/example/example.pro new file mode 100644 index 0000000..2c265c1 --- /dev/null +++ b/example/example.pro @@ -0,0 +1,25 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + ../qtsgraph.cpp + +HEADERS += \ + ../qtsgraph.h + +FORMS += + +TRANSLATIONS += + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/example/main.cpp b/example/main.cpp new file mode 100644 index 0000000..5aac539 --- /dev/null +++ b/example/main.cpp @@ -0,0 +1,39 @@ +#include "../qtsgraph.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + /* + * Задаётся размер и положение окна + * (int w = 640, int h = 480, int x = -1, int y = -1, QWidget *parent = nullptr) + * В случае отрицательного значения x или y, окно создаётся в центре экрана. + */ + QTSGraph w(1024, 768); + + w.show(); + return a.exec(); +} + +void QTSGraph::PaintBox() +{ + // Начало рисования + + SetColor(0x00AAAAAA); + Line(120, 120, 135, 260); + SetWidth(5); + SetColor(clBlue); + Line(110, 110, 125, 250); + PutPixel(100, 100, 0x00FF0000, 10); + Delay(2000); + PutPixel(300, 100); + int x = 1; + while(!MouseClicked() && x < 1024) + { + PutPixel(x, 50, 0x555555+x*16, 5); + x += 1; + Delay(1); + } + + // Конец рисования +} diff --git a/main.cpp b/main.cpp index fb448db..66c7a20 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,4 @@ #include "qtsgraph.h" -#include int main(int argc, char *argv[]) { @@ -8,9 +7,9 @@ int main(int argc, char *argv[]) /* * Задаётся размер и положение окна * (int w = 640, int h = 480, int x = -1, int y = -1, QWidget *parent = nullptr) - * В случае отрицательнго значения x или y, окно создаётся в центре экрана. + * В случае отрицательного значения x или y, окно создаётся в центре экрана. */ - QTSGraph w(1024,768); + QTSGraph w(800, 600); w.show(); return a.exec(); @@ -18,18 +17,12 @@ int main(int argc, char *argv[]) void QTSGraph::PaintBox() { - // Тут рисовать + // Начало рисования - PutPixel(100,100); - Delay(2000); - PutPixel(300,100); - int x=1; - while(!MouseClicked() && x < 1024) - { - PutPixel(x,50); - x+=1; - Delay(200); - } + SetColor(clGreen); + Line(0, 0, 800, 600); + SetColor(0xFF0000); + Line(800, 0, 0, 600); // Конец рисования } diff --git a/qtSimpleGraph.pro b/qtSimpleGraph.pro index ef5a993..fd94d6c 100644 --- a/qtSimpleGraph.pro +++ b/qtSimpleGraph.pro @@ -15,8 +15,7 @@ SOURCES += \ HEADERS += \ qtsgraph.h -FORMS += \ - qtsgraph.ui +FORMS += TRANSLATIONS += \ qtSimpleGraph_ru_RU.ts diff --git a/qtsgraph.cpp b/qtsgraph.cpp index 9a37cf8..09d8a68 100644 --- a/qtsgraph.cpp +++ b/qtsgraph.cpp @@ -1,18 +1,15 @@ #include "qtsgraph.h" -#include "ui_qtsgraph.h" void QTSGraph::Delay(int ms) { QTime dieTime= QTime::currentTime().addMSecs(ms); while (QTime::currentTime() < dieTime) - QCoreApplication::processEvents(QEventLoop::AllEvents, 100); + QCoreApplication::processEvents(QEventLoop::AllEvents, 50); } QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) : QMainWindow(parent) - , ui(new Ui::QTSGraph) { - ui->setupUi(this); if(x < 0 || y < 0) { QDesktopWidget desktop; @@ -23,8 +20,10 @@ QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) } this->setGeometry(x, y, w, h); this->setWindowTitle("Рисунок"); - m_Pixmap = QPixmap(w, h); - m_Pixmap.fill(Qt::white); + Canvas = QPixmap(w, h); + Canvas.fill(Qt::white); + QRgb DefaultColor = 0x00000000; + Pen = QPen(QBrush(QColor(DefaultColor)), 1); StartTimer = new QTimer(); connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer())); StartTimer->start(500); @@ -32,7 +31,7 @@ QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) QTSGraph::~QTSGraph() { - delete ui; + // Деструктор } bool QTSGraph::MouseClicked() @@ -42,11 +41,29 @@ bool QTSGraph::MouseClicked() return m; } -void QTSGraph::PutPixel(int x, int y, Qt::GlobalColor c) +void QTSGraph::PutPixel(int x, int y, QRgb c, int PenWidth) { - QPainter painter(&m_Pixmap); - painter.setPen(QPen(QBrush(QColor(c)), 5 )); - painter.drawPoint( x, y ); + QPainter painter(&Canvas); + painter.setPen(QPen(QBrush(QColor(c)), PenWidth)); + painter.drawPoint(x, y); + update(); +} + +void QTSGraph::SetColor(QRgb c) +{ + Pen.setColor(QColor(c)); +} + +void QTSGraph::SetWidth(int PenWidth) +{ + Pen.setWidth(PenWidth); +} + +void QTSGraph::Line(int x1, int y1, int x2, int y2) +{ + QPainter painter(&Canvas); + painter.setPen(Pen); + painter.drawLine(x1, y1, x2, y2); update(); } @@ -59,7 +76,7 @@ void QTSGraph::slotStartTimer() void QTSGraph::paintEvent(QPaintEvent *event) { QPainter p(this); - p.drawPixmap(0, 0,m_Pixmap); + p.drawPixmap(0, 0, Canvas); } void QTSGraph::mousePressEvent(QMouseEvent *event) diff --git a/qtsgraph.h b/qtsgraph.h index 82f1803..6189d33 100644 --- a/qtsgraph.h +++ b/qtsgraph.h @@ -1,3 +1,4 @@ +#include #include #include #include @@ -9,9 +10,11 @@ #include #include -QT_BEGIN_NAMESPACE -namespace Ui { class QTSGraph; } -QT_END_NAMESPACE +#define clRed 0x00FF0000 +#define clGreen 0x0000FF00 +#define clBlue 0x000000FF +#define clBlack 0x00000000 +#define clWhite 0x00FFFFFF class QTSGraph : public QMainWindow { @@ -20,19 +23,23 @@ class QTSGraph : public QMainWindow public: QTSGraph(int w = 640, int h = 480, int x = -1, int y = -1, QWidget *parent = nullptr); ~QTSGraph(); - void PutPixel(int x, int y, Qt::GlobalColor c = Qt::red); + void Delay(int ms = 1000); + void Line(int x1, int y1, int x2, int y2); bool MouseClicked(); + void PutPixel(int x, int y, QRgb c = 0x00000000, int PenWidth = 1); + void SetColor(QRgb c); + void SetWidth(int PenWidth); private slots: void slotStartTimer(); private: - Ui::QTSGraph *ui; - QPixmap m_Pixmap; + QPixmap Canvas; QTimer *StartTimer; bool EventMouseClicked = false; void PaintBox(); + QPen Pen; protected: void paintEvent(QPaintEvent *event) override; diff --git a/qtsgraph.ui b/qtsgraph.ui deleted file mode 100644 index d9ee13a..0000000 --- a/qtsgraph.ui +++ /dev/null @@ -1,22 +0,0 @@ - - - QTSGraph - - - - 0 - 0 - 800 - 600 - - - - QTSGraph - - - - - - - -