Окружность, чтение клавиши, ожидание нажатия клавиши

main
Артём Проскурнёв 3 years ago
parent eba43e164e
commit cb52162bb6
  1. 57
      example/main.cpp
  2. 2
      main.cpp
  3. 41
      qtsgraph.cpp
  4. 19
      qtsgraph.h

@ -1,5 +1,7 @@
#include "../qtsgraph.h"
#include <string>
#include <iostream>
using namespace std;
using namespace Qt;
int main(int argc, char *argv[])
{
@ -20,30 +22,39 @@ void QTSGraph::PaintBox()
{
// Начало рисования
for(int i = 0; i <= 5; i++)
SetColor(clRed);
Circle(300, 300, 100);
int k;
k = ReadKey();
OutTextXY(20, 40, "Код нажатой клавиши: " + to_string(k));
if(k == 16777220)
{
SetPenStyle(1, 1);
PutPixel(300, 300, clRed, 3);
for(int i = 0; i <= 5; i++)
{
SetPenStyle(1, 1);
SetColor(clBlue);
Rectangle(5, 390 + i * 20, 220, 410 + i * 20);
SetColor(clBlack);
OutTextXY(10, 405 + i * 20, to_string(i));
SetPenStyle(1, i);
Line(30, 400 + i * 20, 200, 400 + i * 20);
}
SetColor(0x00AAAAAA);
Line(120, 120, 135, 260);
SetPenWidth(5);
SetColor(clBlue);
Rectangle(5, 390 + i * 20, 220, 410 + i * 20);
SetColor(clBlack);
OutTextXY(10, 405 + i * 20, std::to_string(i));
SetPenStyle(1, i);
Line(30, 400 + i * 20, 200, 400 + i * 20);
}
SetColor(0x00AAAAAA);
Line(120, 120, 135, 260);
SetPenWidth(5);
SetColor(clBlue);
Line(110, 110, 125, 250);
PutPixel(100, 100, 0x00FF0000, 10);
Delay(1000);
PutPixel(300, 100);
int x = 1;
while(!MouseClicked() && x < 1024)
{
PutPixel(x, 50, 0x555555 + x * 16, 5);
x += 1;
Delay(10);
Line(110, 110, 125, 250);
PutPixel(100, 100, 0x00FF0000, 10);
Delay(1000);
PutPixel(300, 100);
int x = 1;
while(!MouseClicked() && x < 1024)
{
PutPixel(x, 50, 0x555555 + x * 16, 5);
x += 1;
Delay(10);
}
}
// Конец рисования

@ -1,4 +1,6 @@
#include "qtsgraph.h"
using namespace Qt;
using namespace std;
int main(int argc, char *argv[])
{

@ -34,6 +34,14 @@ QTSGraph::~QTSGraph()
// Деструктор
}
void QTSGraph::Circle(int x, int y, int radius)
{
QPainter painter(&Canvas);
painter.setPen(Pen);
painter.drawEllipse(x - radius, y - radius, radius * 2, radius * 2);
update();
}
bool QTSGraph::MouseClicked()
{
bool m = EventMouseClicked;
@ -41,11 +49,11 @@ bool QTSGraph::MouseClicked()
return m;
}
void QTSGraph::OutTextXY(int x, int y, std::string s)
void QTSGraph::OutTextXY(int x, int y, std::string caption)
{
QPainter painter(&Canvas);
painter.setPen(Pen);
painter.drawText(x, y, QString::fromStdString(s));
painter.drawText(x, y, QString::fromStdString(caption));
update();
}
@ -57,6 +65,18 @@ void QTSGraph::PutPixel(int x, int y, QRgb c, int PenWidth)
update();
}
int QTSGraph::ReadKey()
{
if(IDPressedKey == -1)
{
while(!KeyPressed())
Delay(100);
}
int t = IDPressedKey;
IDPressedKey = -1;
return t;
}
void QTSGraph::Rectangle(int x1, int y1, int x2, int y2)
{
QPainter painter(&Canvas);
@ -89,6 +109,13 @@ void QTSGraph::Line(int x1, int y1, int x2, int y2)
update();
}
bool QTSGraph::KeyPressed()
{
bool m = EventKeyPressed;
EventKeyPressed = false;
return m;
}
void QTSGraph::slotStartTimer()
{
StartTimer->stop();
@ -113,3 +140,13 @@ void QTSGraph::mousePressEvent(QMouseEvent *event)
// Правая кнопка
}
}
void QTSGraph::keyPressEvent(QKeyEvent *event)
{
EventKeyPressed = true;
IDPressedKey = event->key();
if (IDPressedKey == Qt::Key_Escape)
{
// Нажатие Esc
}
}

@ -7,15 +7,16 @@
#include <QPen>
#include <QWidget>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QTimer>
#include <QTime>
#include <string>
#define clRed 0x00FF0000
#define clGreen 0x0000FF00
#define clBlue 0x000000FF
#define clBlack 0x00000000
#define clWhite 0x00FFFFFF
#define clRed 0x00FF0000
#define clGreen 0x0000FF00
#define clBlue 0x000000FF
#define clBlack 0x00000000
#define clWhite 0x00FFFFFF
class QTSGraph : public QMainWindow
{
@ -25,11 +26,14 @@ public:
QTSGraph(int w = 640, int h = 480, int x = -1, int y = -1, QWidget *parent = nullptr);
~QTSGraph();
void Circle(int x, int y, int radius);
void Delay(int ms = 1000);
void Line(int x1, int y1, int x2, int y2);
bool KeyPressed();
bool MouseClicked();
void OutTextXY(int x, int y, std::string s);
void OutTextXY(int x, int y, std::string caption);
void PutPixel(int x, int y, QRgb c = 0x00000000, int PenWidth = 1);
int ReadKey();
void Rectangle(int x1, int y1, int x2, int y2);
void SetColor(QRgb c);
void SetPenStyle(int PenWidth, int PenStyle = 1); // Толщина и стиль линии
@ -50,10 +54,13 @@ private:
QPixmap Canvas;
QTimer *StartTimer;
bool EventMouseClicked = false;
bool EventKeyPressed = false;
int IDPressedKey = -1;
void PaintBox();
QPen Pen;
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
};

Loading…
Cancel
Save