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. QWidget control
QtWS25 Last Chance

QWidget control

Scheduled Pinned Locked Moved Solved General and Desktop
qwidget
6 Posts 2 Posters 391 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.
  • D Offline
    D Offline
    dan1973
    wrote on 5 Mar 2024, 12:04 last edited by
    #1

    i need to resize my QPushButton to same as my parent QWidget window?

    Any idea?

    I had been using this code below:

    b1 = new QPushButton("Hello", this);
        b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        b1->resize(this->size());
        b1->show();
    
    J D 2 Replies Last reply 5 Mar 2024, 12:11
    0
    • D dan1973
      5 Mar 2024, 12:21

      @JonB
      hi,

      i had used this code in QWidget Constructor as given below:

      Widget::Widget(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::Widget)
      {
          ui->setupUi(this);
      
          b1 = new QPushButton("Hello", this);
          b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          b1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
          b1->resize(this->size());
          b1->show();
      }
      
      I now found a solution for my requirement:
      i had used override function of resizeEvent of QWidget and it worked:
      
      

      void Widget::resizeEvent(QResizeEvent *event)
      {
      // qDebug() << event->oldSize();
      b1->resize(event->size());
      QWidget::resizeEvent(event);
      }

      Thank you for being with me.:)

      D Offline
      D Offline
      dan1973
      wrote on 5 Mar 2024, 12:22 last edited by
      #5

      @dan1973

      My new updated code:

      Widget::Widget(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::Widget)
      {
          ui->setupUi(this);
      
          b1 = new QPushButton("Hello", this);
          b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          b1->resize(this->size());
          b1->show();
      }
      
      Widget::~Widget()
      {
          delete ui;
      }
      
      void Widget::resizeEvent(QResizeEvent *event)
      {
      //    qDebug() << event->oldSize();
          b1->resize(event->size());
          QWidget::resizeEvent(event);
      }
      
      
      J 1 Reply Last reply 5 Mar 2024, 12:45
      0
      • D dan1973
        5 Mar 2024, 12:04

        i need to resize my QPushButton to same as my parent QWidget window?

        Any idea?

        I had been using this code below:

        b1 = new QPushButton("Hello", this);
            b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            b1->resize(this->size());
            b1->show();
        
        J Online
        J Online
        JonB
        wrote on 5 Mar 2024, 12:11 last edited by
        #2

        @dan1973
        When are you calling this code? Has this been fully shown by the time the code is called? Check what this->size() is returning when this code is executed: it will not be correct if you do this e.g. in the constructor.

        D 1 Reply Last reply 5 Mar 2024, 12:21
        2
        • D dan1973
          5 Mar 2024, 12:04

          i need to resize my QPushButton to same as my parent QWidget window?

          Any idea?

          I had been using this code below:

          b1 = new QPushButton("Hello", this);
              b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
              b1->resize(this->size());
              b1->show();
          
          D Offline
          D Offline
          dan1973
          wrote on 5 Mar 2024, 12:19 last edited by
          #3

          @dan1973

          1 Reply Last reply
          0
          • J JonB
            5 Mar 2024, 12:11

            @dan1973
            When are you calling this code? Has this been fully shown by the time the code is called? Check what this->size() is returning when this code is executed: it will not be correct if you do this e.g. in the constructor.

            D Offline
            D Offline
            dan1973
            wrote on 5 Mar 2024, 12:21 last edited by
            #4

            @JonB
            hi,

            i had used this code in QWidget Constructor as given below:

            Widget::Widget(QWidget *parent)
                : QWidget(parent)
                , ui(new Ui::Widget)
            {
                ui->setupUi(this);
            
                b1 = new QPushButton("Hello", this);
                b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                b1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
                b1->resize(this->size());
                b1->show();
            }
            
            I now found a solution for my requirement:
            i had used override function of resizeEvent of QWidget and it worked:
            
            

            void Widget::resizeEvent(QResizeEvent *event)
            {
            // qDebug() << event->oldSize();
            b1->resize(event->size());
            QWidget::resizeEvent(event);
            }

            Thank you for being with me.:)

            D 1 Reply Last reply 5 Mar 2024, 12:22
            0
            • D dan1973
              5 Mar 2024, 12:21

              @JonB
              hi,

              i had used this code in QWidget Constructor as given below:

              Widget::Widget(QWidget *parent)
                  : QWidget(parent)
                  , ui(new Ui::Widget)
              {
                  ui->setupUi(this);
              
                  b1 = new QPushButton("Hello", this);
                  b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  b1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
                  b1->resize(this->size());
                  b1->show();
              }
              
              I now found a solution for my requirement:
              i had used override function of resizeEvent of QWidget and it worked:
              
              

              void Widget::resizeEvent(QResizeEvent *event)
              {
              // qDebug() << event->oldSize();
              b1->resize(event->size());
              QWidget::resizeEvent(event);
              }

              Thank you for being with me.:)

              D Offline
              D Offline
              dan1973
              wrote on 5 Mar 2024, 12:22 last edited by
              #5

              @dan1973

              My new updated code:

              Widget::Widget(QWidget *parent)
                  : QWidget(parent)
                  , ui(new Ui::Widget)
              {
                  ui->setupUi(this);
              
                  b1 = new QPushButton("Hello", this);
                  b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  b1->resize(this->size());
                  b1->show();
              }
              
              Widget::~Widget()
              {
                  delete ui;
              }
              
              void Widget::resizeEvent(QResizeEvent *event)
              {
              //    qDebug() << event->oldSize();
                  b1->resize(event->size());
                  QWidget::resizeEvent(event);
              }
              
              
              J 1 Reply Last reply 5 Mar 2024, 12:45
              0
              • D dan1973 has marked this topic as solved on 5 Mar 2024, 12:22
              • D dan1973
                5 Mar 2024, 12:22

                @dan1973

                My new updated code:

                Widget::Widget(QWidget *parent)
                    : QWidget(parent)
                    , ui(new Ui::Widget)
                {
                    ui->setupUi(this);
                
                    b1 = new QPushButton("Hello", this);
                    b1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                    b1->resize(this->size());
                    b1->show();
                }
                
                Widget::~Widget()
                {
                    delete ui;
                }
                
                void Widget::resizeEvent(QResizeEvent *event)
                {
                //    qDebug() << event->oldSize();
                    b1->resize(event->size());
                    QWidget::resizeEvent(event);
                }
                
                
                J Online
                J Online
                JonB
                wrote on 5 Mar 2024, 12:45 last edited by JonB 3 May 2024, 12:45
                #6

                @dan1973 said in QWidget control:

                b1->resize(this->size());

                I wouldn't even bother doing this in the constructor --- did you qDebug() << this->size() there like I suggested? But up to you, I guess it does no harm, it will get overwritten by call to Widget::resizeEvent() as soon as shown, I think.

                1 Reply Last reply
                0

                2/6

                5 Mar 2024, 12:11

                4 unread
                • Login

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