开发者

Qt problem with GUI application

开发者 https://www.devze.com 2023-01-20 05:15 出处:网络
I have a simple code in Qt, as below: #include \"mainwindow.h\" #include <QWidget> #include <QGroupBox>

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(t开发者_如何学运维r("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:

Qt problem with GUI application

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


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);
 }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消