QGridLayout, tight rows and columns without padding or spacing

I am using QGridLayout, the first row has a QLabel which is used to show an icon set to 32×32 pixels. The next row has two QSvgWidgets, each of these is 16×14.

My code:

    QGridLayout* pgrdloStatus(new QGridLayout);
    if ( mplblStsIcon == nullptr )
    {
        mplblStsIcon = new QLabel();
    }  
    if ( mpsvgRxIcon == nullptr )
    {
        mpsvgRxIcon = new QSvgWidget(":/SVG_LED");
        mpsvgRxIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        mpsvgRxIcon->setFixedSize(TraineeMonitor::mscuintCommsIconWidth,
                                  TraineeMonitor::mscuintCommsIconHeight);
    }
    if ( mpsvgTxIcon == nullptr )
    {
        mpsvgTxIcon = new QSvgWidget(":/SVG_LED");
        mpsvgTxIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        mpsvgTxIcon->setFixedSize(TraineeMonitor::mscuintCommsIconWidth,
                                  TraineeMonitor::mscuintCommsIconHeight);
    }
    const QString cstrToolTip(QString(
            "   %1: %2rn%3: %4")
                    .arg(tr("Hostname:")).arg(mstrHostname)
                    .arg(tr("MAC address:")).arg(mstrMACaddress));
    mplblStsIcon->setToolTip(cstrToolTip);
    pgrdloStatus->addWidget(mplblStsIcon, 0, 0, 1, 2, Qt::AlignHCenter);
    pgrdloStatus->addWidget(mpsvgRxIcon, 1, 0, Qt::AlignLeft);
    pgrdloStatus->addWidget(mpsvgTxIcon, 1, 1, Qt::AlignRight);
    pgrdloStatus->setMargin(0);
    pgrdloStatus->setSpacing(0);
    return pgrdloStatus;

The result: 8ce2f5ce-c68d-4064-a4e7-c0e3f97ee65f-image.png What I actually want is: 99d2e764-0816-42bf-90f0-76356b1ba7fa-image.png

Answer

I create this Example and add Items From the Designer to show you why you see that distance between icons.

I create GridLayout and put 2 labels and set 2 LED SVG image

this is what actually you do too :

enter image description here

But For Fixing this issue you should check this property :

which means this : label->setScaledContents(true);

enter image description here

Which means that your icons match to labels.

For removing margins of GridLay out you can also do this :

enter image description here

This is its code:

#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H

#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{
public:
    QWidget *centralwidget;
    QGridLayout *gridLayout;
    QLabel *label;
    QLabel *label_2;

    void setupUi(QMainWindow *MainWindow)
    {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(207, 109);
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        gridLayout = new QGridLayout(centralwidget);
        gridLayout->setSpacing(0);
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        gridLayout->setContentsMargins(0, 0, 0, 0);
        label = new QLabel(centralwidget);
        label->setObjectName(QString::fromUtf8("label"));
        label->setPixmap(QPixmap(QString::fromUtf8(":/icons/led-square-red.svg")));
        label->setScaledContents(true);

        gridLayout->addWidget(label, 0, 0, 1, 1);

        label_2 = new QLabel(centralwidget);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setPixmap(QPixmap(QString::fromUtf8(":/icons/led-square-red.svg")));
        label_2->setScaledContents(true);

        gridLayout->addWidget(label_2, 0, 1, 1, 1);

        MainWindow->setCentralWidget(centralwidget);

        retranslateUi(MainWindow);

        QMetaObject::connectSlotsByName(MainWindow);
    } // setupUi

    void retranslateUi(QMainWindow *MainWindow)
    {
        MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
        label->setText(QString());
        label_2->setText(QString());
    } // retranslateUi

};

namespace Ui {
    class MainWindow: public Ui_MainWindow {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MAINWINDOW_H