首先嵌套QGraphicsScene自己做一个头
void CreateTopWidget::initWidget()
{
setStyleSheet("background-color:rgb(8, 21, 66);");
m_scene = new QGraphicsScene();
m_scene->setSceneRect(0, 0, 100, 10);
m_grapView = new QGraphicsView();
m_grapView->setStyleSheet("border: 3px solid rgb(8, 21, 66);");
m_grapView->setScene(m_scene);
QHBoxLayout * lay = new QHBoxLayout();
lay->setContentsMargins(9, 0, 0, 0);
lay->setSpacing(0);
setLayout(lay);
QWidget * widget = new QWidget();
QHBoxLayout* tblay = new QHBoxLayout();
tblay->setContentsMargins(0, 0, 0, 0);
tblay->setSpacing(20);
widget->setLayout(tblay);
lb_img = new QLabel();
tblay->addWidget(lb_img);
lb_name = new QLabel();
tblay->addWidget(lb_name);
QSpacerItem* spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
tblay->addSpacerItem(spacer);
QHBoxLayout* btnLay = new QHBoxLayout();
btnLay->setContentsMargins(0, 0, 0, 0);
QWidget* wt_title = new QWidget();
wt_title->setGeometry(0, 0, 100, 10);
btnLay->addWidget(wt_title);
tblay->addLayout(btnLay);
QHBoxLayout* hblay = new QHBoxLayout();
hblay->setContentsMargins(0, 0, 0, 0);
hblay->setSpacing(0);
wt_title->setLayout(hblay);
hblay->addWidget(m_grapView);
lay->addWidget(widget);
}
因为QChart + QGraphicsScene 也可以使用,所以以QGraphicsView为基类
MyChart::MyChart(QWidget *parent) : QGraphicsView(parent)
{
initWidget();
}
void MyChart::initWidget()
{
m_grapScene = new QGraphicsScene();
this->setScene(m_grapScene);
wt_top = new CreateTopWidget();
m_chart = new QChart();
QBrush brush(QColor(8, 21, 66));
m_chart->setBackgroundBrush(brush);
m_chart->setAnimationOptions(QChart::SeriesAnimations);
m_chart->createDefaultAxes();
QLegend *le = m_chart->legend();
m_grapScene->addItem(m_chart);
m_grapScene->addWidget(wt_top);
wt_top->setGrapScene(le);//获取当前图例放入QGraphicsScene中
}
之后
void MainWindow::initWidget()
{
QGraphicsScene* scene = new QGraphicsScene();
scene->setSceneRect(0, 10, 400, 300);
QGraphicsView* view = new QGraphicsView();
view->setScene(scene);
view->setSceneRect(0, 0, 400, 300);
MyChart *chart = new MyChart();
chart->setTitle(":/icon.png", "Series 样例");
chart->setGeometrys(0, 10, 600, 400);
QBarCategoryAxis* Axis = new QBarCategoryAxis();
Axis->setGridLineVisible(false);
Axis->setLabelsColor(QColor(255, 255, 255));
QStringList list;
list<<"0"<<"1"<<"2"<<"3"<<"4"<<"5";
Axis->append(list);
QValueAxis* AxisY = new QValueAxis;
AxisY->setLabelsColor(QColor(255, 255, 255));
AxisY->setMinorGridLineVisible(false);
AxisY->setGridLineVisible(false);
AxisY->setLabelFormat("%d"); //设置刻度的格式
AxisY->setRange(0, 10);
AxisY->setTickCount(5); //设置多少格
AxisY->setMinorTickCount(5); //设置每格小刻度线的数目
QSplineSeries *series = new QSplineSeries(chart);
QList<QPointF> pointF;
pointF<<QPoint(0,1)<<QPoint(1,1)<<QPoint(2,2)<<QPoint(3,3)<<QPoint(4,4);
series->append(pointF);
QString name("Series ");
series->setName(name + QString::number(0));
QSplineSeries *series1 = new QSplineSeries(chart);
QList<QPointF> pointF1;
pointF1<<QPoint(0,9)<<QPoint(1,4)<<QPoint(2,3)<<QPoint(3,1)<<QPoint(4,10);
series1->append(pointF1);
series1->setName(name + QString::number(1));
chart->Legend()->setLabelColor(QColor(255,255,255));
chart->addSeries(series);
chart->addSeries(series1);
chart->setAxisX(Axis, series);
chart->setAxisX(Axis, series1);
chart->setAxisY(AxisY, series);
chart->setAxisY(AxisY, series1);
ui->widget->layout()->addWidget(chart);
}
效果图:
下载地址:https://download.csdn.net/download/u010304326/10539740