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. Force aspect ratio on window
Forum Updated to NodeBB v4.3 + New Features

Force aspect ratio on window

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 290 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.
  • G Offline
    G Offline
    Grand_Titan
    wrote on last edited by
    #1

    I have a second dialog style window which is started in mainwindow.cpp like this:
    externalDisplay = new ExternalDisplay(this);
    externalDisplay->show();
    I want to allow resizing that window but the aspect ratio must not change. I tried using event filters like this:

    ExternalDisplay::ExternalDisplay(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::ExternalDisplay)
    {
        ui->setupUi(this);
    
        this->installEventFilter(this);
    }
    
    bool ExternalDisplay::eventFilter(QObject* obj, QEvent* event) {
        if (event->type() == QEvent::Resize) {
            QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
            qDebug() << "Window resized to: " << resizeEvent->size();
    
            int newWidth = resizeEvent->size().width();
            int newHeight = (5 * newWidth) / 7;
    
            this->resize(newWidth, newHeight);
        }
        return false;
    }
    

    I want the aspect ratio to be 7/5 but with this solution the window is flickering because it keeps changing between the users input and the forced aspect ratio. Is there a better solution?

    S 1 Reply Last reply
    0
    • G Grand_Titan

      I have a second dialog style window which is started in mainwindow.cpp like this:
      externalDisplay = new ExternalDisplay(this);
      externalDisplay->show();
      I want to allow resizing that window but the aspect ratio must not change. I tried using event filters like this:

      ExternalDisplay::ExternalDisplay(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::ExternalDisplay)
      {
          ui->setupUi(this);
      
          this->installEventFilter(this);
      }
      
      bool ExternalDisplay::eventFilter(QObject* obj, QEvent* event) {
          if (event->type() == QEvent::Resize) {
              QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
              qDebug() << "Window resized to: " << resizeEvent->size();
      
              int newWidth = resizeEvent->size().width();
              int newHeight = (5 * newWidth) / 7;
      
              this->resize(newWidth, newHeight);
          }
          return false;
      }
      

      I want the aspect ratio to be 7/5 but with this solution the window is flickering because it keeps changing between the users input and the forced aspect ratio. Is there a better solution?

      S Offline
      S Offline
      SamiV123
      wrote on last edited by
      #2

      @Grand_Titan

      all I think off is actually not resize the window while the user is resizing it but visualize it only and only resize the window/dialog once after the resize operation stops.

      G 1 Reply Last reply
      0
      • S SamiV123

        @Grand_Titan

        all I think off is actually not resize the window while the user is resizing it but visualize it only and only resize the window/dialog once after the resize operation stops.

        G Offline
        G Offline
        Grand_Titan
        wrote on last edited by
        #3

        @SamiV123 Is there no built in feature for this?

        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