derrickbj
16th September 2016, 00:32
Hello, I'm working in Qt 5.7 (MSVC2015) and I'm having problems getting QChartView to display in my QHBoxLayout. I'm writing a program that will parse through a file and add up instances of Ford, Chevy or GMC trucks per a given location. I then want to graphically represent them via a pie chart. I'm trying to use QHPieModelMapper to dynamically update the pie chart upon reading other files. Here's what I have:
MainWindow.h
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void setupModel();
void setupChart();
void readFile();
void updateCarTotals(const QString &, int FORDS=0, int CHEVYS=0,int GMCS=0);
QChart *m_chart;
QStandardItemModel *overallCarCountModel;
int totalFord,totalChevy,totalGMC;
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupModel()
setupChart()
}
void MainWindow::setupModel()
{
overallCarCountModel = new QStandardItemModel(this);
overallCarCountModel->insertRows(0,2);
overallCarCountModel->insertColumns(0,3);
overallCarCountModel->setHeaderData(0,Qt::Horizontal,"Ford");
overallCarCountModel->setHeaderData(1,Qt::Horizontal,"Chevy");
overallCarCountModel->setHeaderData(2,Qt::Horizontal,"GMC");
}
void MainWindow::setupChart()
{
QHBoxLayout* chartLayout = new QHBoxLayout;
m_chart = new QChart;
m_chart->setTitle("Total Cars");
QPieSeries *series = new QPieSeries();
QHPieModelMapper *pcMapper = new QHPieModelMapper(this);
series->setPieSize(0.7); //I know this is the default, just trying whatever to get it working
pcMapper->setValuesRow(0);
pcMapper->setSeries(series);
pcMapper->setModel(overallCarCountModel );
m_chart->addSeries(series);
QChartView *overallCarsPieChartView = new QChartView(m_chart);
overallCarsPieChartView ->setRenderHint(QPainter::Antialiasing);
overallCarsPieChartView ->setMinimumSize(200,200);
chartLayout->addWidget( overallCarsPieChartView );
overallCarsGroupBox->setLayout(chartLayout);
void MainWindow::readFile()
{
// ...
//code to open file, parse words and tally up cars by site name
// ...
updateCarTotals(siteName, fords, chevys, gmcs)
}
void MainWindow::updateCarTotals(const QString &site, int fords, int chevys, int gmcs)
{
QString fordCount,chevyCount,gmcCount;
fordCount.setNum(fords);
chevyCount.setNum(chevys);
gmcCount.setNum(gmcs);
totalFord+=fords;
totalChevy+=chevys;
totalGMC+=gmcs;
overallCarCountModel->setData(overallCarCountModel->index(0,0),QVariant(totalFord));
overallCarCountModel->setData(overallCarCountModel->index(0,1),QVariant(totalChevy));
overallCarCountModel->setData(overallCarCountModel->index(0,2),QVariant(totalGMC));
}
In Designer, I made a MainWindow and put a TabWidget on there. Right now, I have 1 tab and on that tab I have an group box called "overallCarsGroupBox" where I want the pie chart to be displayed.
The code to read the file and tally up the cars works fine, I checked it through qDebug(). So the values being passed to MainWindow::updateCarTotals are valid. When I run the program, the title of the chart ("Total Cars") is displayed fine, but the chart does not display. Any ideas about what I could be doing wrong that would make this chart not display?
MainWindow.h
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
void setupModel();
void setupChart();
void readFile();
void updateCarTotals(const QString &, int FORDS=0, int CHEVYS=0,int GMCS=0);
QChart *m_chart;
QStandardItemModel *overallCarCountModel;
int totalFord,totalChevy,totalGMC;
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
setupModel()
setupChart()
}
void MainWindow::setupModel()
{
overallCarCountModel = new QStandardItemModel(this);
overallCarCountModel->insertRows(0,2);
overallCarCountModel->insertColumns(0,3);
overallCarCountModel->setHeaderData(0,Qt::Horizontal,"Ford");
overallCarCountModel->setHeaderData(1,Qt::Horizontal,"Chevy");
overallCarCountModel->setHeaderData(2,Qt::Horizontal,"GMC");
}
void MainWindow::setupChart()
{
QHBoxLayout* chartLayout = new QHBoxLayout;
m_chart = new QChart;
m_chart->setTitle("Total Cars");
QPieSeries *series = new QPieSeries();
QHPieModelMapper *pcMapper = new QHPieModelMapper(this);
series->setPieSize(0.7); //I know this is the default, just trying whatever to get it working
pcMapper->setValuesRow(0);
pcMapper->setSeries(series);
pcMapper->setModel(overallCarCountModel );
m_chart->addSeries(series);
QChartView *overallCarsPieChartView = new QChartView(m_chart);
overallCarsPieChartView ->setRenderHint(QPainter::Antialiasing);
overallCarsPieChartView ->setMinimumSize(200,200);
chartLayout->addWidget( overallCarsPieChartView );
overallCarsGroupBox->setLayout(chartLayout);
void MainWindow::readFile()
{
// ...
//code to open file, parse words and tally up cars by site name
// ...
updateCarTotals(siteName, fords, chevys, gmcs)
}
void MainWindow::updateCarTotals(const QString &site, int fords, int chevys, int gmcs)
{
QString fordCount,chevyCount,gmcCount;
fordCount.setNum(fords);
chevyCount.setNum(chevys);
gmcCount.setNum(gmcs);
totalFord+=fords;
totalChevy+=chevys;
totalGMC+=gmcs;
overallCarCountModel->setData(overallCarCountModel->index(0,0),QVariant(totalFord));
overallCarCountModel->setData(overallCarCountModel->index(0,1),QVariant(totalChevy));
overallCarCountModel->setData(overallCarCountModel->index(0,2),QVariant(totalGMC));
}
In Designer, I made a MainWindow and put a TabWidget on there. Right now, I have 1 tab and on that tab I have an group box called "overallCarsGroupBox" where I want the pie chart to be displayed.
The code to read the file and tally up the cars works fine, I checked it through qDebug(). So the values being passed to MainWindow::updateCarTotals are valid. When I run the program, the title of the chart ("Total Cars") is displayed fine, but the chart does not display. Any ideas about what I could be doing wrong that would make this chart not display?