需要来自 QWidget 的完成信号之类的东西

发布于 2024-12-12 04:01:25 字数 178 浏览 0 评论 0原文

我正在寻找来自 QDialog 的完成信号之类的东西,仅适用于 QWidget。原因是,一旦小部件弹出(这根本不是问题),我就禁用工具栏,并且我希望在小部件关闭后再次启用工具栏。

我也无法覆盖该小部件的 close-Event,因为那样我们就会在业务类中拥有 GUI 代码。

I'm searching for something like the finished-signal from QDialog, only for QWidget. The reason is, I disable my toolbar once the widget pops up (which isn't a problem at all) and I want the toolbar to be enabled again, once the widget is closed.

I also can't override the close-Event of that widget, because then we would have GUI-code in business-classes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走野 2024-12-19 04:01:25

您可以将小部件设置为在关闭时删除,然后监听其destroyed信号:

widget->setAttribute( Qt::WA_DeleteOnClose );
connect( widget, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed(QObject*)) );

不过,只有当您对小部件内容不感兴趣时​​,这才有效。在发出 destroyed() 时,小部件不再是 QWidget,而只是一个 QObject (如 destroyed() 是从 ~QObject 发出的),因此您无法再将参数 QObject* 转换为 QWidget

一个简单的替代方案可能是用 QDialog 包装您的小部件。

You can set the widget to be deleted on close, and then listen to its destroyed signal:

widget->setAttribute( Qt::WA_DeleteOnClose );
connect( widget, SIGNAL(destroyed(QObject*)), this, SLOT(widgetDestroyed(QObject*)) );

That only works if you're not interested in the widget contents though. At the point destroyed() is emitted, the widget isn't a QWidget anymore, just a QObject (as destroyed() is emitted from ~QObject), so you can't cast the argument QObject* to QWidget anymore.

A simple alternative might be to wrap your widget with a QDialog.

世态炎凉 2024-12-19 04:01:25

在您的 Widget 类中,您可以添加其他人可以连接到的自己的信号。然后重写 closeEvent() 方法。不用担心重写这个方法,这种情况正是这样做的正确理由。

class MyCustomWidget: public QWidget
{
   Q_OBJECT

    ...

    signals:
       void WidgetClosed();

   protected:

     //===============================================================
     // Summary: Overrides the Widget close event
     //  Allows local processing before the window is allowed to close.
     //===============================================================
     void closeEvent(QCloseEvent *event);

    }

closeEvent 方法中触发您的信号:

void MyCustomWidget::closeEvent(QCloseEvent *event)
{
      emit WidgetClosed();
      event->accept();
}

In your Widget class, you can add your own signal that others can connect to. Then override the closeEvent() method. Don't worry about overriding this method, this kind of situation is exactly the right reason do to it.

class MyCustomWidget: public QWidget
{
   Q_OBJECT

    ...

    signals:
       void WidgetClosed();

   protected:

     //===============================================================
     // Summary: Overrides the Widget close event
     //  Allows local processing before the window is allowed to close.
     //===============================================================
     void closeEvent(QCloseEvent *event);

    }

In the closeEvent method trigger your signal:

void MyCustomWidget::closeEvent(QCloseEvent *event)
{
      emit WidgetClosed();
      event->accept();
}
久光 2024-12-19 04:01:25

QWidget 实际上没有很多信号,根据文档,它总共有 2 个。但是,这并不意味着您不能自己指定信号并使用它,这可能是最好的方法。

QWidget doesn't have many signals really, according to the documentation it has a grand total of 2. However, that doesn't mean you can't specify a signal yourself and use it, which is probably the best method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文