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

QMainWindow : unable to position widget to extreme left



  • Hi,
    I am unable to position the Widget to extreme left of main window. there is a gap on the left of the screen.
    DTSMain.png

    Here is my code:

    DTSMain::DTSMain(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::DTSMain)
    {
        ui->setupUi(this);
    
        QFont fntGgia10( "Georgia", 10, QFont::Bold);
        QFont fntGgia12( "Georgia", 12, QFont::Bold);
        QFont fntGgia12N( "Georgia", 12, QFont::Normal);
        QFont fntGgia16( "Georgia", 16, QFont::Bold);
    
        QString tabStyle = "QTabBar {background-color: #0B6C87; color:#ffffff;}"
                           "QTabBar::tab:!selected {height: 60px; width: 125px;background-color: #0B6C87; color:#ffffff;font-size:11pt;}"
                           "QTabBar::tab:selected {height: 60px; width: 125px;background-color: #0BAFF2; color:#ffffff;font-size:12pt; font-weight: bold;}";
    
        tw1= new QTabWidget;
    //    tw1->setStyleSheet("QTabBar::tab { height: 40px; width: 100px; font-size:12pt;}");
        tw1->setStyleSheet(tabStyle);
        tw1->setMinimumWidth(1000);
        tw1->setMinimumHeight(700);
        tw1->addTab(new MainTab(), tr("MAIN"));
        tw1->addTab(new UtilitiesTab(), tr("UTILITIES"));
        tw1->addTab(new HCTab(), tr("HEALTH \nCHECK"));
        tw1->addTab(new LVTab(), tr("LOG \nVIEW"));
        tw1->addTab(new GRTab(), tr("GENERATE \nREPORT"));
        tw1->addTab(new HelpTab(), tr("HELP"));
        tw1->addTab(new AboutTab(), tr("ABOUT"));
        tw1->addTab(new ExitTab(), tr("EXIT"));
    
        connect(tw1, SIGNAL(currentChanged(int)), this, SLOT(onTabChanged(int)));
    
        // Debug Window 1
        dbgWdw1 = new DebugPortMainWgt(this, "Debug1");
        dbgWdw1->setMaximumHeight(150);
        dbgWdw1->setMaximumWidth(250);
    
        // Debug Window 2
        dbgWdw2 = new DebugPortMainWgt(this, "Debug2");
        dbgWdw2->setMaximumHeight(150);
        dbgWdw2->setMaximumWidth(250);
    
        // Debug Window 3
        dbgWdw3 = new DebugPortMainWgt(this, "Debug3");
        dbgWdw3->setMaximumHeight(150);
        dbgWdw3->setMaximumWidth(250);
    
        // VBox for Debug Windows
        vbxDebug = new QVBoxLayout;
        vbxDebug->addWidget(dbgWdw1);
        vbxDebug->addWidget(dbgWdw2);
        vbxDebug->addWidget(dbgWdw3);
        vbxDebug->addStretch(1);
    
        // LRU Power Widget
        lblLRUPwr = new QLabel("LRU POWER");
        lblLRUPwr->setAlignment(Qt::AlignHCenter);
        lblLRUPwr->setContentsMargins(10, 0, 0, 0);
        lblLRUPwr->setFont(fntGgia10);
    
        LPW1 = new LRUPwrWgt;
        vbxLRU = new QVBoxLayout;
        vbxLRU->addStretch(1);
        vbxLRU->addWidget(lblLRUPwr);
        vbxLRU->addWidget(LPW1);
    
        // SMFD1 Card Status
        lblSCS1 = new QLabel("SMFD1 CARD STATUS");
        lblSCS1->setAlignment(Qt::AlignHCenter);
        lblSCS1->setContentsMargins(10, 0, 0, 0);
        lblSCS1->setFont(fntGgia10);
        SCS1 = new SMFDCardStatusWgt;
        vbxSCS1 = new QVBoxLayout;
        vbxSCS1->addStretch(1);
        vbxSCS1->addWidget(lblSCS1);
        vbxSCS1->addWidget(SCS1);
    
        // SMFD2 Card Status
        lblSCS2 = new QLabel("SMFD2 CARD STATUS");
        lblSCS2->setAlignment(Qt::AlignHCenter);
        lblSCS2->setContentsMargins(10, 0, 0, 0);
        lblSCS2->setFont(fntGgia10);
        SCS2 = new SMFDCardStatusWgt;
        vbxSCS2 = new QVBoxLayout;
        vbxSCS2->addStretch(1);
        vbxSCS2->addWidget(lblSCS2);
        vbxSCS2->addWidget(SCS2);
    
        // SMFD3 Card Status
        lblSCS3 = new QLabel("SMFD3 CARD STATUS");
        lblSCS3->setAlignment(Qt::AlignHCenter);
        lblSCS3->setContentsMargins(10, 0, 0, 0);
        lblSCS3->setFont(fntGgia10);
        SCS3 = new SMFDCardStatusWgt;
        vbxSCS3 = new QVBoxLayout;
        vbxSCS3->addStretch(1);
        vbxSCS3->addWidget(lblSCS3);
        vbxSCS3->addWidget(SCS3);
    
        hbxBottom = new QHBoxLayout;
        hbxBottom->addLayout(vbxLRU);
        hbxBottom->addLayout(vbxSCS1);
        hbxBottom->addLayout(vbxSCS2);
        hbxBottom->addLayout(vbxSCS3);
        hbxBottom->addStretch(1);
    
        glMain = new QGridLayout;
        glMain->addWidget(tw1, 0, 0);
        glMain->addLayout(vbxDebug, 0, 1, 3, 1, Qt::AlignRight);
        glMain->setRowStretch(0, 1);
        glMain->addItem(new QSpacerItem(0, 200, QSizePolicy::Expanding, QSizePolicy::Expanding), 1, 0);
    //    glMain->addWidget(lblLRUPwr, 2, 0, Qt::AlignLeft | Qt::AlignBottom);
    //    glMain->addItem(new QSpacerItem(200, 0, QSizePolicy::Expanding, QSizePolicy::Expanding), 2, 1);
        glMain->addLayout(hbxBottom, 4, 0, Qt::AlignLeft | Qt::AlignBottom);
    
    
        QWidget *wgt1 = new QWidget();
        wgt1->setLayout(glMain);
    
        setCentralWidget(wgt1);
        setWindowTitle("Display Test Setup - Software Version 1.0");
    }
    
    Any suggestions on aligning my widgets to left??
    

  • Lifetime Qt Champion

    @dan1973 said in QMainWindow : unable to position widget to extreme left:

    lblLRUPwr->setContentsMargins(10, 0, 0, 0);

    Set left margins also to 0



  • @dan1973

    The layouts also have a margin by default (depending on OS/platform, style and layout used)

    By default, QLayout uses the values provided by the style. On most platforms, the margin is 11 pixels in all directions.

    (https://doc.qt.io/qt-5/qlayout.html#setContentsMargins)

    Try setting setContentsMargins(0,top,right,bot) to your main layout. Then everything should move to the left by approx 11px.

    To not change everything else, you could get the QMargin from your QLayout / QWidget first and then just change the left value.
    https://doc.qt.io/qt-5/qlayout.html#contentsMargins

    Just theorycrafting... haven't tested it and don't know how it will look like... ;-)



  • @jsulm I had set left margins of all my widgets to 0.
    tw1->setContentsMargins(0, 0, 0, 0);
    ...
    lblLRUPwr->setContentsMargins(0, 0, 0, 0);
    ....

    No effect. I still see the gap between screen edge and the widget in Main Window.
    ![alt text](DTSMain.png image url)


  • Lifetime Qt Champion

    Hi
    Make sure your glMain layout also is set to zero.



  • @mrjj Thank you. It worked finally. Thanks again. Now in my updated code i had used VBox instead of GL. and i reduced left margin of VBox to 0.



  • @Pl45m4 said in QMainWindow : unable to position widget to extreme left:

    Try setting setContentsMargins(0,top,right,bot) to your main layout. Then everything should move to the left by approx 11px.

    I already said it here :-)
    You must have missed it

    But good to hear that it worked :-)

    It seems quite tempting to use GridLayouts every time, but I would say in most cases cascading VBox and HBox layouts work better


Log in to reply