Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QWidget::heightForWidth() is not called

Tags:

c++

qt

I want to make my widget always have square size. Following this answer, I have overridden QWidget::heightForWidth(), and I also call setHeightForWidth(true) in the constructor, as suggested by @peppe. The size policy is set to Preferred,Preferred (for both horizontal size and vertical size).

However, heightForWidth() is not being called. Is there anything I am doing wrong?

This is the declaration of heightForWidth() in my Widget class:

virtual int heightForWidth(int) const;

This happens on Linux and Windows.

like image 974
sashoalm Avatar asked Jul 17 '13 19:07

sashoalm


2 Answers

Your widget needs to be in a layout. The below works on both Qt 4 and 5.

In Qt 4, it will only force the toplevel window's minimum size if it's in a layout.

In Qt 5, it doesn't force the toplevel window size. There may be a flag for that or it's a bug but I don't recall at the moment.

screenshot

#include <QApplication>
#include <QWidget>
#include <QPainter>
#include <QDebug>
#include <QVBoxLayout>
#include <QFrame>

class Widget : public QWidget {
    mutable int m_ctr;
public:
    Widget(QWidget *parent = 0) : QWidget(parent), m_ctr(0) {
        QSizePolicy p(sizePolicy());
        p.setHeightForWidth(true);
        setSizePolicy(p);
    }
    int heightForWidth(int width) const {
        m_ctr ++;
        QApplication::postEvent(const_cast<Widget*>(this), new QEvent(QEvent::UpdateRequest));
        return qMax(width*2, 100);
    }
    QSize sizeHint() const {
        return QSize(300, heightForWidth(300));
    }
    void paintEvent(QPaintEvent *) {
        QPainter p(this);
        p.drawRect(rect().adjusted(0, 0, -1, -1));
        p.drawText(rect(), QString("h4w called %1 times").arg(m_ctr));
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QVBoxLayout * l = new QVBoxLayout(&w);
    l->addWidget(new Widget);
    QFrame * btm = new QFrame;
    btm->setFrameShape(QFrame::Panel);
    btm->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    l->addWidget(btm);
    w.show();
    return a.exec();
}
like image 69
Kuba hasn't forgotten Monica Avatar answered Nov 01 '22 00:11

Kuba hasn't forgotten Monica


To stay square if the widget is in a layout you must reimplement

bool hasHeightForWidth() const{ return true; }
int heightForWidth(int w) const { return w; }

functions of the layout class.

like image 27
Mattia Avatar answered Oct 31 '22 23:10

Mattia