Добавлен пример. Изменена структура

main
Артём Проскурнёв 4 years ago
parent ddb4da7102
commit 8bb265fd94
  1. 25
      example/example.pro
  2. 39
      example/main.cpp
  3. 21
      main.cpp
  4. 3
      qtSimpleGraph.pro
  5. 39
      qtsgraph.cpp
  6. 19
      qtsgraph.h
  7. 22
      qtsgraph.ui

@ -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

@ -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);
}
// Конец рисования
}

@ -1,5 +1,4 @@
#include "qtsgraph.h"
#include <QApplication>
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);
// Конец рисования
}

@ -15,8 +15,7 @@ SOURCES += \
HEADERS += \
qtsgraph.h
FORMS += \
qtsgraph.ui
FORMS +=
TRANSLATIONS += \
qtSimpleGraph_ru_RU.ts

@ -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,14 +41,32 @@ 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 ));
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();
}
void QTSGraph::slotStartTimer()
{
StartTimer->stop();
@ -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)

@ -1,3 +1,4 @@
#include <QApplication>
#include <QMainWindow>
#include <QPainter>
#include <QDesktopWidget>
@ -9,9 +10,11 @@
#include <QTimer>
#include <QTime>
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;

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QTSGraph</class>
<widget class="QMainWindow" name="QTSGraph">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>QTSGraph</string>
</property>
<widget class="QWidget" name="centralwidget"/>
<widget class="QMenuBar" name="menubar"/>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Loading…
Cancel
Save