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 create a square QFrame?
Forum Update on Monday, May 27th 2025

How to create a square QFrame?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.5k 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.
  • J Offline
    J Offline
    jmoellers
    wrote on last edited by
    #1

    I'd like to write a program that displays data received from an MQTT broker, like the Android "MQTT dash". I'm willing to fiddle around with font sizes as long as the overall appearance is regular.
    For this, I'd like to populate a QGridLayout with square QFrames that hold a QVBoxLayout with a title (eg "Office T" or "Garage T") and a value (eg "25.2°" or "26.7°"), both QLabels.
    Any idea how I could create square QFrames or how I could have square QFrames in the end?
    At present, the "tile"s are not square and the rows are of different widths.

    raven-worxR 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      You should use a QTableWidget with a custom QStyledItemDelegate subclass instead of an unmanageable layout. QStyledItemDelegate::sizeHint determines the sizes of an item

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • J jmoellers

        I'd like to write a program that displays data received from an MQTT broker, like the Android "MQTT dash". I'm willing to fiddle around with font sizes as long as the overall appearance is regular.
        For this, I'd like to populate a QGridLayout with square QFrames that hold a QVBoxLayout with a title (eg "Office T" or "Garage T") and a value (eg "25.2°" or "26.7°"), both QLabels.
        Any idea how I could create square QFrames or how I could have square QFrames in the end?
        At present, the "tile"s are not square and the rows are of different widths.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @jmoellers
        only works inside QLayouts:

        class MySquareFrame : public QFrame {
            Q_OBJECT
        
        public:
            MySquareFrame(QWidget* parent  = Q_NULLPTR)
                : QFrame(parent)
            {
                QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Preferred );
                    sizePolicy.setHeightForWidth( true );
                this->setSizePolicy( sizePolicy );
            }
        
           virtual int heightForWidth(int w) const Q_DECL_OVERRIDE
           {
               return w;
           }
        }
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        J J.HilkJ 2 Replies Last reply
        3
        • raven-worxR raven-worx

          @jmoellers
          only works inside QLayouts:

          class MySquareFrame : public QFrame {
              Q_OBJECT
          
          public:
              MySquareFrame(QWidget* parent  = Q_NULLPTR)
                  : QFrame(parent)
              {
                  QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Preferred );
                      sizePolicy.setHeightForWidth( true );
                  this->setSizePolicy( sizePolicy );
              }
          
             virtual int heightForWidth(int w) const Q_DECL_OVERRIDE
             {
                 return w;
             }
          }
          
          J Offline
          J Offline
          jmoellers
          wrote on last edited by
          #4

          @raven-worx BTDTNT
          I implemented your proposal (as class SquareFrame entirely inside a .h file).
          I then replaced the QFrame in my definition of the tile by SquareFrame:

          class tile : public mosqpp::mosquittopp, public SquareFrame // QFrame
          

          But still the tiles have varying sizes and are not square.
          I've uploaded the code to github:
          https://github.com/jmoellers/mqtt-dashboard

          raven-worxR 1 Reply Last reply
          0
          • J jmoellers

            @raven-worx BTDTNT
            I implemented your proposal (as class SquareFrame entirely inside a .h file).
            I then replaced the QFrame in my definition of the tile by SquareFrame:

            class tile : public mosqpp::mosquittopp, public SquareFrame // QFrame
            

            But still the tiles have varying sizes and are not square.
            I've uploaded the code to github:
            https://github.com/jmoellers/mqtt-dashboard

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @jmoellers
            can you try the same code but with the following QSizePolicy initialization:
            QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Fixed)

            .

            class tile : public mosqpp::mosquittopp, public SquareFrame // QFrame

            also note, that QObject derived types should be noted first, before any other deriving types

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            J 1 Reply Last reply
            1
            • raven-worxR raven-worx

              @jmoellers
              can you try the same code but with the following QSizePolicy initialization:
              QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Fixed)

              .

              class tile : public mosqpp::mosquittopp, public SquareFrame // QFrame

              also note, that QObject derived types should be noted first, before any other deriving types

              J Offline
              J Offline
              jmoellers
              wrote on last edited by
              #6

              @raven-worx That was quick! But ... no change.

              raven-worxR 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @jmoellers
                only works inside QLayouts:

                class MySquareFrame : public QFrame {
                    Q_OBJECT
                
                public:
                    MySquareFrame(QWidget* parent  = Q_NULLPTR)
                        : QFrame(parent)
                    {
                        QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Preferred );
                            sizePolicy.setHeightForWidth( true );
                        this->setSizePolicy( sizePolicy );
                    }
                
                   virtual int heightForWidth(int w) const Q_DECL_OVERRIDE
                   {
                       return w;
                   }
                }
                
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @raven-worx said in How to create a square QFrame?:

                QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Preferred );
                sizePolicy.setHeightForWidth( true );

                Correct me if I'm wrong here, but afaik setHeightForWidth no longer has any effect. It never worked in my cases at least.

                An excerpt from one of my classes.

                class SquareFrame : public QFrame
                {
                public:
                    explicit squareLabel(QWidget*parent = 0) : QFrame(parent){
                        this->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred);
                    }
                
                    void useHeightForWidth(const bool &use){
                        useHeight = use;
                        if(use)
                            this->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Fixed);
                        else
                            this->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred);
                    }
                
                protected:
                    virtual void resizeEvent(QResizeEvent *event)Q_DECL_OVERRIDE{
                        event->accept();
                        if(!useHeight){
                            this->setMinimumWidth(this->height());
                            this->setMaximumWidth(this->height());
                        }else{
                            this->setMinimumHeight(this->width());
                            this->setMaximumHeight(this->width());
                        }
                        updateGeometry();
                    }
                
                    bool useHeight = false;
                };
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • J jmoellers

                  @raven-worx That was quick! But ... no change.

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by raven-worx
                  #8

                  @jmoellers
                  k, i just tried.

                  It works with either
                  QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding );
                  or
                  QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Ignored );

                  Whatever you prefer.

                  Note: the hasHeightForWidth() method implementation doesn't need to be overridden on the widget class btw (i removed it from the above example)

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  J 1 Reply Last reply
                  3
                  • raven-worxR raven-worx

                    @jmoellers
                    k, i just tried.

                    It works with either
                    QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding );
                    or
                    QSizePolicy sizePolicy (QSizePolicy::Expanding, QSizePolicy::Ignored );

                    Whatever you prefer.

                    Note: the hasHeightForWidth() method implementation doesn't need to be overridden on the widget class btw (i removed it from the above example)

                    J Offline
                    J Offline
                    jmoellers
                    wrote on last edited by
                    #9

                    @raven-worx Thanks. It still doesn't work here, but I think I'll need to ponder over it to understand exactly what's going on and why it doesn't want to do it.

                    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