Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to ensure that a Widget QPainter changes size when the parent does?
QtWS25 Last Chance

How to ensure that a Widget QPainter changes size when the parent does?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 245 Views
  • 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.
  • supergS Offline
    supergS Offline
    superg
    wrote on last edited by
    #1

    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.
    original size.png
    And this happens when I resize the window:
    window resized.png

    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:
    size changed previously.png

    Resize during LoadingOverlay object lifetime:
    size changed previously 2.png

    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.

    B 1 Reply Last reply
    0
    • supergS superg

      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.
      original size.png
      And this happens when I resize the window:
      window resized.png

      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:
      size changed previously.png

      Resize during LoadingOverlay object lifetime:
      size changed previously 2.png

      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.

      B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #2

      @superg You need to resize the overlay widget in its parent widget's resizeEvent, not itself.

      supergS 1 Reply Last reply
      1
      • supergS superg has marked this topic as solved on
      • B Bonnie

        @superg You need to resize the overlay widget in its parent widget's resizeEvent, not itself.

        supergS Offline
        supergS Offline
        superg
        wrote on last edited by
        #3

        @Bonnie Thank you, I can't believe I didn't think of that. Now it works like a charm, thanks again!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved