我希望我的表單有一個QStackedWidget,有2個(至少)頁面,每個頁面有一個QChartView。 在表單編輯器中,通過 "推廣 "選單,我從QGraphicView制作了一個QChartView。(截圖(到目前為止,只有一個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>/span>
#include <QChart>
#include <QChartView>/span>
#include <QGridLayout>/span>
#include <vector>
#include <map>
#include <QStackedWidget>/span>
#include <QtGlobal>/span>
#include <QRectF>/span>
#include <QRect>
#include <QPushButton>/span>
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"/span>
#include "ui_form.h"
#include <QPieSeries>
#include <QPieSlice>/span>
#include <QChart>
using namespace QtCharts;
Form::Form(QWidget *parent) :
QWidget(father),
ui(new Ui::Form)
{
ui->setupUi(this)。
}
Form::~Form()
{
delete ui;
}
void Form::providerStudioData(std::map<std:: string, size_t> *studiosStats, const std::string & nickname) noexcept
{
this->stats.push_back(stiosStats)。
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)。)
sice->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));/span>
//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->notnames[0])。
this->setWindowTitle(QString:: fromStdString(this->nicknames[0]))。)
chart->legend()->setAlignment(Qt::AlignLeft)。
ui->graphicsView = new QChartView(圖表)。
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()。
但是如果我洗掉這一行,那么圖形就完全不可見了。
請幫助我,提前感謝。
。

uj5u.com熱心網友回復:
執行ui->graphicsView = new QChartView(chart);并不能取代QChartView,你只是分配了指標。解決辦法是重用現有的QChartView,這樣它就變成了。ui->graphicsView->setChart(chart);.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/310171.html
標籤:
