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.
Forum Updated to NodeBB v4.3 + New Features

Offset QLabel from widget center.

Scheduled Pinned Locked Moved Solved General and Desktop
layout
3 Posts 2 Posters 756 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 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
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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
      0
      • JoeCFDJ JoeCFD

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

        A Offline
        A Offline
        Aidan34
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

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