Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Not getting desired alignment of Widgets in QGridLayout



  • I have following code

    class myConfig: public QWidget
    {
    Q_Object;
    public:
    myConfig();

    }

    myConfig::myConfig()
    {
    d_checkBox = new QCheckBox("Use Fill Pattern");

    QPushButton* defaultButton = new QPushButton(tr("Reset Colors"));

    // Max threshold limit
    QLabel* maxThresholdLabel = new QLabel(tr("Use Fill Pattern "));
    maxThresholdLabel->setMaximumWidth(100);
    QDoubleSpinBox* maxThresholdSpinBox = new QDoubleSpinBox();
    maxThresholdSpinBox->setMaximumWidth(75);
    maxThresholdLabel->setBuddy(maxThresholdSpinBox);
    maxThresholdSpinBox->setSingleStep(0.05);
    maxThresholdSpinBox->setMinimum(0);
    maxThresholdSpinBox->setMaximum(INT_MAX);
    maxThresholdSpinBox->setDecimals(2);

    QGridLayout* layout = new IN_CURRENT_POOL QGridLayout;
    layout->addWidget(d_fillCheckBox, 11, 0);
    layout->addWidget(defaultButton, 11, 1);

    // Max threshold limit
    QLabel* maxThresholdLabel = new IN_CURRENT_POOL QLabel(tr("Move Size "));
    maxThresholdLabel->setMaximumWidth(100);
    QDoubleSpinBox* maxThresholdSpinBox = new IN_CURRENT_POOL QDoubleSpinBox();
    maxThresholdSpinBox->setMaximumWidth(75);
    maxThresholdLabel->setBuddy(maxThresholdSpinBox);
    maxThresholdSpinBox->setSingleStep(0.05);
    maxThresholdSpinBox->setMinimum(0);
    maxThresholdSpinBox->setMaximum(INT_MAX);
    maxThresholdSpinBox->setDecimals(2);

    QGridLayout* layout = new IN_CURRENT_POOL QGridLayout;
    layout->addWidget(d_colorTreeWidget, 0, 0, 10, 2);
    layout->addWidget(d_fillCheckBox, 11, 0);
    layout->addWidget(defaultButton, 11, 1);
    layout->addWidget(maxThresholdLabel, 12, 0,1,10,0);
    layout->addWidget(maxThresholdSpinBox, 12,1,Qt::AlignLeft);
    setLayout(layout);

    }

    I have maxThresholdLabel and maxThresholdSpinBox should be side by side in last row and there should be very little space between the two widgets . I am not getting desired results with above code. I am getting layout as follows

    https://postimg.org/image/mna9f66tl/966e5907/

    Could you please suggest how to get max Threshold Label and maxThresholdSpinBox side by side with very little space between them


  • Lifetime Qt Champion

    Hi,

    Looks like QFormLayout might fit the bill.

    Hope it helps.


Log in to reply