使用Qt画出直方图和分位数图

来源:物联网智能信息处理课程的一个小作业

使用工具:Qt5.8

平台:Mac os 10.11

要求:


一些说明:

1.Qt的窗口绘制函数为

void paintEvent(QPaintEvent *event);

需要从父类继承,然后自己实现,我们的有关绘图的操作就在这里实现。

2.Qt的绘图函数我们使用到的绘图相关函数主要为QPainter,QColor等,QBrush等,用法比较初级。

3.本程序的重点在于在坐标的变换,在写程序之前,一定会要画好早图,只有明白了坐标的变换规则,才能顺利的完成自己的程序。

4.一般的窗口的坐标原点都是客户区左上角,向右为X轴正方向,向下为Y轴正方向。



结果展示:





工程的结构:



项目代码:

IIPDraw2.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-05-14T14:54:02
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = IIPDraw2
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void calc();
    void paintEvent(QPaintEvent *event);

private:
    Ui::MainWindow *ui;
    int P1A[10]={0},P1B[10]={0};
    QList<int> listA,listB;
};

#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}


main window.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    calc();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::calc()
{
    listA<<19<<18<<18<<15<<18
         <<11<<16<<19<<17<<17
         <<20<<17<<19<<20<<20
         <<15<<15<<16;
    listB<<19<<15<<20<<20<<17
         <<18<<18<<15<<16<<18
         <<15<<19<<20<<19<<19
         <<16<<17<<13<<15;
    QListIterator<int> i(listA);
    QListIterator<int> j(listB);
    for(;i.hasNext();)
    {
        int num = i.next();
        P1A[num-10-1]++;
    }
    for(;j.hasNext();)
    {
        int num = j.next();
        P1B[num-10-1]++;
    }

}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setPen(QColor(0,0,0));

    //绘制直方图 远点50,400,单位高度50 单位宽度20
    painter.drawLine(50,400,360,400);     //x轴 单位长10,30个单位,总长350
    painter.drawLine(50,400,50,100);      //y轴 单位50,5个单位,总长300
    painter.drawLine(50,100,45,105);      //上箭头
    painter.drawLine(50,100,55,105);      //上箭头
    painter.drawLine(355,395,360,400);    //右箭头
    painter.drawLine(355,405,360,400);    //右箭头
    int xi = 10;            //单位长度x
    int yi = 50;            //单位长度y
    int u = 3;              //刻度的长度
    //画y轴的刻度
    for(int i=0;i<=5;i++)
    {
        painter.drawLine(50,400-yi*i,50+u,400-yi*i);        //画刻度线
        painter.drawText(QPoint(40,403-yi*i),QString::number(i));   //画刻度数字
    }
    //画x轴的刻度
    for(int i=11;i<=20;i++)
    {
        painter.drawLine(40+xi*3*(i-10),400,40+xi*3*(i-10),403);    //画刻度线
        painter.drawText(QPoint(35+xi*3*(i-10),420),QString::number(i-10)); //画刻度数字
    }
    painter.setBrush(QColor(62,147,192));
    for(int i=11;i<=20;i++)
    {
        painter.drawRect(60+(i-10-1)*30,400-50*P1A[i-10-1],10,50*P1A[i-10-1]);
    }
    painter.setBrush(QColor(62,102,149));
    for(int i=11;i<=20;i++)
    {
        painter.drawRect(70+(i-10-1)*30,400-50*P1B[i-10-1],10,50*P1B[i-10-1]);
    }

    //绘制分位数图 远点500,400,单位高度50 单位宽度20
    painter.drawLine(500,400,950,400);     //x轴 单位长100,4个单位,总长450
    painter.drawLine(500,400,500,70);      //y轴 单位30,10个单位,总长330
    painter.drawLine(500,70,495,75);      //上箭头
    painter.drawLine(500,70,505,75);      //上箭头
    painter.drawLine(945,395,950,400);    //右箭头
    painter.drawLine(945,405,950,400);    //右箭头
    int xj = 100;            //单位长度x
    int yj = 30;            //单位长度y
    int v = 3;              //刻度的长度
    //画y轴的刻度
    for(int i=11;i<=20;i++)
    {
        painter.drawLine(500,400-yj*(i-10),500+v,400-yj*(i-10));        //画刻度线
        painter.drawText(QPoint(480,403-yj*(i-10)),QString::number(i));   //画刻度数字
    }
    //画x轴的刻度
    for(int i=0;i<=4;i++)
    {
        painter.drawLine(500+xj*i,400,500+xj*i,397);    //画刻度线
        painter.drawText(QPoint(490+xj*i,420),QString::number(i/4.0)); //画刻度数字
    }
    //画出分为图点的位置
    //x的位置为:(float)num/listA.length()*400+500  y的位置为:400-(i+1)*30
    int num=1;        //内层循环计数
    painter.setBrush(QColor(62,147,192));
    for(int i=0;i<10;i++)
    {
        for(int j=1;j<=P1A[i];j++)
        {
            painter.drawEllipse((float)num/listA.length()*400+500,400-(i+1)*30,8,8);
            num++;
        }
    }
    num = 1;
    painter.setBrush(QColor(62,102,149));
    for(int i=0;i<10;i++)
    {
        for(int j=1;j<=P1B[i];j++)
        {
            painter.drawEllipse((float)num/listB.length()*400+500,400-(i+1)*30,8,8);
            num++;
        }
    }

    //给出说明
    painter.drawText(QPoint(360,520),"说明:");
    painter.drawText(QPoint(450,520),"A班");
    painter.drawText(QPoint(450,570),"B班");
    painter.setBrush(QColor(62,147,192));
    painter.drawRect(400,500,30,30);
    painter.setBrush(QColor(62,102,149));
    painter.drawRect(400,550,30,30);


    //标题
    QFont font;
    font.setFamily("Microsoft YaHei");
    font.setPointSize(50);
    font.setItalic(true);
    painter.setFont(font);
    painter.drawText(rect(), Qt::AlignHCenter, "A班和B班的直方图和分位数图");


}

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1000</width>
    <height>700</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>1000</width>
    <height>700</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>1000</width>
    <height>700</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget"/>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1000</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>


本程序只是QT图形绘制的最简单最基础的应用,如有哪些写的不好的地方,忘各位批评指正。




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值