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. Issue with QLabel alignment properties
Forum Updated to NodeBB v4.3 + New Features

Issue with QLabel alignment properties

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 177 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.
  • C Offline
    C Offline
    CLBSII
    wrote on last edited by
    #1

    Hi everyone,

    I have a stupid issue, but cannot find any solutions for this. I think I may missing some understanding :

    I want to display an image in a Scroll Area, and I want it at the center. I made the following code:

    void MainWindow::setImage(const QImage &newImage)
    {
        scrollArea = new QScrollArea;
        imageLabel = new QLabel;
    
        scrollArea->setWidget(imageLabel);
        scrollArea->setFrameShape(QFrame::NoFrame);
        scrollArea->setStyleSheet("QScrollBar:vertical{width: 50px;}");
    
        ui->horizontalLayout->addWidget(scrollArea);
    
        image = newImage;
        if (image.colorSpace().isValid())
            image.convertToColorSpace(QColorSpace::SRgb);
        imageLabel->setPixmap(QPixmap::fromImage(image));
    
        imageLabel->adjustSize();
        imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    }
    

    It works, but the problem is that the image contained in the label remain aligned to the left, regardless of the setAlignment(Qt::AlignHCenter | Qt::AlignVCenter) instruction.

    Any ideas why ?

    PS: I can't use QtCreator to change it.

    Pl45m4P 1 Reply Last reply
    0
    • C CLBSII

      Hi everyone,

      I have a stupid issue, but cannot find any solutions for this. I think I may missing some understanding :

      I want to display an image in a Scroll Area, and I want it at the center. I made the following code:

      void MainWindow::setImage(const QImage &newImage)
      {
          scrollArea = new QScrollArea;
          imageLabel = new QLabel;
      
          scrollArea->setWidget(imageLabel);
          scrollArea->setFrameShape(QFrame::NoFrame);
          scrollArea->setStyleSheet("QScrollBar:vertical{width: 50px;}");
      
          ui->horizontalLayout->addWidget(scrollArea);
      
          image = newImage;
          if (image.colorSpace().isValid())
              image.convertToColorSpace(QColorSpace::SRgb);
          imageLabel->setPixmap(QPixmap::fromImage(image));
      
          imageLabel->adjustSize();
          imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
      }
      

      It works, but the problem is that the image contained in the label remain aligned to the left, regardless of the setAlignment(Qt::AlignHCenter | Qt::AlignVCenter) instruction.

      Any ideas why ?

      PS: I can't use QtCreator to change it.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @CLBSII said in Issue with QLabel alignment properties:

      imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

      You've set the alignment to your label, not to your QScrollArea

      • https://doc.qt.io/qt-5/qscrollarea.html#alignment-prop

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2
      • C Offline
        C Offline
        CLBSII
        wrote on last edited by
        #3

        Thank you very much, it works.

        Pl45m4P 1 Reply Last reply
        0
        • C CLBSII

          Thank you very much, it works.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @CLBSII

          You're welcome. What you did is, you centered the pixmap on your QLabel, but the label itself still had the QScrollArea- widget default alignment, which is topLeft :)


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          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