Solved Centering Qlabel on screen
-
@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?
-
@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);
-
@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
-
@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.
-
@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.