【问题标题】:How to display a QChartView inside a QStackedWidget?如何在 QStackedWidget 中显示 QChartView?
【发布时间】:2021-11-08 02:59:33
【问题描述】:

我希望我的表单有一个带有 2 个(至少)页面的 QStackedWidget,并且每个页面都有一个 QChartView。 在表单编辑器中,通过“推广”菜单,我从 QGraphicView 制作了一个 QChartView。 (屏幕截图(到目前为止只有 1 个 QChartView - 这样我就可以看到打开了哪个页面)。

在主窗口中,当按下其中一个按钮时,我想循环打开上述窗口:

void Widget::ShowStudioCharts() noexcept
{
    for(auto & e : this->userInfoVector){
        Form *pForm = new Form();
        pForm->provideStudioData(&e.studiosStats, e.nickname);
        pForm->processStudioStats();
        pForm->show();
    }
}

我试着这样做:

Form.h

#ifndef FORM_H
#define FORM_H
 
#include <QWidget>
#include <QPieSeries>
#include <QChart>
#include <QChartView>
#include <QGridLayout>
#include <vector>
#include <map>
#include <QStackedWidget>
#include <QtGlobal>
#include <QRectF>
#include <QRect>
#include <QPushButton>
 
namespace Ui {
class Form;
}
 
class Form : public QWidget
{
    Q_OBJECT
 
public:
    explicit Form(QWidget *parent = nullptr);
    ~Form();
    void provideStudioData(std::map<std::string, std::size_t> *studiosStats, const std::string &nickname) noexcept;
    void processStudioStats() noexcept;
private slots:
    void on_pushButton_2_clicked();
 
private:
    std::vector<std::map<std::string, size_t>*>  stats;
    std::vector<std::string>  nicknames;
    Ui::Form *ui;
};
 
#endif // FORM_H

Form.cpp

#include "form.h"
#include "ui_form.h"
#include <QPieSeries>
#include <QPieSlice>
#include <QChart>
using namespace QtCharts;
 
Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
}
 
Form::~Form()
{
    delete ui;
}
 
void Form::provideStudioData(std::map<std::string, size_t> *studiosStats, const std::string &nickname) noexcept
{
    this->stats.push_back(studiosStats);
    this->nicknames.push_back(nickname);
}
 
void Form::processStudioStats() noexcept
{
    srand(time(0));
    QPieSeries *series = new QPieSeries();
 
//    for (const auto & e : this->stats ){
//        //QBarSet *set0 = new QBarSet("1");
//        for ( const auto & a : *e){
//            //*set0 << a.second;
//            qDebug( (a.first + " " + std::to_string(a.second)).c_str());
//            }
//    }
    for ( const auto & a : *this->stats[0]){
        QPieSlice * slice = new QPieSlice();
        slice->setColor(QColor(rand()%255, rand()%255, rand()%255));
        slice->setValue(a.second);
        slice->setLabel(a.first.c_str());
        series->append(slice);
       }
 
    QChart *chart = new QChart();
    chart->setAnimationOptions(QChart::AnimationOption::AllAnimations);
    chart->addSeries(series);
 
    //chart->setPlotArea(QRectF(200,0,1400,1100));
    //chart->legend()->detachFromChart();
    chart->legend()->setBackgroundVisible(true);
    chart->legend()->setBrush(QBrush(QColor(128, 128, 128, 128)));
    chart->legend()->setPen(QPen(QColor(192, 192, 192, 192)));
    //chart->legend()->setGeometry(QRectF(20,20,200,1000));
    chart->setTitle(QString::fromStdString(this->nicknames[0]));
    this->setWindowTitle(QString::fromStdString(this->nicknames[0]));
    chart->legend()->setAlignment(Qt::AlignLeft);
    ui->graphicsView  = new QChartView(chart);
    ui->graphicsView->show();     
        //ui->stackedWidget->show();
}
void Form::on_pushButton_2_clicked()
{
    if(0 == this->ui->stackedWidget->currentIndex())
        this->ui->stackedWidget->setCurrentIndex(1);
    else if(1 == this->ui->stackedWidget->currentIndex())
        this->ui->stackedWidget->setCurrentIndex(0);
 
}

编译代码,打开窗口。但问题是我的图表显示在打开的窗口上方的另一个窗口中。 这显然是

ui->graphicsView->show();

但是如果我删除这条线,那么图表根本不可见。 请帮忙,提前谢谢。

【问题讨论】:

    标签: c++ qt qgraphicsview qchart qchartview


    【解决方案1】:

    执行ui-&gt;graphicsView = new QChartView(chart); 不会替换 QChartView,您只是在分配指针。解决方案是重用现有的 QChartView,将其更改为:ui-&gt;graphicsView-&gt;setChart(chart);

    【讨论】:

      猜你喜欢
      • 2016-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2022-12-10
      • 2018-04-15
      • 1970-01-01
      相关资源
      最近更新 更多