Solved Custom QWidget doesn't show up in QGridLayout, only in a new window
-
I have a custom QWidget-based class,
MyChart
, that internally creates a QChart. I would like to add instances of this widget to a QGridLayout of another custom QWidget,MyContainer
. However, doing so results inMyChart
not being displayed within the grid layout: an emptyMyContainer
pops up.I can get
MyChart
to show in a new window if I callQChartView->setVisible(true)
after addingMyChart
to theQGridLayout
, but that's not what I want. I would like it to show up in the grid layout.Code:
// MyChart - works fine by itself, I just can't get it to show inside QGridLayout class MyChart : public QWidget { Q_OBJECT public: explicit MyChart(QWidget *parent = 0); ~MyChart(); void showChart() { _chartView->setVisible(true); } // For testing. private: Ui::MyChart *ui; QtCharts::QChartView *_chartView; QtCharts::QChart *_chart; }; MyChart::MyChart(QWidget *parent) : QWidget(parent), ui(new Ui::MyChart) { ui->setupUi(this); // Sample chart. QtCharts::QChart *chart = new QtCharts::QChart(); ... // Fills the chart with some sample data etc. _chart and _chartView are assigned at the end. } class MyContainer : public QWidget { Q_OBJECT public: explicit MyContainer(QWidget *parent = 0); ~MyContainer(); private: Ui::MyContainer *ui; // Contains a QGridLayout. }; MyContainer::MyContainer(QWidget *parent) : QWidget(parent), ui(new Ui::MyContainer) { ui->setupUi(this); // Add sample stuff to MyContainer's QGridLayout. ui->gridLayout->addWidget(new QLabel("Sample label 1"), 0, 0, Qt::AlignCenter); // Works ok. MyChart *chart1 = new MyChart(this); ui->gridLayout->addWidget(chart1, 1, 0, Qt::AlignCenter); // Nothing shows up. // chart1->showChart(); // Opens chart1 in a new window, but it should be inside the gridLayout. } // Application window. int main(int argc, char *argv[]) { QApplication app(argc, argv); MyContainer w; w.setWindowTitle("Test"); w.show(); return app.exec(); }
What am I doing wrong? Am I supposed to create a QMainWindow to host
MyContainer
widget, or something similar? -
Hi
Can you try with
MyChart::MyChart(QWidget *parent) ...
QtCharts::QChart *chart = new QtCharts::QChart(this); // assign parentalso you are not initializing the chart member but a new local variable.
QtCharts::QChart *chart = new QtCharts::QChart();
should be
_chart = new QtCharts::QChart(); -
Thanks, but this doesn't seem to work. Note: I'm initializing members at the end of that ctor, it's just not shown in the pasted code.
I can't assign parent like you specify, because QChart's ctor expects aQGraphicsItem
type rather thanQWidget
, thus resulting in compiler error ...Though you've given me an idea: it could be that I should be using QGraphicsGridLayout instead, and deriving MyChart from QGraphicsWidget instead of QWidget.
-
@InverseTransform
Ah sorry, i forgot you need to use chartView to use charts with widgets.
Im not sure what could be wrong.
If it opens a window with
chart1->showChart()
did you set parent for _chartview ? -
@mrjj Yes, QChartView's parent is set to chart object and works normally: the chart itself is displayed as expected, but in a new window ...
-
@InverseTransform
hmm thats very odd as normally it becomes a window if no parent is set. ( all widgets do)I assume "chart object" is MyChart
There should be nothing wrong with QChartView in layout.
I wonder if it can be a size thing ?
I mean the widget get really compressed when added to layout ? -
I found the solution: you need to add QChartView object to the layout, not the widget itself:
ui->gridLayout->addWidget(chart->getQChartView(), 1, 0, Qt::AlignCenter);
wheregetQChartView()
simply returns the QChartView* of the underlying QChart.