zoukankan      html  css  js  c++  java
  • Qt使用cos、sin绘制圆

    实例:

    .pro

     1 QT       += core gui
     2 
     3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     4 
     5 CONFIG += c++17
     6 
     7 # The following define makes your compiler emit warnings if you use
     8 # any Qt feature that has been marked deprecated (the exact warnings
     9 # depend on your compiler). Please consult the documentation of the
    10 # deprecated API in order to know how to port your code away from it.
    11 DEFINES += QT_DEPRECATED_WARNINGS
    12 
    13 # You can also make your code fail to compile if it uses deprecated APIs.
    14 # In order to do so, uncomment the following line.
    15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
    16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    17 
    18 SOURCES += 
    19     main.cpp 
    20     mainwindow.cpp
    21 
    22 HEADERS += 
    23     mainwindow.h
    24 
    25 FORMS += 
    26     mainwindow.ui
    27 
    28 # Default rules for deployment.
    29 qnx: target.path = /tmp/$${TARGET}/bin
    30 else: unix:!android: target.path = /opt/$${TARGET}/bin
    31 !isEmpty(target.path): INSTALLS += target
    32 
    33 RESOURCES += 
    34     resource.qrc
    View Code

    main.cpp

     1 #include "mainwindow.h"
     2 
     3 #include <QApplication>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8     MainWindow w;
     9     w.show();
    10     return a.exec();
    11 }
    View Code

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 #include <QPainter>
     6 #include <QDebug>
     7 #include <QtWidgets/QLabel>
     8 #include <QtWidgets/QLineEdit>
     9 #include <QtWidgets/QMainWindow>
    10 #include <QtWidgets/QMenuBar>
    11 #include <QtWidgets/QSlider>
    12 #include <QtWidgets/QStatusBar>
    13 #include <QtWidgets/QWidget>
    14 #include <QtMath>
    15 
    16 QT_BEGIN_NAMESPACE
    17 namespace Ui { class MainWindow; }
    18 QT_END_NAMESPACE
    19 
    20 class MainWindow : public QMainWindow
    21 {
    22     Q_OBJECT
    23 
    24 public:
    25     MainWindow(QWidget *parent = nullptr);
    26     ~MainWindow();
    27     void paintEvent(QPaintEvent *e);
    28 private slots:
    29     void on_horizontalSlider_valueChanged(int value);
    30 
    31     void on_horizontalSlider_2_valueChanged(int value);
    32 
    33 private:
    34     Ui::MainWindow *ui;
    35     int m_y = 0;//
    36     int m_l = 100;//
    37     int m_nSize = 200;
    38     QPoint m_oCentre;
    39     int m_nCentreWidth = 6;// 中心点宽度
    40 };
    41 #endif // MAINWINDOW_H
    View Code

    mainwindow.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 MainWindow::MainWindow(QWidget *parent)
     5     : QMainWindow(parent)
     6     , ui(new Ui::MainWindow)
     7 {
     8     ui->setupUi(this);
     9     setWindowTitle(QStringLiteral("Qt使用cos、sin绘制圆"));
    10 }
    11 
    12 MainWindow::~MainWindow()
    13 {
    14     delete ui;
    15 }
    16 
    17 void MainWindow::paintEvent(QPaintEvent *e)
    18 {
    19     m_oCentre.setX((m_nSize/2));
    20     m_oCentre.setY((m_nSize/2));
    21 
    22     int nDeviation = 0;
    23     // 生成绘制
    24     QPainter oLinePainter(this);
    25     oLinePainter.setWindow(nDeviation, nDeviation, this->width(), this->height());
    26     // 背景
    27     QBrush objBrush(Qt::yellow, Qt::SolidPattern);
    28     oLinePainter.fillRect(0, 0, m_nSize, m_nSize, objBrush);
    29     // ---------------------画中心点--------------------------
    30     QPainter oCentrePainter(this);
    31     oCentrePainter.setWindow(nDeviation, nDeviation, this->width(), this->height());
    32     QPen oCentrePen;
    33     oCentrePen.setColor(Qt::red);
    34     oCentrePen.setWidth(1);
    35     oCentrePen.setStyle(Qt::SolidLine);
    36     oCentrePainter.setPen(oCentrePen);
    37 
    38     QBrush oCentreBrush(QColor(255,0,0), Qt::SolidPattern);
    39     oCentrePainter.setBrush(oCentreBrush);
    40 
    41     QPainterPath oCentreDrawPath;
    42 
    43     oCentreDrawPath.addRect(m_oCentre.x(),
    44                             m_oCentre.y(),
    45                             m_nCentreWidth,
    46                             m_nCentreWidth);
    47     //
    48     oCentrePainter.drawPath(oCentreDrawPath);
    49     // ---------------------画中心点--------------------------
    50     float x = 0;
    51     float y = 0;
    52     float fPI = 3.14159265358979323;
    53     x = sin(m_y*fPI/180)*m_l + m_oCentre.x();
    54     y = -cos(m_y*fPI/180)*m_l + m_oCentre.x();
    55 
    56 
    57     QPainter oPainter(this);
    58     oCentrePainter.setWindow(nDeviation, nDeviation, this->width(), this->height());
    59     QPen oPen;
    60     oPen.setColor(Qt::blue);
    61     oPen.setWidth(1);
    62     oPen.setStyle(Qt::SolidLine);
    63     oPainter.setPen(oPen);
    64 
    65     QBrush oBrush(QColor(255,0,0), Qt::SolidPattern);
    66     oPainter.setBrush(oBrush);
    67 
    68     QPainterPath oDrawPath;
    69 
    70     oDrawPath.addRect(x,
    71                       y,
    72                       m_nCentreWidth,
    73                       m_nCentreWidth);
    74     //
    75     oPainter.drawPath(oDrawPath);
    76 }
    77 
    78 void MainWindow::on_horizontalSlider_valueChanged(int value)
    79 {
    80     ui->label->setText(QString::number(value));
    81     m_y = value;
    82     this->repaint();
    83 }
    84 
    85 void MainWindow::on_horizontalSlider_2_valueChanged(int value)
    86 {
    87     ui->label_2->setText(QString::number(value));
    88     m_l = value;
    89     this->repaint();
    90 }
    View Code

    mainwindow.ui

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <ui version="4.0">
     3  <class>MainWindow</class>
     4  <widget class="QMainWindow" name="MainWindow">
     5   <property name="geometry">
     6    <rect>
     7     <x>0</x>
     8     <y>0</y>
     9     <width>678</width>
    10     <height>234</height>
    11    </rect>
    12   </property>
    13   <property name="windowTitle">
    14    <string>MainWindow</string>
    15   </property>
    16   <widget class="QWidget" name="centralwidget">
    17    <widget class="QSlider" name="horizontalSlider">
    18     <property name="geometry">
    19      <rect>
    20       <x>260</x>
    21       <y>20</y>
    22       <width>341</width>
    23       <height>22</height>
    24      </rect>
    25     </property>
    26     <property name="maximum">
    27      <number>360</number>
    28     </property>
    29     <property name="orientation">
    30      <enum>Qt::Horizontal</enum>
    31     </property>
    32    </widget>
    33    <widget class="QLabel" name="label">
    34     <property name="geometry">
    35      <rect>
    36       <x>620</x>
    37       <y>20</y>
    38       <width>54</width>
    39       <height>12</height>
    40      </rect>
    41     </property>
    42     <property name="text">
    43      <string>TextLabel</string>
    44     </property>
    45    </widget>
    46    <widget class="QLabel" name="label_2">
    47     <property name="geometry">
    48      <rect>
    49       <x>620</x>
    50       <y>60</y>
    51       <width>54</width>
    52       <height>12</height>
    53      </rect>
    54     </property>
    55     <property name="text">
    56      <string>TextLabel</string>
    57     </property>
    58    </widget>
    59    <widget class="QSlider" name="horizontalSlider_2">
    60     <property name="geometry">
    61      <rect>
    62       <x>260</x>
    63       <y>60</y>
    64       <width>341</width>
    65       <height>22</height>
    66      </rect>
    67     </property>
    68     <property name="maximum">
    69      <number>150</number>
    70     </property>
    71     <property name="value">
    72      <number>100</number>
    73     </property>
    74     <property name="orientation">
    75      <enum>Qt::Horizontal</enum>
    76     </property>
    77    </widget>
    78   </widget>
    79  </widget>
    80  <resources/>
    81  <connections/>
    82 </ui>
    View Code
    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    CSV格式的文件与EXCEL文件的区别
    Arcgis 离线部署api 4.x的两种本地部署方法!
    IDEA版部署离线ArcGIS api for JavaScript
    java web中统一结果返回封装类JsonResult
    网络最大流dinic
    Luogu P3834 可持久化线段树2(主席树)
    LuoguP2824[HEOI2016/TJOI2016]排序
    2021.03.24模拟赛DP
    Luogu P3166数三角形
    乘法逆元
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/15126731.html
Copyright © 2011-2022 走看看