Q3DBars在垂直墙上有网格,怎么办?

时间:2019-08-21 16:38:17

标签: c++ qt qt5 data-visualization qwidget

这是情节

enter image description here

这是代码

.pro

QT      += core gui widgets datavisualization
TEMPLATE = app
SOURCES += main.cpp mainwindow.cpp
HEADERS += mainwindow.h

main.cpp

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

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

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

mainwindow.cpp

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

#include <QtDataVisualization/Q3DBars>
using namespace QtDataVisualization;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    resize(800,600);
    Q3DBars *graph = new Q3DBars;
    setCentralWidget(QWidget::createWindowContainer(graph));
    graph->scene()->activeCamera()->setCameraPosition(30,30);
    graph->setBarSpacing(QSizeF(1,3));
    QBar3DSeries *series = new QBar3DSeries;
    graph->addSeries(series);
    QStringList row_labels{"0", "", "20", "", "40"};
    QStringList column_labels{"0", "", "20", "", "40"};
    graph->rowAxis()->setRange(0, row_labels.count()-1);
    graph->columnAxis()->setRange(0, column_labels.count()-1);
    series->dataProxy()->setRowLabels(row_labels);
    series->dataProxy()->setColumnLabels(column_labels);

//    graph->activeTheme()->setGridEnabled(false);
}

MainWindow::~MainWindow(){}

现在要问的是:

我可以在垂直墙上有网格线吗?我的意思是图片中的垂直线,与地板上的垂直线相同。

1 个答案:

答案 0 :(得分:0)

enter image description here

使用Q3DBars可能无法使用Q3DScatter,也许是:

#include "mainwindow.h"

#include <Q3DScatter>

using namespace QtDataVisualization;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {

    auto chart = new Q3DScatter;
    setCentralWidget(QWidget::createWindowContainer(chart));

    auto series = new QScatter3DSeries;
    chart->addSeries(series);

    chart->axisX()->setRange(0,50);
    chart->axisY()->setRange(0,50);
    chart->axisZ()->setRange(0,50);

    chart->setShadowQuality(QAbstract3DGraph::ShadowQualityNone);

    auto add_bar = [](int x, int y, int z){
        auto bar = new QScatterDataArray;;
        while(z-- > 0)
            *bar << QVector3D(y, z, x);
        return bar;
    };

    QList<QScatterDataArray*> row;
    for(int i = 0; i < 5; i++) 
        row << add_bar(0, i*10, 20);

    foreach (auto bar, row)
        series->dataProxy()->addItems(*bar);
}

MainWindow::~MainWindow() {}