Aesope
Aesope

Reputation: 465

Qt - Overriding ResizeEvent give multiple calls when showed

I get this strange behavior with a simple reimplementation of ResizeEvent from a standard Widget. After calling show, the resize event is called 2 times. I trace the behavior with breakpoint (see where below "<- - - -") and I got this :

The first call seem coherent. But, is anyone can explain to me why I got a second call with inconsistant values and how to get rid of this second resize event?

class MyClass : public QLabel
{
    Q_OBJECT

public:
    MyClass(QWidget *parent = 0, Qt::WindowFlags f = 0);
   ~MyClass();

protected:
    void resizeEvent(QResizeEvent *event);
};

MyClass::MyClass(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
}

MyClass::~MyClass()
{
}

void MyClass::resizeEvent(QResizeEvent *event)
{
    int a = 0;  //   <- - - - use break point here
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyClass w;
    w.show();
    return a.exec();
}

Upvotes: 4

Views: 3741

Answers (1)

Aesope
Aesope

Reputation: 465

Your last question make me do some tests and I found a solution.

I just add in the constructor :

this->setGeometry(...)  

With that, I got only one resizeEvent call (the first one).

I do not know why Qt make this second call but I can guess is to set an initial geometry.

However, thank you for your time. It help me a lot! :-)

Upvotes: 1

Related Questions