Centering Qlabel on screen



  • Hi,
    I have the following code:

      QMovie* movie = new QMovie(":/images/ajax-loader.gif");
    
      if(!movie->isValid ()) {
        qDebug() << "Movie doesn't work.";
      } else {
        QLabel* label = new QLabel(this);
        label->setGeometry (66, 66, 66, 66);
        label->setMovie (movie);
        movie->start ();
        label->show ();
      }
    
    

    How can I center the QLabel on the screen?
    Thank you.



  • you can use QHBoxLayout


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    How can I center the QLabel on the screen?

    1. Call QApplication::desktop()->screenGeometry() or QApplication::desktop()->availableGeometry() to find the screen dimensions.
    2. Use this information to calculate the center position for your QLabel.
    3. Call label->setGeometry() with your calculated position.

    See http://doc.qt.io/qt-5/qdesktopwidget.html for more details on QDesktopWidget.



  • @Johnson
    Hi,
    Now I have this:

      QLabel* label = new QLabel(this);
      QHBoxLayout* layout = new QHBoxLayout(this);
      layout->setSpacing (0);
      layout->setContentsMargins (0, 0, 0, 0);
      layout->addWidget (label);
      QMovie* movie = new QMovie(":/images/ajax-loader.gif");
    
      if(!movie->isValid ()) {
        qDebug() << "Movie doesn't work.";
      } else {
    
        label->setGeometry (66, 66, 66, 66);
        label->setMovie (movie);
        movie->start ();
        label->show ();
      }
    

    No error message, but the image is not centered. Any suggestions what's wrong? Thank you.



  • @JKSH
    Thank you.
    I tried this:

      QRect screenGeometry = QApplication::desktop ()->screenGeometry ();
      int x = (screenGeometry.width () - label->width ()) / 2;
      int y = (screenGeometry.height () - label->height ()) / 2;
    
      qDebug() << "Screen width: " << screenGeometry.width ();
      qDebug() << "Screen height: " << screenGeometry.height ();
      qDebug() << "x: " << x;
      qDebug() << "y: " << y;
    
     label->setGeometry (x, y, 66, 66);
    

    The label is not centered. what did I do incorrectly?
    Thank you.


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    what did I do incorrectly?

    You printed several qDebug lines, which is good. What does your debug output tell you? Are x and y what you expect?


  • Qt Champions 2016

    Hi
    Just a small note.
    QLabel is left aligned pr default.
    So make sure change its set to center

    alt text


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    QLabel* label = new QLabel(this);

    I just noticed that your QLabel has a parent. That means your QLabel's geometry is relative to its parent.

    @mrjj said in Centering Qlabel on screen:

    QLabel is left aligned pr default.
    So make sure change its set to center

    alt text

    This post made me wonder if I understood the original question correctly.

    @gabor53, are you trying to make QLabel centered on your computer screen? Or are you trying to make the QMovie centered on the QLabel?



  • @JKSH
    I am trying to cener QLabel on the screen.


  • Qt Champions 2016

    @gabor53
    and it NOT about centering the QMovie ? so it plays in the middle?



  • @mrjj
    Yes, you are right. It is about centering the QMovie. The qDebug gives the expected values.


  • Qt Champions 2016

    @gabor53
    Ok, then the Horizontal Alignment should be set to center as else the actual movie
    will always be shown at left side, regardless of how u place the QLabel.


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    The qDebug gives the expected values.

    Please post your qDebug output here.



  • @JKSH
    Screen width: 1280
    Screen height: 768
    x: 320
    y: 144



  • @mrjj
    Please show me an example how to do it in this case. Thank you.


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    @JKSH
    Screen width: 1280
    Screen height: 768
    x: 320
    y: 144

    (320, 144) is very far from the center of 1280 x 768.

    Can you explain why you got x=320 and y=144?

    If your QLabel is 66x66, then you want

    • Global X = (1280-66)/2 = 607
    • Global Y = (768-66)/2 = 351

    Make sure your code calculates the correct x and y values first.


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    Please show me an example how to do it in this case. Thank you.

    It's all in the documentation: http://doc.qt.io/qt-5/qlabel.html#alignment-prop



  • @gabor53 ,

    Put the label in a layout, and set the alignment.

    QMovie* movie = new QMovie(":/images/ajax-loader.gif");
    QVBoxLayout *layout = new QVBoxLayout;
    if(!movie->isValid ()) {
        qDebug() << "Movie doesn't work.";
    } else {
        QLabel* label = new QLabel(this);
        label->setMovie (movie);
        movie->start ();
        label->show ();
        layout->addWidget(label);
        layout->setAlignment(Qt::AlignCenter);
    }
    

    this->setLayout(layout);



  • @Vinod-Kuntoji
    Thank you. This puts the movie in the top left corner:
    0_1509977217031_Capture.JPG



  • @JKSH
    You were right, the calculation was incorrect. The following code puts the movie in the center:

     QLabel* label = new QLabel();
      label->setGeometry (0, 0, 66, 66);
      QRect screenGeometry = QApplication::desktop ()->screenGeometry ();
      int x = (screenGeometry.width () - label->width ()) / 2;
      int y = (screenGeometry.height () - label->height ()) / 2;
    
      QMovie* movie = new QMovie(":/images/ajax-loader.gif");
    
      if(!movie->isValid ()) {
        qDebug() << "Movie doesn't work.";
      } else {
        qDebug() << "Screen width: " << screenGeometry.width ();
        qDebug() << "Screen height: " << screenGeometry.height ();
        qDebug() << "x: " << x;
        qDebug() << "y: " << y;
    
        label->setGeometry (x, y, 66, 66);
        label->setMovie (movie);
    
        movie->start ();
        label->show ();
     }
    

    Now it look like this:
    0_1509977961950_Capture.JPG
    Any way to delete the window controls from the movie?
    Thank you.



  • Oh, I finally figured out what you try to accomplish @gabor53 !

    I have a slightly different solution for you, that way you can use it as anywhere else, later in your code as well.

    #ifndef BUSYANIMATION_H
    #define BUSYANIMATION_H
    
    #include <QLabel>
    
    class BusyAnimation : public QLabel
    {
        Q_OBJECT
    public:
        explicit jhLogo(QWidget *parent = nullptr) : QLabel(parent){
              setWindowFlag(Qt::FramelessWindowHint, true);
              setFixedSize(QSize(66,66));
              QMovie* movie = new QMovie(":/images/ajax-loader.gif");
              setMovie(movie);
              movie->start();
        }
    }
    
    #endif
    
    //Main.cpp
    
    #include <QApplication>
    #include "BusyAnimation.h"
    
    int main(int argc, char *argv[])
    {
        QApplication::setAttribute(Qt::AA_Use96Dpi);
        QApplication a(argc, argv);
    
        BusyAnimation w; 
        w.show();
       
        return a.exec();
    }
    

    [edit: fixed typo in QSize SGaist]


  • Qt Champions 2016

    Hi
    just a note
    Who is going to delete movie?
    QMovie* movie = new QMovie

    http://doc.qt.io/qt-5/qlabel.html#setMovie
    states that label do not take ownership.


  • Moderators

    @J.Hilk said in Centering Qlabel on screen:

    explicit jhLogo(QWidget *parent = nullptr) : QLabel(parent){
    

    "jhLogo" -> "BusyAnimation"

          setFixedSize(QSite(66,66));
    

    "QSite" -> "QSize"

    Or, just use the other overload: QWidget::setFixedSize(int x, int y)

    @mrjj said in Centering Qlabel on screen:

    http://doc.qt.io/qt-5/qlabel.html#setMovie
    states that label do not take ownership.

    Ooh, I didn't know that!

    BusyAnimation needs to delete the QMovie it in its destructor then



  • @J.Hilk
    I started to implement the new class, but ended up with this error message:
    C:\Programming\Quick\Try\Ajax\busyanimation.h:11: error: invalid conversion from 'QObject' to 'QWidget' [-fpermissive]
    explicit BusyAnimation(QObject
    parent = nullptr) : QLabel(parent) {
    ***

    Is there any way to fix this issue?



  • @gabor53 ,

    QMovie* movie = new QMovie(":/images/ajax-loader.gif");
    QVBoxLayout *layout = new QVBoxLayout;
    
    if(!movie->isValid ()) {
        qDebug() << "Movie doesn't work.";
    } else {
        QLabel* label = new QLabel(this);
        label->setMovie (movie);
        movie->start ();
        label->show ();
        layout->addWidget(label);
        layout->setAlignment(Qt::AlignCenter);
    }
    QWidget *central = new QWidget;
    central->setLayout(layout);
    this->setCentralWidget(central);

  • Moderators

    @gabor53
    If you take a look at the documentation (http://doc.qt.io/qt-5/qlabel.html) you will see that QLabel constructor takes a QWidget* as parent, so just change:

    explicit BusyAnimation(QWidget* parent = nullptr) : QLabel(parent) 
    


  • @JKSH said in Centering Qlabel on screen:

    @J.Hilk said in Centering Qlabel on screen:

    explicit jhLogo(QWidget *parent = nullptr) : QLabel(parent){
    

    "jhLogo" -> "BusyAnimation"

    UUUPS, Copy Paste Error 0_1510034437365_Emojee1.png



  • @J.Hilk
    Hi,
    I finally ended p with the following:
    busyanimation.h:

    #ifndef BUSYANIMATION_H
    #define BUSYANIMATION_H
    
    #include "mainwindow.h"
    #include <QObject>
    #include <QWidget>
    #include <QLabel>
    #include <QMovie>
    #include <QDebug>
    
    class BusyAnimation : public QLabel {
      Q_OBJECT
     public:
      explicit BusyAnimation(QWidget* parent = nullptr) ;
    
    };
    
    #endif // BUSYANIMATION_H
    
    

    busyanimaion.cpp:

    #include "busyanimation.h"
    
    
    BusyAnimation::BusyAnimation(QWidget* parent) :  QLabel(parent) {
    
      setWindowFlag (Qt::FramelessWindowHint, true);
      QMainWindow* wndow = new QMainWindow;
      int width = wndow->width ();
      int height = wndow->height ();
      int x = ((width - 66) / 2 - 120) ;
      int y = (height / 2 - 132);
    
      qDebug() << "Window size: " << width << "," << height;
      qDebug() << "x,y: " << x << "," << y;
    
      setFixedSize (66, 66);
      setGeometry (x, y, 66, 66);
      setWindowModality (Qt::WindowModal);
      QMovie* movie = new QMovie(":/images/ajax-loader.gif");
      setMovie (movie);
      movie->start ();
    

    I know this aligns on MainWindow and not the screen. If it aligned to the screen the busyanimation window always underneath MainWinow; I was not able to make it the top window.

    What I don't understand is why

      int x = ((width - 66) / 2 ) ;
      int y = ((height -66)/ 2 );
    

    doesn't come even come close centering BusyAnimation. Please let me know if there is an error in the code. Thank you.



  • @gabor53 Hi,
    ok you're doing a few things wrong here. First of, let's take care of the previously stated memory leak issue

    //in.h
    class BusyAnimation : public QLabel {
      Q_OBJECT
     public:
      explicit BusyAnimation(QWidget* parent = nullptr) ;
    ~BusyAnimation();
    
    private:
        QMovie* movie = Q_NULLPTR;
    };
    
    //in .cpp
    
    BusyAnimation::~BusyAnimation(){
        if(movie)
            movie->deleteLater();
    }
    

    Ok, what ever this, is:

    QMainWindow* wndow = new QMainWindow;
    int width = wndow->width ();
    int height = wndow->height ();

    its defenitly not doing what you want it to do:

    you have to consider 2 situations The Animation has a parent window and the animation has no parent window. Either way, the positioning should be handled by the QWidget that creates BusyAnimation

    In your specific case QMainWindow. You could to it, by taking the width and height from parent but that only works if parent != 0 and does not automatically update with size changes of the parent.

    So, delte this stuff:

    QMainWindow* wndow = new QMainWindow;
      int width = wndow->width ();
      int height = wndow->height ();
      int x = ((width - 66) / 2 - 120) ;
      int y = (height / 2 - 132);
    
      qDebug() << "Window size: " << width << "," << height;
      qDebug() << "x,y: " << x << "," << y;
    
    //...
        setGeometry (x, y, 66, 66);
    

    In your Mainwindow .cpp you handle the position of the busy animation:

    //MainWindow.cpp
    #include "busyanimation.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        resize(500,500); //To give it a decent default size
        
        BusyAnimation *bAnim = new BusyAnimation(this);// create animation
        bAnim->show();//technically only needed when  BusyAnimation has no parent
        bAnim->move((width()-bAnim->width())/2, (height()-bAnim->heigh())/2); //move it to the center of QMainwindow
        connect(qApp, &QApplication::aboutToQuit, bAnim, & BusyAnimation::deleteLater); //Frees Memory at program stop
    }
    

    This will position BusyAnimation in the center of QMainWindow, and stay there, even if you drag the Window around. However it will not stay centered if you resize QMainWindow. You'll have to manually adjust the position for that.

    If you give BusyAnimation no parent, than you'll have to handle the positioning during QMainWindow-drags as well.


  • Moderators

    @gabor53 said in Centering Qlabel on screen:

    QMainWindow* wndow = new QMainWindow;
    int width = wndow->width ();
    int height = wndow->height ();

    Why do you create a NEW main window here?



  • @jsulm
    I thought I ad to.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.