Search code examples
c++qtqwidget

Widget and MainWindow


this is my code:

mLayout = new QVBoxLayout;
mChart = new OpenChart(this);
setCentralWidget(mChart);
mLayout->addWidget(mChart);
mLayout->setMargin(0);
setLayout(mLayout);
QMap<QString,double> empleados;
empleados["Ana"]=5000.0;
empleados["Juan"]=6000.0;
empleados["Luis"]=7000.0;
empleados["Jorge"]=8000.0;
empleados["Francisco"]=100.0;
empleados["Mario"]=10000.0;
mChart->setTitle("sueldos");
mChart->setTipo(OpenChart::Sectores_2D);
mChart->setTipoleyenda(OpenChart::Circular);
const auto empleadosEnd=empleados.end();
for(auto i=empleados.begin();i!=empleadosEnd;++i)
{mChart->addItem(i.key(),i.value());}

Output: enter image description here

I want the widget to display on the whole window of mainwindow !! Help me Please !!!!!


Solution

    • Create a QWidget with parent as your mainwindow.

    • Add chart widget to your vboxlayout.

    • Set the vBoxLayout to dummy widget created in first step.

    • Then assign dummy widget as central widget.

    rough code below:

    mLayout = new QVBoxLayout;
    
    QWidget *vBox = new QWidget(this);
    
    mChart = new OpenChart(this);
    
    mLayout->addWidget(mChart);
    
    vBox->setLayout(mLayout);
    
    setCentralWidget(vBox);
    

    Do not call setLayout again, as the layout is already set to a QWidget (vbox).