如何处理QDialogBu​​ttonBox RestoreDefaults按钮

发布于 2025-01-02 01:43:19 字数 468 浏览 0 评论 0原文

在 Qt Designer 中,我创建了一个对话框,其中有一个 QDialogBu​​ttonBox,其中包含“确定”、“取消”和“恢复默认值”按钮。花了一段时间,但我弄清楚了如何在我的实现文件中设置信号/槽:

QPushButton* resetButton = m_ui.buttonBox->button(QDialogButtonBox::RestoreDefaults);
connect(resetButton, SIGNAL(clicked()), this, SLOT(resetDefaultsPressed()));

我的问题是:有没有办法在 QtDesigner 中设置信号/槽,就像使用“确定”和“取消”按钮一样?我一直无法找到名为 RestoreDefaults 或类似的信号...我的方法有效,但如果所有信号/槽设置都在 中,那就太好了。 uic 生成的.h 文件。

In Qt Designer I created a dialog box that has a QDialogButtonBox with OK, Cancel, and "Restore Defaults" buttons in it. It took a while, but I figured out how to setup the signals/slots in my implementation file with:

QPushButton* resetButton = m_ui.buttonBox->button(QDialogButtonBox::RestoreDefaults);
connect(resetButton, SIGNAL(clicked()), this, SLOT(resetDefaultsPressed()));

My question is this: Is there a way to setup the signals/slots in QtDesigner like you can with the OK and Cancel buttons? I haven't been able to find a signal called RestoreDefaults or similar... The method I have works, but it would be nice if all the signal/slot setup was together in the .h file that uic generates.

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

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

发布评论

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

评论(2

会发光的星星闪亮亮i 2025-01-09 01:43:19

Qt Designer 只能处理类中声明的信号。

但是您可以编写一个从QDialogBu​​ttonBox派生的新类,使用信号clicked来接收哪个按钮角色被触发,并发出新信号。

然后,您可以将 QDialogBu​​ttonBox 升级到设计器中的新类,并且您必须手动为每个 ui 文件添加信号(左键单击升级的小部件,然后“更改信号/ slot...”),或者编写一个设计器插件,让您的自定义类出现在设计器小部件列表中(请参阅其他回答了解详情)。

Qt Designer can only handle the signals declared in the class.

But you can write a new class derived from QDialogButtonBox, use the signal clicked to receive which button role has been triggered, and emit new signals.

Then you can either promote your QDialogButtonBox to that new class in the designer and you'll have to manually add the signals for each ui file (with a left-click on your promoted widget then "Change signal/slots..."), or write a designer plugin to have your custom class(es) appear in the designer widget list (see that other answer for details).

友谊不毕业 2025-01-09 01:43:19

我通过添加

<connection>
 <sender>buttonBox</sender>
 <signal>clicked(button)</signal>
 <receiver>OptionsWindow</receiver>
 <slot>clicked(button)</slot>
</connection>

到我的 OptionsWindow.ui 部分来完成此操作,然后在代码中它是:

void OptionsWindow::clicked(QAbstractButton* button)
{
    if (mUi->buttonBox->buttonRole(button) == QDialogButtonBox::ButtonRole::ResetRole)
    {
        mOptionsData = OptionsData();
        mSavedOptionsData = mOptionsData;
        updateUi();
    }
}

作为插槽。

I've done it by adding

<connection>
 <sender>buttonBox</sender>
 <signal>clicked(button)</signal>
 <receiver>OptionsWindow</receiver>
 <slot>clicked(button)</slot>
</connection>

to my OptionsWindow.ui <connections> section, and then in code it is:

void OptionsWindow::clicked(QAbstractButton* button)
{
    if (mUi->buttonBox->buttonRole(button) == QDialogButtonBox::ButtonRole::ResetRole)
    {
        mOptionsData = OptionsData();
        mSavedOptionsData = mOptionsData;
        updateUi();
    }
}

as a slot.

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