如何调用showEvent()

How to make showEvent() get called?

本文关键字:showEvent 调用 何调用      更新时间:2023-10-16

QWidget派生类中调用showEvent()需要什么?

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
}

当我show()我的小部件时,我无法触发它。。。我的意思是,代码没有效果,当放置断点时,它不会停止,所以我认为事件没有被触发。

我做错了什么?

编辑-添加更多代码和精度:

我使用的是Qt Creator 3.0.0和Qt 5.2.0(MSVC 2010,32位)

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

然后当我按下主窗口上的按钮时

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

让我困惑的是,m_configMenuForm实际上显示在主窗口的顶部,它变得可见并正常工作!只是没有调用showEvent。

我在回答我自己的问题,因为最终这不是编程问题。编译/调试的东西一定出了问题。

记录在案,如果代码中的一切都是正确的,但由于某种奇怪的原因没有调用函数(也许这只能在Qt的事件处理程序重新实现的情况下发生?),该怎么办。

这是使用Qt Creator 3.0.0和Qt 5.2.0 MSVC2010-32位时发生的

  1. 清理项目:菜单构建->全部清理
  2. 关闭Qt呼吸器
  3. 转到您的构建项目/debug文件夹并删除.exe、.pdb和.ilk文件
  4. 转到你的构建项目/缓存文件夹,删除与你的项目同名、后缀为.pdb的文件夹(你的项目.pdb文件夹)-不确定这是否必要,但我做到了,所以我也把它写在这里

  5. 重新启动QtCreator、qmake、构建并运行/debug(以及tadaaa!)

"简单"的清理并没有奏效,甚至连电脑重启都没有。我不得不手动删除QtCreator没有删除的文件。

我希望它能在未来帮助别人,节省几个小时。

要在visual studio中设置断点,请参阅:断点

当您恢复窗口以获取更多信息时调用showEvent()showEvent

代码段:-

#include <QtGui>
#include <iostream>
//Move this class to any header file then exceute
class widget : public QWidget
{
Q_OBJECT
protected :
void showEvent( QShowEvent * event )
{
QWidget::showEvent(event);
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
widget w;
w.show(); //Here showEvent() get called
return app.exec();

}