Цвет пикселя, заливка, сброс положений нажатий мыши и клавиатуры

main
Артём Проскурнёв 4 years ago
parent cb52162bb6
commit fe6ba051de
  1. 38
      example/main.cpp
  2. 25
      qtsgraph.cpp
  3. 26
      qtsgraph.h

@ -1,5 +1,6 @@
#include "../qtsgraph.h" #include "../qtsgraph.h"
#include <iostream> #include <iostream>
#include <sstream>
using namespace std; using namespace std;
using namespace Qt; using namespace Qt;
@ -24,12 +25,12 @@ void QTSGraph::PaintBox()
SetColor(clRed); SetColor(clRed);
Circle(300, 300, 100); Circle(300, 300, 100);
OutTextXY(20, 10, "Нажмите Enter или другую клавишу");
int k; int k;
k = ReadKey(); k = ReadKey();
OutTextXY(20, 40, "Код нажатой клавиши: " + to_string(k)); OutTextXY(20, 40, "Код нажатой клавиши: " + to_string(k));
if(k == 16777220) if(k == 16777220)
{ {
PutPixel(300, 300, clRed, 3);
for(int i = 0; i <= 5; i++) for(int i = 0; i <= 5; i++)
{ {
SetPenStyle(1, 1); SetPenStyle(1, 1);
@ -48,7 +49,17 @@ void QTSGraph::PaintBox()
PutPixel(100, 100, 0x00FF0000, 10); PutPixel(100, 100, 0x00FF0000, 10);
Delay(1000); Delay(1000);
PutPixel(300, 100); PutPixel(300, 100);
PutPixel(301, 100);
PutPixel(302, 100);
PutPixel(298, 100);
PutPixel(299, 100);
PutPixel(300, 101);
PutPixel(300, 102);
PutPixel(300, 99);
PutPixel(300, 98);
PutPixel(300, 300, clRed, 3);
int x = 1; int x = 1;
OutTextXY(20, 70, "Можно прервать, если кликнуть мышью.");
while(!MouseClicked() && x < 1024) while(!MouseClicked() && x < 1024)
{ {
PutPixel(x, 50, 0x555555 + x * 16, 5); PutPixel(x, 50, 0x555555 + x * 16, 5);
@ -56,6 +67,31 @@ void QTSGraph::PaintBox()
Delay(10); Delay(10);
} }
} }
SetPenStyle(1, 1);
SetColor(clBlack);
SetFillStyle(3, clRed);
Rectangle(500, 100, 800, 300);
SetFillStyle(CrossPattern, clBlue);
Rectangle(500, 400, 800, 600);
SetPenStyle(1, 1);
SetColor(clBlack);
SetFillStyle(1, clBlack);
Rectangle(10, 600, 200, 700);
stringstream ss;
ss << "#" << hex << GetPixel(100,720);
SetColor(clBlack);
OutTextXY(10, 590, ss.str());
ss.str("");
ss << "#" << hex << GetPixel(50,650);
SetColor(clWhite);
OutTextXY(30, 620, ss.str());
ss.str("");
SetColor(clRed);
SetFillStyle(1, clRed);
Rectangle(200, 600, 400, 700);
ss << "#" << hex << GetPixel(250,650);
SetColor(clWhite);
OutTextXY(230, 620, ss.str());
// Конец рисования // Конец рисования
} }

@ -7,6 +7,10 @@ void QTSGraph::Delay(int ms)
QCoreApplication::processEvents(QEventLoop::AllEvents, 50); QCoreApplication::processEvents(QEventLoop::AllEvents, 50);
} }
QRgb QTSGraph::GetPixel(int x, int y)
{
return Canvas.toImage().pixelColor(x, y).rgba();
}
QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent) QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
@ -24,6 +28,11 @@ QTSGraph::QTSGraph(int w, int h, int x, int y, QWidget *parent)
Canvas.fill(Qt::white); Canvas.fill(Qt::white);
QRgb DefaultColor = 0x00000000; QRgb DefaultColor = 0x00000000;
Pen = QPen(QBrush(QColor(DefaultColor)), 1); Pen = QPen(QBrush(QColor(DefaultColor)), 1);
Brush = QBrush(QColor(DefaultColor),Qt::NoBrush);
ResetTimer = new QTimer();
connect(ResetTimer, SIGNAL(timeout()), this, SLOT(slotResetTimer()));
ResetTimer->start(500);
StartTimer = new QTimer(); StartTimer = new QTimer();
connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer())); connect(StartTimer, SIGNAL(timeout()), this, SLOT(slotStartTimer()));
StartTimer->start(500); StartTimer->start(500);
@ -38,6 +47,7 @@ void QTSGraph::Circle(int x, int y, int radius)
{ {
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(Pen); painter.setPen(Pen);
painter.setBrush(Brush);
painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2); painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2);
update(); update();
} }
@ -67,7 +77,7 @@ void QTSGraph::PutPixel(int x, int y, QRgb c, int PenWidth)
int QTSGraph::ReadKey() int QTSGraph::ReadKey()
{ {
if(IDPressedKey == -1) if(!EventKeyPressed || IDPressedKey == -1)
{ {
while(!KeyPressed()) while(!KeyPressed())
Delay(100); Delay(100);
@ -81,6 +91,7 @@ void QTSGraph::Rectangle(int x1, int y1, int x2, int y2)
{ {
QPainter painter(&Canvas); QPainter painter(&Canvas);
painter.setPen(Pen); painter.setPen(Pen);
painter.setBrush(Brush);
painter.drawRect(x1, y1, x2 - x1, y2 - y1); painter.drawRect(x1, y1, x2 - x1, y2 - y1);
update(); update();
} }
@ -90,6 +101,12 @@ void QTSGraph::SetColor(QRgb c)
Pen.setColor(QColor(c)); Pen.setColor(QColor(c));
} }
void QTSGraph::SetFillStyle(int Pattern, QRgb Color)
{
Brush.setStyle(Qt::BrushStyle(Pattern));
Brush.setColor(QColor(Color));
}
void QTSGraph::SetPenStyle(int PenWidth, int PenStyle) void QTSGraph::SetPenStyle(int PenWidth, int PenStyle)
{ {
Pen.setWidth(PenWidth); Pen.setWidth(PenWidth);
@ -122,6 +139,12 @@ void QTSGraph::slotStartTimer()
PaintBox(); PaintBox();
} }
void QTSGraph::slotResetTimer()
{
EventKeyPressed = false;
EventMouseClicked = false;
}
void QTSGraph::paintEvent(QPaintEvent *event) void QTSGraph::paintEvent(QPaintEvent *event)
{ {
QPainter p(this); QPainter p(this);

@ -11,6 +11,7 @@
#include <QTimer> #include <QTimer>
#include <QTime> #include <QTime>
#include <string> #include <string>
#include <QDebug>
#define clRed 0x00FF0000 #define clRed 0x00FF0000
#define clGreen 0x0000FF00 #define clGreen 0x0000FF00
@ -28,6 +29,7 @@ public:
void Circle(int x, int y, int radius); void Circle(int x, int y, int radius);
void Delay(int ms = 1000); void Delay(int ms = 1000);
QRgb GetPixel(int x, int y);
void Line(int x1, int y1, int x2, int y2); void Line(int x1, int y1, int x2, int y2);
bool KeyPressed(); bool KeyPressed();
bool MouseClicked(); bool MouseClicked();
@ -36,6 +38,27 @@ public:
int ReadKey(); int ReadKey();
void Rectangle(int x1, int y1, int x2, int y2); void Rectangle(int x1, int y1, int x2, int y2);
void SetColor(QRgb c); void SetColor(QRgb c);
void SetFillStyle(int Pattern, QRgb Color); // Стиль и цыет заливки
/*
0 - NoBrush
1 - SolidPattern
2 - Dense1Pattern
3 - Dense2Pattern
4 - Dense3Pattern
5 - Dense4Pattern
6 - Dense5Pattern
7 - Dense6Pattern
8 - Dense7Pattern
9 - HorPattern
10 - VerPattern
11 - CrossPattern
12 - BDiagPattern
13 - FDiagPattern
14 - DiagCrossPattern
15 - LinearGradientPattern
16 - RadialGradientPattern
17 - ConicalGradientPattern
*/
void SetPenStyle(int PenWidth, int PenStyle = 1); // Толщина и стиль линии void SetPenStyle(int PenWidth, int PenStyle = 1); // Толщина и стиль линии
/* /*
0 - NoPen 0 - NoPen
@ -49,14 +72,17 @@ public:
private slots: private slots:
void slotStartTimer(); void slotStartTimer();
void slotResetTimer();
private: private:
QPixmap Canvas; QPixmap Canvas;
QTimer *StartTimer; QTimer *StartTimer;
QTimer *ResetTimer;
bool EventMouseClicked = false; bool EventMouseClicked = false;
bool EventKeyPressed = false; bool EventKeyPressed = false;
int IDPressedKey = -1; int IDPressedKey = -1;
void PaintBox(); void PaintBox();
QBrush Brush;
QPen Pen; QPen Pen;
protected: protected:

Loading…
Cancel
Save