Home > OS >  Expanding QChartView
Expanding QChartView

Time:09-22

A little at lost as to why QChartView will expand when put inside of a QTabWidget.

Here's a picture of the application when QChartView is not expanding (because it's hidden).

enter image description here

The black portion of the app is QOpenGLWidget.

When I click on the chart view, it will gradually increase in size until QOpenGLWidget is hidden.

enter image description here

When QChartView is just in a QVBoxLayout with QOpenGLWidget, then this effect does not occur. It's only when I add QChartView inside of the QTabWidget that this happens. I'm trying to figure out how to have QChartView not expand, and resize the same way other widgets do (such as the QTextEdit widget in this example).

Here's the code, which was written as a minimal example to reproduce the effect.

#include <QApplication>
#include <QChart>
#include <QChartView>
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QTabWidget>
#include <QTextEdit>
#include <QVBoxLayout>

int
main(int argc, char** argv)
{
  QApplication app(argc, argv);

  // Main Window

  QMainWindow main_window;

  main_window.resize(1280, 720);

  main_window.show();

  // Central Widget

  QWidget central_widget(&main_window);

  main_window.setCentralWidget(&central_widget);

  QVBoxLayout layout(&central_widget);

  central_widget.setLayout(&layout);

  // OpenGL Widget

  QOpenGLWidget gl_widget(&central_widget);

  gl_widget.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

  layout.addWidget(&gl_widget);

  // Tab Widget

  QTabWidget tab_widget(&central_widget);

  layout.addWidget(&tab_widget);

  // Log

  QTextEdit text_edit(&tab_widget);

  text_edit.setReadOnly(true);

  tab_widget.addTab(&text_edit, "Log");

  // Chart View

  QtCharts::QChartView chart_view(&tab_widget);

  tab_widget.addTab(&chart_view, "Chart");

  return app.exec();
}

CodePudding user response:

The problem is caused because the QChartView has the expansion sizePolicy as opposed to the QOpenGLWidget, so when it becomes visible it expands, hiding the other widget. The solution is to set a stretch factor associated with each widget in the layout:

layout.addWidget(&gl_widget, 1);
layout.addWidget(&tab_widget, 1);
  • Related