Results 1 to 10 of 10

Thread: ActiveQT + Excel charts

  1. #1
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default ActiveQT + Excel charts

    Goodevening to everybody,
    I should create an excel chart from my QT 4.6 application.
    I'using ActiveQT on Mingw (Win XP SO), and I obtain to write some data to an excel sheet.

    How I can add a Series to the chart now?
    This is my code.
    Qt Code:
    1. QAxObject * excel = new QAxObject("Excel.Application",0);
    2. QAxObject *workbooks = excel->querySubObject( "Workbooks" );
    3. QAxObject *workbook = workbooks->querySubObject( "Add");
    4. QAxObject *sheets = workbook->querySubObject( "Worksheets" );
    5. QAxObject *charts =workbook->querySubObject("Charts");
    6. QAxObject *chart = charts->querySubObject("Add");
    7.  
    8. QAxObject *sheet = sheets->querySubObject( "Item( int )", 1 );
    9. sheet->setProperty("Name","Dati applicazione");
    10.  
    11. QAxObject*cell=sheet->querySubObject("Cells(int,int)",1,1);
    12. cell->dynamicCall("SetValue(String)","Serie");
    13.  
    14. cell=sheet->querySubObject("Cells(int,int)",1,2);
    15. cell->dynamicCall("SetValue(String)","Dati");
    16.  
    17. for (int i=2; i<10; i++){
    18. cell=sheet->querySubObject("Cells(int,int)",i,1);
    19. cell->dynamicCall("SetValue(int)",i-1);
    20. cell=sheet->querySubObject("Cells(int,int)",i,2);
    21. cell->dynamicCall("SetValue(double)",qrand());
    22. }
    23.  
    24. chart->setProperty("Name","Report Grafico dei dati");
    25. chart->setProperty("CharThype",73);
    26.  
    27. QAxObject *series = chart->property("SeriesCollection");
    To copy to clipboard, switch view to plain text mode 
    I got this error:
    QAxBase: Error calling IDispatch member SeriesCollection: Member not found
    Thanks for your time

    Michele

  2. #2
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    Hi,

    QAxObject *series = chart->querySubObject("SeriesCollection");

    Then try to use NewSeries.

    Maybe you should try this before in VBA to see how to do it.

    HTH, Bernd

  3. #3
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    hello bst,
    thanks for your reply but... no way
    It got Segmentation Fault and the pointer of debug highlight this code:
    Qt Code:
    1. bool QAxBase::isNull() const
    2. {
    3. return !d->ptr;
    4. }
    To copy to clipboard, switch view to plain text mode 

    so the QAxBase class don't return anything for this code:

    Qt Code:
    1. QAxObject *series = chart->querySubObject("SeriesCollection");
    2. QAxObject *serie = series->querySubObject("NewSeries");
    To copy to clipboard, switch view to plain text mode 

    When I try to exec NewSeries command, it got Segmentation Fault.

    I use VBA on excel several time, it show Object and Collection, in this case a collaction give the problem.

    It is possible to create a "Collection-equivalent" QAxBase object?
    How can I create a new series?
    Where could I show how I should use the Excel methods?

    Thanks for your time

  4. #4
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    Hi,

    doesn't work here either. I've no idea why :-(

    What works here is to 'force' Charts.Add to already add a series. This can be done by 'Selecting the source-range' before calling Charts.Add. Here - for one series - it's enough to write into the cells and set XValues and Values later.

    HTH, Bernd
    --
    Qt Code:
    1. #include <QtGui/QtGui>
    2. #include <QAxWidget>
    3. #include <QAxObject>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. QAxWidget *excel = new QAxWidget("Excel.Application", 0);
    10. excel->setProperty("SheetsInNewWorkbook", 1);
    11. // excel->setProperty("Visible", true);
    12. QAxObject *workbooks = excel->querySubObject("Workbooks");
    13. QAxObject *workbook = workbooks->querySubObject("Add");
    14. QAxObject *worksheet = workbook->querySubObject("Worksheets (int)", 1);
    15.  
    16. worksheet->setProperty("Name","Dati applicazione");
    17.  
    18. QAxObject *cell = worksheet->querySubObject("Cells(int,int)", 1, 1);
    19. cell->dynamicCall("SetValue(String)", "Serie");
    20.  
    21. cell = worksheet->querySubObject("Cells(int,int)", 1, 2);
    22. cell->dynamicCall("SetValue(String)", "Dati");
    23.  
    24. for (int i = 2; i < 10; i++){
    25. cell = worksheet->querySubObject("Cells(int,int)", i, 1);
    26. cell->dynamicCall("SetValue(int)", i - 1);
    27. cell = worksheet->querySubObject("Cells(int,int)", i, 2);
    28. cell->dynamicCall("SetValue(double)", qrand());
    29. }
    30.  
    31. QAxObject *charts = workbook->querySubObject("Charts");
    32. QAxObject *chart = charts->querySubObject("Add");
    33. chart->setProperty("Name", "Report Grafico dei dati");
    34. chart->setProperty("ChartType", 73);
    35.  
    36. QAxObject * serie = chart->querySubObject("SeriesCollection(int)", 1);
    37. QAxObject * xvalues = worksheet->querySubObject("Range(A2:A9)");
    38. QAxObject * yvalues = worksheet->querySubObject("Range(B2:B9)");
    39.  
    40. serie->setProperty("XValues", xvalues->asVariant());
    41. serie->setProperty("Values", yvalues->asVariant());
    42.  
    43. workbook->dynamicCall("SaveAs (const QString&)", "E:/test/chartbyqt");
    44. workbook->dynamicCall("Close (Boolean)", false);
    45. excel->dynamicCall("Quit (void)");
    46.  
    47. return 0;
    48. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    mmm interesting!!

    do u think I could use this way for more than one series?
    I should build an excel file, with several sheets: most of these contains (datetime-values) couples, and at the end i should plot all series in a chart.

    Probably this way give me the answer.

    Thanks a lot for your time, I'll try it soon.

    Michele

    PS. I saw other way to connect with OCX object such as VOLE, it divide the Object, from Collection, so the NewSeries command can be used, probabily the answer is concernent this.

  6. #6
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    Hi,

    this works here. The 'trick' ist the range->dynamicCall("Select (void)");

    HTH, Bernd
    --
    Qt Code:
    1. #include <QtGui/QtGui>
    2. #include <QAxWidget>
    3. #include <QAxObject>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. QAxWidget *excel = new QAxWidget("Excel.Application", 0);
    11. //excel->setProperty("SheetsInNewWorkbook", 3);
    12. //excel->setProperty("Visible", true);
    13. QAxObject *workbook = excel->querySubObject("Workbooks")->querySubObject("Add");
    14. QAxObject *worksheet = workbook->querySubObject("Worksheets(1)");
    15. worksheet->setProperty("Name", "Dati applicazione");
    16. worksheet->querySubObject("Cells(1,1)")->dynamicCall("SetValue", "Serie");
    17. worksheet->querySubObject("Cells(1,2)")->dynamicCall("SetValue", "Dati");
    18.  
    19. QAxObject *cell;
    20. double dval;
    21. for (int i = 2; i < 10; i++){
    22. dval = qrand();
    23. cell = worksheet->querySubObject("Cells(int,int)", i, 1);
    24. cell->dynamicCall("SetValue(int)", i - 1);
    25. cell = worksheet->querySubObject("Cells(int,int)", i, 2);
    26. cell->dynamicCall("SetValue(double)", dval);
    27. cell = worksheet->querySubObject("Cells(int,int)", i, 3);
    28. cell->dynamicCall("SetValue(double)", dval/2);
    29. cell = worksheet->querySubObject("Cells(int,int)", i, 4);
    30. cell->dynamicCall("SetValue(double)", dval/3);
    31. }
    32. // Selecting 3 columns will force charts.add to create 3 series !
    33. QAxObject *range = worksheet->querySubObject("Range(A2:C9)");
    34. range->dynamicCall("Select (void)");
    35.  
    36. QAxObject *chart = workbook->querySubObject("Charts")->querySubObject("Add");
    37. chart->setProperty("Name", "Report Grafico dei dati");
    38. chart->setProperty("ChartType", 73);
    39.  
    40. QAxObject * series = chart->querySubObject("SeriesCollection");
    41. // qDebug() << series->property("Count"); // 3 !
    42.  
    43. QAxObject * serie = series->querySubObject("Item (int)", 1);
    44. QAxObject * xvalues = worksheet->querySubObject("Range(A2:A9)");
    45. QAxObject * yvalues = worksheet->querySubObject("Range(B2:B9)");
    46. serie->setProperty("XValues", xvalues->asVariant());
    47. serie->setProperty("Values", yvalues->asVariant());
    48.  
    49. serie = series->querySubObject("Item (int)", 2);
    50. yvalues = worksheet->querySubObject("Range(C2:C9)");
    51. serie->setProperty("XValues", xvalues->asVariant());
    52. serie->setProperty("Values", yvalues->asVariant());
    53.  
    54. serie = series->querySubObject("Item (int)", 3);
    55. yvalues = worksheet->querySubObject("Range(D2:D9)");
    56. serie->setProperty("XValues", xvalues->asVariant());
    57. serie->setProperty("Values", yvalues->asVariant());
    58.  
    59. workbook->dynamicCall("SaveAs (const QString&)", "E:/test/chartbyqt");
    60. workbook->dynamicCall("Close (Boolean)", false);
    61. excel->dynamicCall("Quit (void)");
    62.  
    63. return 0;
    64. //return a.exec();
    65. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    Dear bst,
    I tried your code but... It doesn't run! Give me this error:
    QAxBase: Error calling IDispatch member SeriesCollection: Member not found
    Now... looking at your code, I saw that you use QAxWidget, while I ever use QAxObject to open Excel.Application, which is the difference? I need to add some libraries? any project configuration? This is my project file:
    Qt Code:
    1. QT += core gui sql
    2. CONFIG +=qaxcontainer
    3. TARGET = TestThread
    4. TEMPLATE = app
    5.  
    6.  
    7. SOURCES += main.cpp\
    8. mythread1.cpp \
    9. dlgok.cpp \
    10. [...]
    11.  
    12. HEADERS += \
    13. mythread1.h \
    14. dlgok.h \
    15. [...]
    16. FORMS += \
    17. dlgok.ui
    To copy to clipboard, switch view to plain text mode 
    there is something wrong?

    Thanks for your time.

    Michele


    Added after 5 minutes:


    I'm use Wt 4.7.2 on Windows and Excel 2007.

    Thanks for your time.

    Michele
    Last edited by cia.michele; 16th June 2011 at 10:36.

  8. #8
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    Hi Michele,

    I tried your code but... It doesn't run!
    Here - with Win XP and Office 2003 prof. - it runs fine. I'm using Qt 4.5.3 and VS2003, and the following .pro:

    TEMPLATE = app
    CONFIG += debug console qaxcontainer
    TARGET = ExcelChart

    SOURCES += ./main.cpp

    Addition: Whether you use QAxObject or QAxWidget shouldn't matter here.

    cu, Bernd
    Last edited by bst; 20th June 2011 at 09:05.

  9. #9
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    I try it on Office 2010 Prof. It seems run OK!
    Thanks for your Help!

    Michele

  10. #10
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: ActiveQT + Excel charts

    How about if I want to create embedded charts in excel?

Similar Threads

  1. Replies: 21
    Last Post: 8th December 2011, 02:18
  2. Help with Bar charts
    By romeo in forum Qwt
    Replies: 0
    Last Post: 30th June 2010, 16:58
  3. ActiveQt hosting Excel sheet
    By ArmanS in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2010, 10:27
  4. ActiveQt + Excel
    By nile.one in forum Qt Programming
    Replies: 7
    Last Post: 19th October 2007, 22:58
  5. MVC charts for Qt4
    By wysota in forum Qt-based Software
    Replies: 8
    Last Post: 28th September 2006, 16:06

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
  •  
Qt is a trademark of The Qt Company.