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. QLabel - setAlignment not working on image
Forum Updated to NodeBB v4.3 + New Features

QLabel - setAlignment not working on image

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Hi,

    I'm trying to achieve a simple layout
    [QLabel1 (image)] [QLabel2]
    [Widget here -------------- ]

    The layout works when QLabel1 is displaying some text, but when I show an image in the QLabel using stylesheet, the alignement is all over the place, see screenshot below

    -"AlignLeft example":https://www.dropbox.com/s/i81sg8p8m1v4a47/alignLeftQLabel.png
    -"AlignRight example":https://www.dropbox.com/s/26r7g04po4ip11m/aligntRightQLabel.png

    Code for building the layout :
    @ QVBoxLayout *layout = new QVBoxLayout( this );
    QHBoxLayout *hlayout = new QHBoxLayout();

    hlayout->setSpacing( 5 );
    hlayout->addWidget( d_label_icon );
    hlayout->addWidget( d_label_current_value );
    
    d_label_icon->setText("d");
    d_label_icon->setAlignment(Qt::AlignRight);  // only the text moves right!
    d_label_current_value->setAlignment(Qt::AlignLeft);
    
    layout->setSpacing( 5 );
    layout->addLayout(hlayout);
    layout->addWidget( d_dial );@
    

    If someone know a workaround, i'm stuck with this since a long time, maybe using something else that a QLabel to show the image? I tried multiple layout, grid, etc. all ignores the alignment of the image.
    Image is set with style sheet (d_label_icon->setStyleSheet("image: url(:/image/icon/heart2)");)

    Thanks in advance!


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      messi
      wrote on last edited by
      #2

      Hi Maximus

      for your example I would use QGridLayout.
      Try to lay a grid over your label arrangement.
      E.g. use 5 columns and 3 rows.
      @
      QGridLayout* layout = new QGridLayout;

      layout->addWidget(d_label_icon, 0, 0, 1, 1);
      layout->addWidget(d_label_current_value, 0, 1, 1, 1 );
      layout->addWidget(d_label_icon2, 0, 3, 1, 1);
      layout->addWidget(d_dial, 1, 2, 2, 3);
      @

      With the following function you fix your gridlayout:

      setColumnStrech
      setColumnMinimumWidth

      With the class QSpacerItem you can keep the labels in the layout in position

      With that info you should solve your problem

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        Thanks, I learned to use a gridLayout and it's working perfectly!

        @ gridLayout->setSpacing( 5 );

        gridLayout->setColumnMinimumWidth(0, 100);
        gridLayout->setColumnMinimumWidth(3, 100);
        gridLayout->addWidget(d_label_icon, 0, 1, 1, 1, Qt::AlignRight);
        gridLayout->addWidget(d_label_current_value, 0, 2, 1, 1, Qt::AlignLeft);
        gridLayout->addWidget(d_dial, 1, 0, 1, 4, Qt::AlignHCenter);@
        

        Free Indoor Cycling Software - https://maximumtrainer.com

        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