<<1<<2<<3<<4<<5<<6;*set1<<3<<7<<6<<5<<5<<2;*set2<<2<<8<<1<<3<<6<<5;*set3<<1<<3<<5<<7<<1<<6;*set4<<4<<3<<3<<6<<7<<2;//创建一个序列对象,并将上面的数据添加到序列中QBarSeries *bseries = new QBarSeries();bseries->append(set0);bseries->append(set1);bseries->append(set2);bseries->append(set3);bseries->append(set4);//创建一个QChart类的对象chart,并将series对象加入到chart中QChart *chart = new QChart();chart->addSeries(bseries);//设置标题和动画chart->setTitle("simple chart example");chart->setAnimationOptions(QChart::SeriesAnimations);//设置动画//创建坐标//首先创建一个字符串列表,作为横坐标QStringList *categories = new QStringList();*categories<<"Jane"<<"Feb"<<"Mar"<<"Apr"<<"May"<<"Jun";//创建一个类别轴对象QBarCategoryAxis *axis = new QBarCategoryAxis();axis->append(*categories);//添加横坐标,月份chart->createDefaultAxes();//设置默认坐标轴,虽然后面的设置会覆盖默认坐标轴,但是也要在后面设置之前进行初始设置chart->setAxisX(axis,bseries);//将类别轴对象(横坐标月)和序列对象(每个月的数据)作为参数,设置X轴 。//设置图例chart->legend()->setVisible(true);//图例可见chart->legend()->setAlignment(Qt::AlignBottom);//将图例放在表底chart->setTheme(QChart::ChartThemeBlueCerulean);//主题颜色设置为天然色return chart;}QChart *Widget::creatLineChart() const{QLineSeries *series=new QLineSeries();series->append(0,0);series->append(1,1);series->append(2,4);series->append(3,9);series->append(4,7);*series<legend()->hide();//隐藏图例chart->addSeries(series);chart->createDefaultAxes();chart->setTitle("linechart测试示例");chart->setTheme(QChart::ChartThemeBlueCerulean);//主题颜色设置为天然色return chart;}QChart *Widget::creatAreaChart() const{//参考帮助文档源码QLineSeries *series0 = new QLineSeries();QLineSeries *series1 = new QLineSeries();*series0 << QPointF(1, 5) << QPointF(3, 7) << QPointF(7, 6) << QPointF(9, 7) << QPointF(12, 6)<< QPointF(16, 7) << QPointF(18, 5);*series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) << QPointF(12, 3)<< QPointF(16, 4) << QPointF(18, 3);QAreaSeries *series = new QAreaSeries(series0, series1);series->setName("Batman");QPen pen(0x059605);pen.setWidth(3);series->setPen(pen);QLinearGradient gradient(QPointF(0, 0), QPointF(0, 1));gradient.setColorAt(0.0, 0x3cc63c);gradient.setColorAt(1.0, 0x26f626);gradient.setCoordinateMode(QGradient::ObjectBoundingMode);series->setBrush(gradient);QChart *chart = new QChart();chart->addSeries(series);chart->setTitle("Simple areachart example");chart->createDefaultAxes();chart->axisX()->setRange(0, 20);chart->axisY()->setRange(0, 10);chart->setTheme(QChart::ChartThemeBlueCerulean);//主题颜色设置为天然色return chart;}QDockWidget *Widget::m_titlebar(QDockWidget *pd,QWidget * m_chart,QString m_str){/******************************自定义标题栏********************/TitleBar *pTitleBar = new TitleBar(m_chart);pd->installEventFilter(pTitleBar);pd->setWindowTitle(m_str);pd->setTitleBarWidget(pTitleBar);return pd;}/************************窗口移动*********************//*在取消标题栏后,无法对窗口进行拖拽,这里对鼠标点击和移动事件进行重写*///void Widget::mousePressEvent(QMouseEvent *e) // 鼠标点击事件//{//this->lastPos = e->globalPos();//}//void Widget::mouseMoveEvent(QMouseEvent *e) // 鼠标移动事件//{//int xLen = e->globalX() - lastPos.x();//int yLen = e->globalY() - lastPos.y();//this->lastPos = e->globalPos();//move(x()+xLen, y()+yLen); // 移动窗口//}
main.cpp
#include "widget.h"#include int main(int argc, char *argv[]){QApplication a(argc, argv);Widget w;//w.show();//a.setPalette();QPalette palette = a.palette();palette.setColor(QPalette::Window, QColor(4, 7, 38));//改变控件背景色a.setPalette(palette);return a.exec();}