Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to use ChartView at DockWidget
QtWS25 Last Chance

How to use ChartView at DockWidget

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • yanagidaY Offline
    yanagidaY Offline
    yanagida
    wrote on last edited by
    #1

    I tried adding ChartView to DockWidget but ChartView was not shown(only blank dockwidget was shown).
    The code is as follows.

    [mainwindow.cpp]
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        Chart *chart = new Chart;
        chart->setTitle("Chart");
        chart->legend()->hide();
        chart->setAnimationOptions(QChart::AllAnimations);
    
        QChartView chartView(chart);
        chartView.setRenderHint(QPainter::Antialiasing);
    
        QDockWidget *chartwidget = new QDockWidget();
        chartwidget->setWidget(&chartView);
        addDockWidget(Qt::LeftDockWidgetArea, chartwidget);
    
        ui->setupUi(this);
    }
    

    However, following code could show the chartview at dockwidget.

    [main.cpp]
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
    
        Chart *chart = new Chart;
        chart->setTitle("Chart");
        chart->legend()->hide();
        chart->setAnimationOptions(QChart::AllAnimations);
    
        QChartView chartView(chart);
        chartView.setRenderHint(QPainter::Antialiasing);
    
        QDockWidget *chartwidget = new QDockWidget();
        chartwidget->setWidget(&chartView);
        w.addDockWidget(Qt::LeftDockWidgetArea, chartwidget);
    
        w.show();
    
        return a.exec();
    }
    

    Why is chartview not shown in the mainwindow.cpp?

    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome
      You might have classic error where the object runs out of scope.

      QChartView chartView(chart); << not using new. so local variable
      chartwidget->setWidget(&chartView); << assign to other object
      } << here chartView is deleted. runs out of scope. when function ends.

      It did work in the "working" sample as return a.exec() prevented it from running out of scope
      as it stays in there until app ends.

      yanagidaY 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi and welcome
        You might have classic error where the object runs out of scope.

        QChartView chartView(chart); << not using new. so local variable
        chartwidget->setWidget(&chartView); << assign to other object
        } << here chartView is deleted. runs out of scope. when function ends.

        It did work in the "working" sample as return a.exec() prevented it from running out of scope
        as it stays in there until app ends.

        yanagidaY Offline
        yanagidaY Offline
        yanagida
        wrote on last edited by
        #3

        @mrjj

        Thank you for your reply!!
        I did solve the problem.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved