I've subclassed QWidget and defined constructor this way:

LoupingWidget::LoupingWidget(QWidget *parent): QWidget(parent)
{
    QGroupBox *topGroupBox = new QGroupBox(this);

    QGraphicsView *xRGBPlot = new QGraphicsView(this);
    QGraphicsView *yRGBPlot = new QGraphicsView(this);
    QGraphicsView *loupe = new QGraphicsView(this);
    QSlider *slider = new QSlider(this);

    QGridLayout *boxGLayout = new QGridLayout;
    boxGLayout->addWidget(xRGBPlot, 0, 0);
    boxGLayout->addWidget(slider, 0, 1);
    boxGLayout->addWidget(loupe, 1, 0);
    boxGLayout->addWidget(yRGBPlot, 1, 1);

    topGroupBox->setLayout(boxGLayout);
}

Next, I am trying to add it in a QDialog:

Window::Window(QWidget *parent): QDialog(parent)
{
    LoupingWidget *firstLoupindWidget = new LoupingWidget(this);
    LoupingWidget *secondLoupindWidget = new LoupingWidget(this);
//  QGraphicsView *mainPicture  = new QGraphicsView(this);

    QGridLayout *gridLayout = new QGridLayout;
//    gridLayout->addWidget(mainPicture, 0, 0);
    gridLayout->addWidget(firstLoupindWidget, 1, 0);
    gridLayout->addWidget(secondLoupindWidget, 1, 1);
    setLayout(gridLayout);
}

When this two lines are commented out, two widgets are placed horizontally. And that's good, but when I uncomment lines with another QGraphicsViews, it fills entire window.

What am I doing wrong?

有帮助吗?

解决方案

LoupingWidget doesn't have a layout, so when it's added to another layout, layout can't resize it according to its contents. You need to create another layout (e.g. QGridLayout) in LoupingWidget constructor, add topGroupBox to the layout and set the layout as LoupingWidget's layout.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top