Qt:未通过信号和槽机制恢复首选项

发布于 2025-01-01 04:12:27 字数 1217 浏览 2 评论 0 原文

在我的文本编辑器应用程序中,我将用户的字体格式选择保存为首选项。

首先在构造函数中设置信号和槽,然后按以下代码读取首选项:

构造函数:

boldAction->setCheckable(true);
italicAction->setCheckable(true);
underlineAction->setCheckable(true);
fontSizeSelector->setCheckable(false);

connect(boldAction,SIGNAL(changed()),this,SLOT(bold()));
connect(italicAction,SIGNAL(triggered()),this,SLOT(italic()));
connect(underlineAction,SIGNAL(triggered()),this,SLOT(underline()));

ReadUserPreferences():

void TextEditor::readUserPreferences()
    {
        QSettings userPreferences(QSettings::NativeFormat,QSettings::UserScope,ORGANIZATION_TITLE,APPLICATION_TITLE);

        this->boldAction->setChecked( userPreferences.value("appearance/bold").toBool() );
        this->italicAction->setChecked( userPreferences.value("appearance/italic").toBool() );
        this->underlineAction->setChecked( userPreferences.value("appearance/underline").toBool());

       //other code.
}

现在,在 readPreferences 函数中,当boldAction->setChecked(true);时,文本不应该变成粗体吗,因为信号和槽机制已经被定义了?如果是这样,那么为什么它不适用于我的应用程序?粗体功能本身运行得很好。

有比我正在做的更好的方法吗?感谢您的帮助

In my Text Editor application, I save the users font format selection as a preference.

Signals and slots are first set up in the constructor, and then the preferences are read as in the code below:

Constructor:

boldAction->setCheckable(true);
italicAction->setCheckable(true);
underlineAction->setCheckable(true);
fontSizeSelector->setCheckable(false);

connect(boldAction,SIGNAL(changed()),this,SLOT(bold()));
connect(italicAction,SIGNAL(triggered()),this,SLOT(italic()));
connect(underlineAction,SIGNAL(triggered()),this,SLOT(underline()));

ReadUserPreferences():

void TextEditor::readUserPreferences()
    {
        QSettings userPreferences(QSettings::NativeFormat,QSettings::UserScope,ORGANIZATION_TITLE,APPLICATION_TITLE);

        this->boldAction->setChecked( userPreferences.value("appearance/bold").toBool() );
        this->italicAction->setChecked( userPreferences.value("appearance/italic").toBool() );
        this->underlineAction->setChecked( userPreferences.value("appearance/underline").toBool());

       //other code.
}

Now, in the readPreferences function, when boldAction->setChecked(true);, shouldn't the text become bold because the signal and slot mechanism has already been defined? If so, then why isn't it working on my application? The bold function itself works perfectly fine.

Is there a better way of doing this than what I'm doing? Thanks for your help

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

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

发布评论

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

评论(1

很酷又爱笑 2025-01-08 04:12:27

这里似乎有两件事是错误的。

首先,您连接到了错误的信号。 changed 信号不会传递指示操作的值检查状态,并且setChecked 时,根本不会发出 rel="nofollow">triggered 。您需要连接到 toggled 信号。

其次,只有当已检查状态发生变化时才会发出信号。因此,如果已检查该操作并且用户首选项的计算结果为 true,则不会发生任何情况。您可能需要采取措施确保在连接信号之前设置适当的默认状态。

There appear to be two things wrong here.

Firstly, you are connecting to the wrong signals. The changed signal does not pass a value indicating the action's checked state, and triggered is not emitted at all when setChecked is called. You need to connect to the toggled signal.

Secondly, the signal will only be emitted if the checked state has changed. So if the action is already checked and the user preference evaluates to true, nothing will happen. You probably need to take steps to ensure the appropriate default state is set before connecting the signals.

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