2012-01-31 36 views
1

在我的文本编辑器应用程序中,我将用户字体格式选择保存为首选项。Qt:不通过信号和插槽机制恢复首选项

信号和槽首先被设置在构造,然后将偏好被读取如在下面的代码:

构造:

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

有没有比我所做的更好的方式做到这一点?感谢您的帮助

回答

2

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

首先,您正在连接到错误的信号。 changed信号未通过指示动作的检查状态的值,并且在调用setChecked时根本不发射triggered。您需要连接到toggled信号。

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