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. Custom QWidget doesn't show up in QGridLayout, only in a new window

Custom QWidget doesn't show up in QGridLayout, only in a new window

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 2.0k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    InverseTransform
    wrote on last edited by InverseTransform
    #1

    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 in MyChart not being displayed within the grid layout: an empty MyContainer pops up.

    I can get MyChart to show in a new window if I call QChartView->setVisible(true) after adding MyChart to the QGridLayout, 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?

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

      Hi
      Can you try with
      MyChart::MyChart(QWidget *parent) ...
      QtCharts::QChart *chart = new QtCharts::QChart(this); // assign parent

      also you are not initializing the chart member but a new local variable.
      QtCharts::QChart *chart = new QtCharts::QChart();
      should be
      _chart = new QtCharts::QChart();

      1 Reply Last reply
      0
      • I Offline
        I Offline
        InverseTransform
        wrote on last edited by InverseTransform
        #3

        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 a QGraphicsItem type rather than QWidget, 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.

        mrjjM 1 Reply Last reply
        0
        • I InverseTransform

          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 a QGraphicsItem type rather than QWidget, 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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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 ?

          I 1 Reply Last reply
          0
          • mrjjM mrjj

            @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 ?

            I Offline
            I Offline
            InverseTransform
            wrote on last edited by
            #5

            @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 ...

            mrjjM 1 Reply Last reply
            0
            • I InverseTransform

              @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 ...

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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 ?

              1 Reply Last reply
              0
              • I Offline
                I Offline
                InverseTransform
                wrote on last edited by InverseTransform
                #7

                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);
                where getQChartView() simply returns the QChartView* of the underlying QChart.

                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