Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top