How to create a square QFrame?
-
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. -
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.@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; } }
-
@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; } }
@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-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@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
-
@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
@raven-worx That was quick! But ... no change.
-
@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; } }
@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; };
-
@raven-worx That was quick! But ... no change.
@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) -
@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)@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.