How to ensure that a Widget QPainter changes size when the parent does?
-
Hello to everyone, I'm having a hard time figuring out how to achieve the funcionality i want. Here is the problem:
I have a window where, when a button is clicked, a custom class I made called 'LoadingOverlay' and inherits from QWidget is displayed, it holds a loading gif and a Label that indicates the user that it has to wait. This works perfectly, but now I found out that when I resize the window, I get that both the gif label and the text label remain in the original place (they do not readjust to the center of the window), and only the original size of the window is painted. Here I show an example:
This is the window working correctly, with no resizes made and with the original size I set up.
And this happens when I resize the window:
Also, I found out that If I change the size of the window before creating the LoadingOverlay object, it paints the new size but the Labels remain in the same place where no resize was made, which seems odd to me. Here is an example:
Window resized before:
Resize during LoadingOverlay object lifetime:
Sorry if I'm being unable to express myself correctly, English is not my first language.
Does anyone know how to solve this?Here I add my implementation of my class "LoadingOverlay":
LoadingOverlay::LoadingOverlay(QWidget *parent) : QWidget(parent) { setGeometry(parent->rect()); setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground, true); // Make the background translucent setAttribute(Qt::WA_NoSystemBackground); // Make the widget transparent setMinimumSize( QSize(200,150) ); // Set up loading animation loadingAnimation = new QLabel(this); loadingAnimation->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); loadingAnimation->setMinimumSize( QSize(120,120) ); loadingAnimation->setAlignment(Qt::AlignCenter); loadingAnimation->setStyleSheet("QLabel { background-color:transparent; }"); movie = new QMovie(":/images/Resources/loading-gif.gif"); loadingAnimation->setMovie(movie); movie->start(); // Set up loading text loadingText = new QLabel(tr("Espere..."), this); loadingText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); loadingText->setMinimumSize( QSize(150,60) ); loadingText->setAlignment(Qt::AlignCenter); loadingText->setStyleSheet("QLabel { background-color:transparent; color: #304A8D; font: bold 22px; }"); // Set layout QVBoxLayout *layout = new QVBoxLayout(this); layout->addStretch(1); layout->addWidget(loadingAnimation, 0, Qt::AlignCenter); layout->addSpacing(15); layout->addWidget(loadingText, 0, Qt::AlignCenter); layout->addStretch(1); setLayout(layout); } LoadingOverlay::~LoadingOverlay() { if (movie) movie->stop(); delete movie; } void LoadingOverlay::resizeEvent(QResizeEvent *event) { Q_UNUSED(event); setGeometry(parentWidget()->rect()); } void LoadingOverlay::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.setOpacity(0.6); painter.fillRect(rect(), Qt::white); // Fill the background with white color }
As you can see, I've tried overriding the 'resizeEvent' with no luck, I've also tried adding in this same method calls to 'update()' and 'adjustSize()', but that only made things worse.
Thank you all in advance.
-
Hello to everyone, I'm having a hard time figuring out how to achieve the funcionality i want. Here is the problem:
I have a window where, when a button is clicked, a custom class I made called 'LoadingOverlay' and inherits from QWidget is displayed, it holds a loading gif and a Label that indicates the user that it has to wait. This works perfectly, but now I found out that when I resize the window, I get that both the gif label and the text label remain in the original place (they do not readjust to the center of the window), and only the original size of the window is painted. Here I show an example:
This is the window working correctly, with no resizes made and with the original size I set up.
And this happens when I resize the window:
Also, I found out that If I change the size of the window before creating the LoadingOverlay object, it paints the new size but the Labels remain in the same place where no resize was made, which seems odd to me. Here is an example:
Window resized before:
Resize during LoadingOverlay object lifetime:
Sorry if I'm being unable to express myself correctly, English is not my first language.
Does anyone know how to solve this?Here I add my implementation of my class "LoadingOverlay":
LoadingOverlay::LoadingOverlay(QWidget *parent) : QWidget(parent) { setGeometry(parent->rect()); setWindowFlags(Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground, true); // Make the background translucent setAttribute(Qt::WA_NoSystemBackground); // Make the widget transparent setMinimumSize( QSize(200,150) ); // Set up loading animation loadingAnimation = new QLabel(this); loadingAnimation->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); loadingAnimation->setMinimumSize( QSize(120,120) ); loadingAnimation->setAlignment(Qt::AlignCenter); loadingAnimation->setStyleSheet("QLabel { background-color:transparent; }"); movie = new QMovie(":/images/Resources/loading-gif.gif"); loadingAnimation->setMovie(movie); movie->start(); // Set up loading text loadingText = new QLabel(tr("Espere..."), this); loadingText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); loadingText->setMinimumSize( QSize(150,60) ); loadingText->setAlignment(Qt::AlignCenter); loadingText->setStyleSheet("QLabel { background-color:transparent; color: #304A8D; font: bold 22px; }"); // Set layout QVBoxLayout *layout = new QVBoxLayout(this); layout->addStretch(1); layout->addWidget(loadingAnimation, 0, Qt::AlignCenter); layout->addSpacing(15); layout->addWidget(loadingText, 0, Qt::AlignCenter); layout->addStretch(1); setLayout(layout); } LoadingOverlay::~LoadingOverlay() { if (movie) movie->stop(); delete movie; } void LoadingOverlay::resizeEvent(QResizeEvent *event) { Q_UNUSED(event); setGeometry(parentWidget()->rect()); } void LoadingOverlay::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.setOpacity(0.6); painter.fillRect(rect(), Qt::white); // Fill the background with white color }
As you can see, I've tried overriding the 'resizeEvent' with no luck, I've also tried adding in this same method calls to 'update()' and 'adjustSize()', but that only made things worse.
Thank you all in advance.
-
S superg has marked this topic as solved on
-
@superg You need to resize the overlay widget in its parent widget's resizeEvent, not itself.