Results 1 to 6 of 6

Thread: Display own widget in new window

  1. #1
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    18
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Question Display own widget in new window

    Hi.
    I want to do, when I click pushButton it should show new window with my widget (pie chart)

    I have something like this

    Qt Code:
    1. MainWindow::on_PieChart1_pushButton_clicked()
    2. {
    3. QPieSeries *series = new QPieSeries();
    4. series->append("Jane", 1);
    5. series->append("Joe", 2);
    6. series->append("Andy", 3);
    7. series->append("Barbara", 4);
    8. series->append("Axel", 5);
    9.  
    10. QPieSlice *slice = series->slices().at(1);
    11. slice->setExploded();
    12. slice->setLabelVisible();
    13. slice->setPen(QPen(Qt::darkGreen, 2));
    14. slice->setBrush(Qt::green);
    15.  
    16. QChart *chart = new QChart();
    17. chart->addSeries(series);
    18. chart->setTitle("Simple piechart example");
    19. chart->legend()->hide();
    20.  
    21. QChartView *chartView = new QChartView(chart);
    22. chartView->setRenderHint(QPainter::Antialiasing);
    23.  
    24. //here I need to make new window and put chartView inside
    25. }
    To copy to clipboard, switch view to plain text mode 

    So how to write it, to show new window with my pie chart when I click the push button?

    I know you can use setCentralWidget(QWidget *widget) but it works only with mainWindow. I need it in new window. Any ideas?
    Last edited by time; 21st November 2016 at 21:21.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display own widget in new window

    Qt Code:
    1. chartView->show();
    To copy to clipboard, switch view to plain text mode 

    Of course, what you are doing leaks memory for the QChartView. And since you create your QChartView without a parent, it is a top-level window and probably won't appear where you want it to.

    If that isn't the behavior you want, then you'll have to explain what should happen when the new chart is displayed.
    Last edited by d_stranz; 22nd November 2016 at 01:27.
    <=== 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.

  3. #3
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    18
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display own widget in new window

    Quote Originally Posted by d_stranz View Post
    Of course, what you are doing leaks memory for the QChartView.
    So, of course I do not want memory leaks. I should use the delete chartView somewhere near end of function?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display own widget in new window

    I should use the delete chartView somewhere near end of function?
    No. If you don't want a memory leak, then create the QChartView with the MainWindow as a parent. The chart view will then be deleted when the main window is deleted, whether the chart view is visible or not.
    <=== 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.

  5. #5
    Join Date
    Nov 2016
    Location
    Poland
    Posts
    18
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display own widget in new window

    Quote Originally Posted by d_stranz View Post
    No. If you don't want a memory leak, then create the QChartView with the MainWindow as a parent. The chart view will then be deleted when the main window is deleted, whether the chart view is visible or not.
    Oh, ok. Can I ask you for simple example how to do that? How to create QChartView as a parent of MainWindow?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Display own widget in new window

    How to create QChartView as a parent of MainWindow?
    No, no. Your QChartView should be a child of MainWindow. See that there are two QChartView constructors; in Line 21 of your code above use the one that takes a QWidget * parent argument and use "this" (your MainWindow instance) as the value for "parent".
    <=== 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. Replies: 1
    Last Post: 5th April 2013, 03:22
  2. Replies: 6
    Last Post: 9th November 2011, 04:31
  3. Replies: 2
    Last Post: 2nd November 2010, 15:52
  4. QMainWindow to display X Window
    By augusbas in forum Qt Programming
    Replies: 5
    Last Post: 21st July 2010, 14:00
  5. Move QT Window to different x display
    By jbpvr in forum Qt Programming
    Replies: 1
    Last Post: 5th September 2007, 20:28

Tags for this Thread

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.