【发布时间】:2017-01-06 12:04:54
【问题描述】:
为了在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() 我的小部件时,我无法触发它...
我的意思是代码没有效果,并且在放置断点时,它不会停止,所以我假设事件没有被触发。
我做错了什么?
编辑 - 添加更多代码和精度:
我正在使用 QtCreator 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。
【问题讨论】:
-
你怎么称呼这个小部件的
show? -
为了让
showEvent被调用,您只需要按照您所说所做的操作。这是minimal example。如果您通过放置断点进行测试,则可能您尚未将调试器附加到您的应用程序。如果您使用的是 Qt Creator,则需要使用“开始调试”按钮F5(而不是“运行”按钮ctrl + R)。 -
@Jonathan Mee:直接从拥有此小部件的另一个小部件调用
show()插槽 -
@Mike:是的,谢谢你的提示,现在使用 QtCreator 几个月了@工作,这正是我一直在做的事情。看到这种奇怪的行为,我以为我错过了由于我现在的反应而看不到的东西^^我会看看这个例子。对我来说真的很奇怪,我可以调试主窗口的 closeEvent 但 showEvent 仍然不起作用。难道是因为一些eventFilter?
-
我的项目似乎有问题或类似的问题,因为我刚刚在一个小的新项目中测试了这个想法,它的工作就像一个魅力。当我发现问题所在时,我会发布解决方案。感谢您的耐心等待。