Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Offset QLabel from widget center.
QtWS25 Last Chance

Offset QLabel from widget center.

Scheduled Pinned Locked Moved Solved General and Desktop
layout
3 Posts 2 Posters 704 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aidan34
    wrote on 18 May 2022, 16:40 last edited by
    #1

    I'm trying to have a QLabel consistently positioned at a fixed vertical offset from the center of a parent widget, although I'm having trouble achieving this with layouts.
    Any suggestions would be appreciated.

    Qt_label_position.png

    #include "container.h"
    
    container::container(QWidget *parent)
        : QWidget{parent}
    {
    
        layout = new QGridLayout();
    
        label = new QLabel(this);
        label->setText("My Label");
        label->setAlignment(Qt::AlignCenter);
    
        layout->addWidget(label, 0, 0);
        setLayout(layout);
    
        //label->move(0,-30); Something like this
        square = new QPolygon();
        int side = 100;
        *square << QPoint(-side, -side) << QPoint(-side, side)
                 << QPoint(side, side) << QPoint(side, -side);
    }
    
    void container::paintEvent(QPaintEvent*)
    {
        drawSquare();
    }
    
    void container::drawSquare()
    {
        QPainter painter(this);
        painter.translate(this->width()/2, this->height()/2);
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::green);
        painter.drawPolygon(*square);
    }
    
    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 18 May 2022, 17:09 last edited by JoeCFD
      #2

      layout = new QGridLayout();
      layout->setContentsMargins( 0, 40, 0, 0 );
      .....
      layout->addWidget(label, 0, 0, Qt::AlignTop);

      A 1 Reply Last reply 18 May 2022, 17:50
      0
      • J JoeCFD
        18 May 2022, 17:09

        layout = new QGridLayout();
        layout->setContentsMargins( 0, 40, 0, 0 );
        .....
        layout->addWidget(label, 0, 0, Qt::AlignTop);

        A Offline
        A Offline
        Aidan34
        wrote on 18 May 2022, 17:50 last edited by
        #3

        @JoeCFD That in combination with setMaximumSize seems to work, thanks!

        1 Reply Last reply
        0

        1/3

        18 May 2022, 16:40

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved