tags:

views:

62

answers:

2

Hello. I have a simple code in Qt, as below:

#include "mainwindow.h"
#include <QWidget>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QVBoxLayout>

class classA;
class classB;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
  classA * objA = new classA(this);
  classB * objB = new classB(this);

  QVBoxLayout * mainLayout = new QVBoxLayout(this);
  setLayout(mainLayout);
  mainLayout->addWidget(objA);
  mainLayout->addWidget(objB);
 }

MainWindow::~MainWindow(){}

classA::classA(QWidget *parent) : QWidget(parent)
{
    QGroupBox *grupa = new QGroupBox(tr("classA"),this);

    QLabel *labelA1 = new QLabel(tr("Label A1"));
    QLabel *labelA2 = new QLabel(tr("Label A2"));

    QLineEdit *LineEditA1 = new QLineEdit("LineEditA1");
    QLineEdit *LineEditA2 = new QLineEdit("LineEditA2");

    QGridLayout *lay = new QGridLayout(grupa);

    lay->addWidget(labelA1, 0, 0, Qt::AlignLeft);
    lay->addWidget(LineEditA1, 0, 1, Qt::AlignLeft);
    lay->addWidget(labelA2, 1, 0, Qt::AlignLeft);
    lay->addWidget(LineEditA2, 1, 1, Qt::AlignLeft);

    grupa->setLayout(lay);
}

classA::~classA(){}

classB::classB(QWidget *parent) : QWidget(parent)
{
    QGroupBox *grupa = new QGroupBox(tr("classB"),this);

    QLabel *labelB1 = new QLabel(tr("Label B1"));
    QLabel *labelB2 = new QLabel(tr("Label B2"));

    QLineEdit *LineEditB1 = new QLineEdit("LineEditB1");
    QLineEdit *LineEditB2 = new QLineEdit("LineEditB2");

    QGridLayout *lay = new QGridLayout(grupa);

    lay->addWidget(labelB1, 0, 0, Qt::AlignLeft);
    lay->addWidget(LineEditB1, 0, 1, Qt::AlignLeft);
    lay->addWidget(labelB2, 1, 0, Qt::AlignLeft);
    lay->addWidget(LineEditB2, 1, 1, Qt::AlignLeft);

    grupa->setLayout(lay);
}

classB::~classB(){}

As a result, I should see a window with a nicely spaced elements. Unfortunately, I have something like this:

alt text

What am I doing wrong that these items will not spaced properly?

+2  A: 

A QMainWindow needs to have a central widget. Try this code:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
  classA * objA = new classA(this);
  classB * objB = new classB(this);

  QWidget * q = new QWidget();
  setCentralWidget(q);
  QVBoxLayout * mainLayout = new QVBoxLayout(this);
  q->setLayout(mainLayout);
  mainLayout->addWidget(objA);
  mainLayout->addWidget(objB);
 }
Arnold Spence
I added this code and it work OK, but I have warning : QLayout: Attempting to add QLayout "" to MainWindow "", which already has a layout
paw4el
Sorry, yes, as Shynthriir mentions, passing this in the layout constructor assigns the layout to MainWindow. Leave the constructor empty so the layout can be set to q with q->setLayout(mainLayout).
Arnold Spence
A: 

I wanted to point this out really quick first:

QVBoxLayout * mainLayout = new QVBoxLayout(this);
setLayout(mainLayout);

The second line is not needed. If you pass a QWidget to the constructor of a QLayout, the QLayout is set to that QWidget.

To answer your question though, a QMainWindow is composed of various widgets, one of which is a centralWidget. You need to create a new QWidget which functions as your QMainWindow's centralWidget and is composed of your two custom QWidgets.

Shynthriir