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 reports incorrect width value
Forum Updated to NodeBB v4.3 + New Features

qwidget reports incorrect width value

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.6k Views 2 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #1

    i have such a class

    class MyWidget : public QWidget
    {
    /// ...
    };
    

    then i add widgets to a hbox layout, and set thay layout to MyWidget object.
    i want MyWidget widget to have the width of a widget contained in it.
    so i overrode size hint:

    QSize MyWidget::sizeHint() const
    {
        // use the width of the first widget in the frame
        if (auto pLayout = layout())
        {
            auto pItem = pLayout->itemAt(0);
            //std::cout << pItem->widget()->width() << std::endl;
            if (pItem && pItem->widget())
                return QSize{ pItem->widget()->width(), QWidget::sizeHint().height() };
        }
        return QWidget::sizeHint();
    }
    

    now, pItem->widget()->width() always reports the same value no matter what size the widget is. what's the problem???

    mrjjM 1 Reply Last reply
    0
    • U user4592357

      i have such a class

      class MyWidget : public QWidget
      {
      /// ...
      };
      

      then i add widgets to a hbox layout, and set thay layout to MyWidget object.
      i want MyWidget widget to have the width of a widget contained in it.
      so i overrode size hint:

      QSize MyWidget::sizeHint() const
      {
          // use the width of the first widget in the frame
          if (auto pLayout = layout())
          {
              auto pItem = pLayout->itemAt(0);
              //std::cout << pItem->widget()->width() << std::endl;
              if (pItem && pItem->widget())
                  return QSize{ pItem->widget()->width(), QWidget::sizeHint().height() };
          }
          return QWidget::sizeHint();
      }
      

      now, pItem->widget()->width() always reports the same value no matter what size the widget is. what's the problem???

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @user4592357
      Hi
      If you call pItem->widget()->width() while the inner widget
      is not visible, it might not report actual size. ( layout are not calculated yet )
      You can try use the ShowEvent instead or
      use
      layout->invalidate();
      layout->activate();
      to try to force layout to be calculated before shown.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        this didn't work, i get the same result:

        QSize MyWidget::sizeHint() const
        {
        	// use the width of the first widget in the frame
        	if (auto pLayout = layout())
        	{
        		auto pItem = pLayout->itemAt(0);
        		//std::cout << pItem->widget()->sizeHint().width() << std::endl;
        		if (pItem && pItem->widget())
        		{
        			pLayout->invalidate();
        			pLayout->activate();
        			return QSize{ pItem->widget()->width(), QWidget::sizeHint().height() };
        		}
        	}
        	return QWidget::sizeHint();
        }
        

        and i don't get how i'd use the inner widget sizes in showEvent()

        mrjjM 1 Reply Last reply
        0
        • U user4592357

          this didn't work, i get the same result:

          QSize MyWidget::sizeHint() const
          {
          	// use the width of the first widget in the frame
          	if (auto pLayout = layout())
          	{
          		auto pItem = pLayout->itemAt(0);
          		//std::cout << pItem->widget()->sizeHint().width() << std::endl;
          		if (pItem && pItem->widget())
          		{
          			pLayout->invalidate();
          			pLayout->activate();
          			return QSize{ pItem->widget()->width(), QWidget::sizeHint().height() };
          		}
          	}
          	return QWidget::sizeHint();
          }
          

          and i don't get how i'd use the inner widget sizes in showEvent()

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          Hi
          Hmm ok. I did something similar and that worked but i cant spot the difference.
          Well try
          pItem->widget()->width()
          in ShowEvent to see if that even report expected size.

          Also if MyWidget is a layout, then layout set size of it.
          Unless you set MinimumSize for MyWidget
          It will ignore resize / set Geometry.
          Also sizeHint is just suggested size but if innner widget is also in a layout, then
          om not sure this logic can even work. ( as inner widget then follow outer widget)
          So something about this setup confuses me.

          1 Reply Last reply
          0
          • U Offline
            U Offline
            user4592357
            wrote on last edited by user4592357
            #5

            yes, inner widget has min size set
            and in showEvent inner widget still has the same size as before.

            mrjjM 1 Reply Last reply
            0
            • U user4592357

              yes, inner widget has min size set
              and in showEvent inner widget still has the same size as before.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @user4592357
              So it does not report the minimumSize for the inner widget?

              1 Reply Last reply
              0
              • U Offline
                U Offline
                user4592357
                wrote on last edited by user4592357
                #7

                no, it reports a little big larger, like 12px larger.
                also, i have set setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

                p.s. i solved this another way. thanks for helping

                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