1

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

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

构造函数:

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()));

读取用户首选项():

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

有没有比我正在做的更好的方法呢?谢谢你的帮助

4

1 回答 1

2

这里似乎有两件事不对。

首先,您连接到错误的信号。改变的信号没有传递一个指示动作的检查状态的值,并且在被调用时根本不会发出触发。setChecked您需要连接到切换信号。

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

于 2012-01-31T18:34:37.310 回答