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
Qt 6.11 is out! See what's new in the release blog

QWidget control

Scheduled Pinned Locked Moved Solved General and Desktop
qwidget
6 Posts 2 Posters 1.1k Views 1 Watching
  • 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 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();
    
    JonBJ D 2 Replies Last reply
    0
    • D dan1973

      @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 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);
      }
      
      
      JonBJ 1 Reply Last reply
      0
      • D dan1973

        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();
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on 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
        2
        • D dan1973

          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 last edited by
          #3

          @dan1973

          1 Reply Last reply
          0
          • JonBJ JonB

            @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 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
            0
            • D dan1973

              @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 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);
              }
              
              
              JonBJ 1 Reply Last reply
              0
              • D dan1973 has marked this topic as solved on
              • D dan1973

                @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);
                }
                
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #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

                • Login

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