Search code examples
c++qtqwidget

How to make showEvent() get called?


What do I need in order to have showEvent() be called in a QWidget derived class?

ConfigMenuForm.h

//simplified the code of the class declaration
class ConfigMenuForm : public QWidget
{
    Q_OBJECT

public:
    explicit ConfigMenuForm(QWidget *parent = 0);
    ~ConfigMenuForm();

signals:

public slots:

private slots:

protected:
    void showEvent(QShowEvent *event) override; //with or without the override keyword, no change
private:
}

ConfigMenuForm.cpp

//amongst others
void ConfigMenuForm::showEvent(QShowEvent * event)
{
    //do some stuff here - really simple
}

I can't have it triggered when I show() my widget... I mean the code has no effect and when placing a break point, it doesn't stop on it, so I assume the event is not triggered.

What am I doing wrong?

EDIT - adding some more code and precision:

I'm using QtCreator 3.0.0 with Qt 5.2.0 (MSVC 2010, 32 bit)

//creating the widget in the main window's constructor (class Viewer)
// ConfigMenuForm calls hide() in its own constructor
 m_configMenuForm = new ConfigMenuForm(this);

then when I press a button on the main window

void Viewer::ontBConfigClicked()
{
    m_configMenuForm->show();
}

What confuses me is that m_configMenuForm is actually shown on top of the main window, it becomes visible and properly works! It's just that the showEvent is not called.


Solution

  • I'm answering my own question as in the end it was not a programming issue. Something must have gone wrong with the compilation/debug stuff.

    For the record, here is what to do if everything is right in your code but for some freakin' reason a function is not called(maybe it can only happen with Qt's event handlers reimplementations?).

    This happened using QtCreator 3.0.0 with Qt 5.2.0 MSVC2010 - 32 bits

    1. Clean the project: Menu Build->Clean All
    2. Close QtCreator
    3. Go to your build-project/debug folder and remove the .exe, .pdb and .ilk files
    4. Go to your build-project/cache folder and delete the folder with the same name as your project suffixed with .pdb (yourproject.pdb folder) - not sure if this is necessary, but I did it so I write it down here too

    5. Restart QtCreator, qmake, build and run/debug (and tadaaa!)

    A "simple" clean all didn't do the trick, not even a computer restart. I had to manually delete the files that weren't deleted by QtCreator.

    I hope it can help someone in the future, saving a couple of hours.