【问题标题】:Qt: Preferences not restored through signal and slot mechanismQt:不通过信号和槽机制恢复首选项
【发布时间】:2012-01-31 10:40:07
【问题描述】:

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

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

构造函数:

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); 时,文本不应该变成粗体,因为已经定义了信号和槽机制吗?如果是这样,那为什么它不能在我的应用程序上运行?粗体功能本身工作得很好。

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

【问题讨论】:

    标签: preferences signals-slots qt4.7 qsettings


    【解决方案1】:

    这里似乎有两个问题。

    首先,您连接到了错误的信号。 changed 信号没有传递指示操作的已检查 状态的值,并且在调用setChecked 时根本不会发出triggered。您需要连接到toggled 信号。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多