Results 1 to 6 of 6

Thread: Problem getting QChartView to display

  1. #1
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Problem getting QChartView to display

    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
    Qt Code:
    1. class MainWindow : public QMainWindow, private Ui::MainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. private:
    10. void setupModel();
    11. void setupChart();
    12. void readFile();
    13. void updateCarTotals(const QString &, int FORDS=0, int CHEVYS=0,int GMCS=0);
    14. QChart *m_chart;
    15. QStandardItemModel *overallCarCountModel;
    16. int totalFord,totalChevy,totalGMC;
    17. };
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    2. {
    3. setupModel()
    4. setupChart()
    5. }
    6.  
    7. void MainWindow::setupModel()
    8. {
    9. overallCarCountModel = new QStandardItemModel(this);
    10. overallCarCountModel->insertRows(0,2);
    11. overallCarCountModel->insertColumns(0,3);
    12. overallCarCountModel->setHeaderData(0,Qt::Horizontal,"Ford");
    13. overallCarCountModel->setHeaderData(1,Qt::Horizontal,"Chevy");
    14. overallCarCountModel->setHeaderData(2,Qt::Horizontal,"GMC");
    15. }
    16.  
    17. void MainWindow::setupChart()
    18. {
    19. QHBoxLayout* chartLayout = new QHBoxLayout;
    20. m_chart = new QChart;
    21. m_chart->setTitle("Total Cars");
    22.  
    23. QPieSeries *series = new QPieSeries();
    24. QHPieModelMapper *pcMapper = new QHPieModelMapper(this);
    25. series->setPieSize(0.7); //I know this is the default, just trying whatever to get it working
    26. pcMapper->setValuesRow(0);
    27. pcMapper->setSeries(series);
    28. pcMapper->setModel(overallCarCountModel );
    29. m_chart->addSeries(series);
    30. QChartView *overallCarsPieChartView = new QChartView(m_chart);
    31. overallCarsPieChartView ->setRenderHint(QPainter::Antialiasing);
    32. overallCarsPieChartView ->setMinimumSize(200,200);
    33. chartLayout->addWidget( overallCarsPieChartView );
    34. overallCarsGroupBox->setLayout(chartLayout);
    35.  
    36. void MainWindow::readFile()
    37. {
    38. // ...
    39. //code to open file, parse words and tally up cars by site name
    40. // ...
    41. updateCarTotals(siteName, fords, chevys, gmcs)
    42.  
    43. }
    44.  
    45. void MainWindow::updateCarTotals(const QString &site, int fords, int chevys, int gmcs)
    46. {
    47. QString fordCount,chevyCount,gmcCount;
    48. fordCount.setNum(fords);
    49. chevyCount.setNum(chevys);
    50. gmcCount.setNum(gmcs);
    51.  
    52. totalFord+=fords;
    53. totalChevy+=chevys;
    54. totalGMC+=gmcs;
    55.  
    56. overallCarCountModel->setData(overallCarCountModel->index(0,0),QVariant(totalFord));
    57. overallCarCountModel->setData(overallCarCountModel->index(0,1),QVariant(totalChevy));
    58. overallCarCountModel->setData(overallCarCountModel->index(0,2),QVariant(totalGMC));
    59. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem getting QChartView to display

    Is "overallCarsGroupBox" in a layout itself?
    Is the TabWidget in a layout?

    I.e. is the layout chain up to the central widget complete?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem getting QChartView to display

    I thought about this. The fact that the chart title (a component of the Chart instance) is displayed means that the chart itself is correctly being placed in the layout chain. I think the problem lies in the communication between the model, mapper, and series. However, I can't see any substantial difference between the OP's code and the code in the BarModelMapper example with respect to setting up the components.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Problem getting QChartView to display

    Thanks for the responses. Yes, I had thought about the layout chain, but since the title is being displayed I didn't think the layout was an issue. If the model is populated, the mapper should pick this up right away, correct? In other words, I don't need to do any particular "refresh" on the mapper/series combo, do I? I couldn't exactly see how to do that anyway, so I hope not I really based this code off of the example code, so it's really baffling me why it's not working.

  5. #5
    Join Date
    Sep 2006
    Posts
    38
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Problem getting QChartView to display

    I found the problem. It seems the default QHPieModelMapper::labelsRow() is -1 (invalid mapping). So it would appear that I have to give it a labelsRow setting in order for the mapping to be valid. I was thinking the model mapper would pull the label information from the header data set through QAbstractItemModel::setHeaderData(), but I guess not. So I had to make my model have two rows, one containing the the words "Ford, Chevy, GMC" for the pie labels and the 2nd row containing the values. Then I set QHPieModelMapper::labelsRow() to 0 and QHPieModelMapper::valuesRow() to 1 and my pie chart displayed properly!

  6. The following user says thank you to derrickbj for this useful post:

    d_stranz (20th September 2016)

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem getting QChartView to display

    Good to know this. As I said, my money was on a problem with the mapper, but with the exception of the problem you identified, it all looked OK. The mapper *should* keep the view updated whenever there is a change to the model, and calling setData() is exactly that.

    I am curious now - if you had derived your model from QAbstractTableModel, would the QHPieModelMapper have automatically used the headers from that? QStandardItemModel *could be* used to represent tabular data, whereas QAbstractTableModel *always* does so the mapper could be assured of finding a horizontal header row.

    Thanks for following up by posting your solution.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to do QChartView delegate like QTableView item delegate
    By malleeswarareddy.s in forum Qt Programming
    Replies: 0
    Last Post: 16th September 2015, 12:37
  2. Display Problem
    By Ashwath in forum Qt Programming
    Replies: 3
    Last Post: 7th March 2011, 11:47
  3. Display Problem
    By yuvaraj.yadav in forum Newbie
    Replies: 1
    Last Post: 4th May 2009, 14:00
  4. Display Problem
    By yuvaraj.yadav in forum Qt Programming
    Replies: 21
    Last Post: 3rd May 2009, 15:32
  5. qtdemo.exe display problem with Qt 4.4.0
    By Dwarf007 in forum Installation and Deployment
    Replies: 5
    Last Post: 4th June 2008, 06:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.