Solved Centering Qlabel on screen
-
@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. -
@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?
-
Hi
Just a small note.
QLabel is left aligned pr default.
So make sure change its set to center -
@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 centerThis 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. -
@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. -
@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. -
@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. -
@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.
-
@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:
-
@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:
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]
-
Hi
just a note
Who is going to delete movie?
QMovie* movie = new QMoviehttp://doc.qt.io/qt-5/qlabel.html#setMovie
states that label do not take ownership. -
@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?