ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  change qtdemo to show viewportcontrol chart without opening on new window
Posted by oliver smith on Feb-03-2023 00:07
i want to have a qt program like qtdemo that has treewidget and when i clicked on treewidget item, a new viewportcontrol chart like viewportcontroldemo open on the left side of that window like other demo charts (not open on new window).
i added viewportcontroldemo class on qtdemo class but it doesn't work.

Please let me know if you have any solution. Thank you!
Best Regards,

  Re: change qtdemo to show viewportcontrol chart without opening on new window
Posted by Peter Kwan on Feb-03-2023 13:07
Hi Oliver,

The original ViewPortControlDemo is just an example of using ChartDirector. You can freely modify it to fit you needs.

In the sample code that comes with ChartDirector, the ViewPortControlDemo is derived from QDialog. QDialog is a top level window in Qt (please refer to Qt documentation for details). It cannot be contained in another Qt window.

If you want to put the ViewPortControlDemo in another Qt window, you may change the base class to QFrame instead. (You need to modify both "viewportcontroldemo.cpp" and "viewportcontroldemo.h".) You can now add it to another Qt window just like any other Qt widgets.

In the original qtdemo code, if you read the code, you can see the right side is occupied by a QScrollArea which contains 8 QChartViewer widgets. If you want to do a quick test, you can hide the QScrollArea and add the modified ViewPortControlDemo to the right side. This is by adding the following code at the bottom of the QTDemo::QTDemo(QWidget *parent) constructor.

    // This is just for testing to show that the ViewPortControlDemo can be put at the
    // right side of the tree widget.
    scrollArea->hide();
    ViewPortControlDemo *v = new ViewPortControlDemo(this);
    horizontalLayout->addWidget(v);

The original qtdemo sample code also includes code that assumes ViewPortControlDemo is a QDialog. It is in the viewportcontroldemo function in the democharts.cpp. You need to remove them as well, otherwise the code cannot compile. It is like:

// Comment out the code in the following function as ViewPortControlDemo is
// no longer a QDialog.
void viewportcontroldemo(QChartViewer* viewer, int /* chartIndex */)
{
    //ViewPortControlDemo d;
    //d.exec();
}

Now when you run the qtdemo, you can see the right side becomes the ViewPortControlDemo.

In your real code, you have to add the widget to the right side only if the user press an item in the tree widget, and then remove it when the user select another item.

Note that this question is completely related to ChartDirector. It is about how to use Qt. The modification above contains only Qt code. There is no charting code that needs to be modified.

Best Regards
Peter Kwan

  Re: change qtdemo to show viewportcontrol chart without opening on new window
Posted by Oliver Smith on Feb-05-2023 03:48
Hi Peter

Thanks for your response.
I do your solution by changing viewPortcontrolDemo to qframe based class and it work well.