3

为了更清楚地解释我的问题,我做了一个截图,上面有一些注释,希望它有所帮助:

QGroupBox 布局格式问题

正如你所看到的,我有一个大QVBoxLayout的应用程序的主要布局,在里面我放了 a Qwidget,然后是 a QGridLayout,然后是 a QGridLayout。在最后一个里面QGridLayout,我放了两个QGroupBoxes,一个在位置 0,0,一个在位置 0,1。每个QGroupBox都有自己的内部布局,两者都是QGridLayout类型。

屏幕截图显示QGroupBox第一个效果很好,而第二个比第一个小很多,有两个问题:1)标签应该是“特定操作”但它是主干的,并且完全显示它的唯一方法似乎是将按钮水平放置在另一个旁边......但我不想要它!2)我设法将QGroupbox其“网格”的左侧对齐,但我需要它位于它的左上角,而它暂时居中......我怎样才能做到这一点?

这是可以帮助您理解的部分代码。这是 kalk.h 文件:

class Kalk : public QWidget
{
    Q_OBJECT

public:
    Kalk(QWidget *parent = 0);

private slots:
    void kalkChange(QString);
//....
private:
    QComboBox *chooser;

    QVBoxLayout *mainLayout;
    QGridLayout *subLayout;

    QGridLayout *operationsLayout;

    QGroupBox *baseOperators;
    QGridLayout *baseOperatorsLayout;

    QGroupBox *specificOperators;
    QGridLayout *specificOperatorsLayout;
};

然后对应的kalk.cpp文件:

Kalk::Kalk(QWidget *parent) : QWidget(parent){
    chooser = new QComboBox();
    //...
    connect(chooser,SIGNAL(currentIndexChanged(QString)),this,SLOT(kalkChange(QString)));

    mainLayout = new QVBoxLayout;
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    subLayout = new QGridLayout;
    subLayout->setEnabled(false);
    subLayout->setSizeConstraint(QLayout::SetFixedSize);
    mainLayout->addWidget(chooser);
    mainLayout->addLayout(subLayout);

    //operationsLayout = new QHBoxLayout;
    operationsLayout = new QGridLayout;
    operationsLayout->setSizeConstraint(QLayout::SetFixedSize);
    baseOperators = new QGroupBox(tr("Base Operations"));
    baseOperatorsLayout = new QGridLayout(baseOperators);
    baseOperatorsLayout->setSizeConstraint(QLayout::SetFixedSize);

    specificOperators = new QGroupBox(tr("Specific Operations"));
    specificOperatorsLayout = new QGridLayout(specificOperators);
    specificOperatorsLayout->setSizeConstraint(QLayout::SetFixedSize);

    operationsLayout->addWidget(baseOperators,0,0);
    operationsLayout->setAlignment(baseOperators,Qt::AlignLeft);
    operationsLayout->addWidget(specificOperators,0,1);
    operationsLayout->setAlignment(specificOperators,Qt::AlignLeft);
    mainLayout->addLayout(operationsLayout);

    setLayout(mainLayout);

//...
}

在另一个函数中,我加载 QGroupBox 布局内的按钮,但我认为问题不在这里......

4

0 回答 0