Quick Tip: Quickly Enable and Disable Widgets in Qt

Today, we’ll look at a quick tip to show you how to use the Qt Creator Signals and Slots editor to add a checkbox that disables/enables widgets on a form. Of course, you could easily do this in code, but this is one of those things that I simply prefer to use the form editor for, since it creates a lot of redundant code in your UI class.

The logic behind it is simple. You have a checkbox toggled(bool) signal connected up to your widget with the setEnabled(bool) slot. First, go to the editor and change it to signals and slots mode. Drag a connection from the checkbox/toggle to the widget(s) you would like to have toggled. When they connect, make sure you have inherited signals/slots checked! You will not get the setEnabled(bool) otherwise.

Configure Connection Dialog

Make sure you have the inherited signals/slots.

Qt Creator Signals/Slots editor

This form totally won’t be used on this blog… 😉

And all this without cluttering your UI class! Below you can see it in action. I would encourage you to play around with some of the other slots, namely setHidden(bool), which might give you a result you are looking for.

Enabled Widgets

The widgets when they are enabled function like normal.

Disabled Widgets

When widgets are disabled, they give a visual cue to the fact and do not allow interaction.

Hope you learned something! Keep on coding!

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s