如何知道小部件当前正在 Qt Designer 中运行

发布于 2024-09-11 05:04:46 字数 739 浏览 1 评论 0原文

我如何在自定义 Qt 小部件的代码中知道它当前已在 Qt 设计器中实例化?

用例:

我构建了一个复杂的自定义小部件,其中包含多个子小部件,例如 QPushButton、QLabel 等。

根据应用程序逻辑要求,创建小部件时,大多数子组件不可见,但在设计时我把它写在我想看到的表格上。

能够在设计时使用样式表。 目前我得到的空只是构造函数的结果 - 最小视图(在我的情况下实际上是空的)。

我正在寻找的是能够做类似的事情

MyQWidget::(QWidget *parent)
{
 ....
   if(isRunningInDesigner())
   {
      myChildWidget1->setVisible(true);
      myChildWidget2->setVisible(true);
      myChildWidget3->setVisible(true);
   }
   else
   {
      myChildWidget1->setVisible(false);
      myChildWidget2->setVisible(false);
      myChildWidget3->setVisible(false);
   }
....
}

那么我应该在这个 bool isRunningInDesigner() 中放入什么?

How can I in code of the custom Qt widget know that it is currently instantiated in Qt designer?

Use case:

I build a complex custom widget that has several child widgets like QPushButton, QLabel etc.

As application logic require, when widget is created most of those sub component are not visible but in design time when I put it on a form I would like to see them.

To be able to play with style sheet at design time.
Currently what I get is a empty is only a result of constructor - minimal view (actually empty in my case).

What I am looking for is to be able to do something like

MyQWidget::(QWidget *parent)
{
 ....
   if(isRunningInDesigner())
   {
      myChildWidget1->setVisible(true);
      myChildWidget2->setVisible(true);
      myChildWidget3->setVisible(true);
   }
   else
   {
      myChildWidget1->setVisible(false);
      myChildWidget2->setVisible(false);
      myChildWidget3->setVisible(false);
   }
....
}

So what should I put in to this bool isRunningInDesigner() ?

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

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

发布评论

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

评论(1

云醉月微眠 2024-09-18 05:04:46

来自 Qt 设计器手册< /a>:

要在 Qt Designer 中为自定义小部件提供特殊行为,请提供 initialize() 函数的实现,以配置 Qt Designer 特定行为的小部件构造过程。此函数将在调用 createWidget() 之前首次调用,并且可能会设置一个内部标志,稍后当 Qt Designer 调用插件的 createWidget() 时可以对其进行测试> 功能。

这些是来自 QDesignerCustomWidgetInterface 插件接口的方法。简而言之:当 Qt Designer 要求您的插件创建自定义小部件的实例时,您告诉小部件以不同的方式表现。

From the Qt Designer manual:

To give custom widgets special behavior in Qt Designer, provide an implementation of the initialize() function to configure the widget construction process for Qt Designer specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when Qt Designer calls the plugin’s createWidget() function.

Those are methods from the QDesignerCustomWidgetInterface plugin interface. In short: you tell the widget to behave differently when Qt Designer asks your plugin to create instances of your custom widget.

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