我是靠谱客的博主 危机耳机,最近开发中收集的这篇文章主要介绍qt画柱状图,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

用qt画了个很简单的柱状图,横坐标长度不变,个数越多,每个柱状图的宽度越窄。没什么难度,写过的就记录一下,以后可能用到类似的直接拿来用。

头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMap>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void paintEvent(QPaintEvent *e);

private:
    int getMaxVal(QList<int> list);

private:
    QMap<QString, int> m_answerMap;
    QList<int> m_numList;
    QList<QString> m_answerStrList;
    int m_nPeopleCount;


};

#endif // MAINWINDOW_H

cpp文件

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

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->resize(500, 570);

    m_answerMap.insert("A", 2);
    m_answerMap.insert("ABCD", 1);
    m_answerMap.insert("AD", 3);
    m_answerMap.insert("BD", 10);
    m_answerMap.insert("AC", 2);
    m_answerMap.insert("BCD", 1);

    m_nPeopleCount = 0;

    m_answerStrList = m_answerMap.keys();
    m_numList = m_answerMap.values();
    foreach (int num, m_numList) {
        m_nPeopleCount += num;
    }

}

MainWindow::~MainWindow()
{

}

void MainWindow::paintEvent(QPaintEvent *e)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);
    painter.setPen(Qt::NoPen);
    painter.setBrush(Qt::NoBrush);

    if(m_numList.isEmpty())
    {
        return;
    }

    painter.setPen(Qt::black);

    int lineWidth = 360;
    int lineX = this->width()/2 - lineWidth/2;
    int lineY = 402;
    painter.drawLine(lineX, lineY, lineX + lineWidth, lineY);

    int answerCount = m_answerStrList.count();
    int maxWidth = 41;
    int maxHeight = 280;

    int curWidth = maxWidth - (answerCount - 1)*3;
    int unitWidth = lineWidth/answerCount;

    //获取选项中值最大的那个,以这个为基准高度,来计算其他值的高度
    int maxVal = getMaxVal(m_numList);
    int unitHeight = 0;
    if(maxVal)
    {
        unitHeight = maxHeight/maxVal;
    }

    QFont ft;
    ft.setPixelSize(13);
    for(int i = 0; i < m_answerStrList.count(); ++i)
    {
        QString answerStr = m_answerStrList.at(i);
        int selectNum = m_numList.at(i);
        QString selectNumStr = QString::number(selectNum);

        int x = lineX + i*unitWidth + unitWidth/2 - curWidth/2;
        //绘制柱状图
        int GraphHight = selectNum*unitHeight;
        QRect r = QRect(x, lineY - GraphHight, curWidth, GraphHight);
        QPainterPath path;
        path.addRect(r);
        painter.fillPath(path, QColor(185, 185, 185));

        //绘制答案
        QFontMetrics metrics(ft);
        int charWitdh = metrics.width(answerStr);
        QRect answerRect;
/**     对比文字长度和柱状图的宽度,比柱状图宽的以文字的长度为基准会画,反之以
 *      柱状图的宽度来画,保证显示全并居中,下面的也是一样
 */
        if(charWitdh < curWidth)
        {
            answerRect = QRect(x, lineY, curWidth, 30);
        }
        else
        {
            answerRect = QRect(x - (charWitdh - curWidth)/2, lineY, charWitdh, 30);
        }
        painter.drawText(answerRect, Qt::AlignCenter, answerStr);

        //绘制每个选项对应的值
        QRect percentRect;
        charWitdh = metrics.width(selectNumStr);
        if(charWitdh < curWidth)
        {
            percentRect = QRect(x, lineY - GraphHight - 30, curWidth, 30);
        }
        else
        {
            percentRect = QRect(x - (charWitdh - curWidth)/2, lineY - GraphHight - 40, charWitdh, 30);
        }
        painter.drawText(percentRect, Qt::AlignCenter, selectNumStr);

    }
}

int MainWindow::getMaxVal(QList<int> list)
{
    qSort(list.begin(), list.end());
    return list.last();
}

 

最后

以上就是危机耳机为你收集整理的qt画柱状图的全部内容,希望文章能够帮你解决qt画柱状图所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(37)

评论列表共有 0 条评论

立即
投稿
返回
顶部