Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Issue with QLabel alignment properties

    General and Desktop
    2
    4
    38
    Loading More Posts
    • 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
      CLBSII last edited by

      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.

      Pl45m4 1 Reply Last reply Reply Quote 0
      • Pl45m4
        Pl45m4 @CLBSII last edited by

        @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 Reply Quote 2
        • Pl45m4
          Pl45m4 @CLBSII last edited by

          @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 Reply Quote 2
          • C
            CLBSII last edited by

            Thank you very much, it works.

            Pl45m4 1 Reply Last reply Reply Quote 0
            • Pl45m4
              Pl45m4 @CLBSII last edited by

              @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 Reply Quote 0
              • First post
                Last post