c++qtqwidgetqmainwindowqlayout

Widget not shown in QMainWindow


Good evening,

the aim is to have a mainwindow (created without Designer but by coding) with three sections next to each other:

  1. a list of data points (vector)
  2. statistics about the data points
  3. a graphical summary (histogram)of the data

I have started to create my own widget do draw a diagram (just a line for test purposes so far). However, while the tableview widget is shown, the "diagram" widget (instance of class histogram) is not shown (should be a third vertical column).

A very similar problem in Stackoverflow gave me some direction. But while it helped me to display the widget with the table, I did not figure out how to show my custom widget.

post: widgets not shown in qt main window

I have also checked literature (Summerfield and Qt4 Hui Entwicklung mit C++ by Jürgen Wolff), but they only have examples with only one central widget.

#include "mainwindow.h"
#include <QApplication>

#include <QDebug>

MainWindow::MainWindow(QMainWindow *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{

    mainWidget = new QWidget(this);
    setCentralWidget(mainWidget);

    tableWidget = new QTableWidget(mainWidget);         // QTableWidget to display the data vector

   //...    
   // here comes code to fill the table...
   //...


    // result labels
    lbl_sampleSize = new QLabel("sample size");
    lbl_meanValue = new QLabel("mean");
    lbl_sigma = new QLabel("sigma");
    lbl_andersonDarling = new QLabel("Anderson Darling");
    lbl_pValue = new QLabel("p-value for Anderson-Darling");




    rightLayout = new QVBoxLayout();                    // a vertical layout to contain labels
    rightLayout->addWidget(lbl_sampleSize);
    rightLayout->addWidget(lbl_meanValue);
    rightLayout->addWidget(lbl_sigma);
    rightLayout->addWidget(lbl_andersonDarling);
    rightLayout->addWidget(lbl_pValue);
    rightLayout->addStretch();

    //diagram
    diagram = new Histogram(mainWidget);


    mainLayout = new QHBoxLayout(mainWidget);
    mainLayout->addWidget(tableWidget,0);
    mainLayout->addLayout(rightLayout,0);
    mainLayout->addWidget(diagram, 0);
    //mainWidget->setLayout(mainLayout);


}

screenshot: enter image description here

remark: with this code

//diagram
    diagram = new Histogram();


    mainLayout = new QHBoxLayout(mainWidget);
    mainLayout->addWidget(tableWidget,0);
    mainLayout->addLayout(rightLayout,0);
    //mainLayout->addWidget(diagram, 0);
    //mainWidget->setLayout(mainLayout);

    diagram->show();

I was able to create a separate Widget with the test diagram. (removed the parent information and called diagram->show())


Solution

  • Just set the minimum size (QWidget::setMinimumSize()):

    diagram->setMinimumSize(100, 100);
    

    More control over the widget sizing can be achieved through its size policy.