Step 2: Designing the UI
Making the UI layout
First you must create the UI layout. In the GuitarTuner application there must be buttons for selecting the tone, sliders for microphone sensitivity and volume, a mute button, and a button to change the input or output mode. Furthermore, one slider is used to show the voice level.
A more advanced way to construct the application UI is presented in the Qt Quick hands-on lab.
-
Open the guitartunerui.ui file from the src folder under the Forms category. The design mode will open and you will see an empty widget to be edited.
-
Drag and drop a new label to the widget, give it the name tabulatorIcon, remove the default text, and make the horizontal alignment to be centered by choosing AlignHCenter. Properties other than default values will appear as bold text. To add a pixmap, click the pixmap value, and buttons will appear; choose the image from files or from resources. Press the button with the down arrow, select Choose Resource from the opened menu, and select guitartab.svg.
-
Add a new horizontal slider to the widget; give it the name correctSoundSlider; set the minimum and maximum values to be -50 and 50, respectively; and set the tickPosition property to be TicksAbove and tickInterval to be 50.
-
Add a new horizontal label to the widget and name it noteLabel. Set the text to be A.
-
Add two tool buttons to the widget, name them buttonPrev and buttonNext, and set their arrowType property to be LeftArrow and RightArrow, respectively. Place them beside the noteLabel. Set their horizontal size policy to be preferred instead of fixed.
-
Select the created noteLabel label, buttonPrev, and buttonNext buttons. Choose Tools > Form Editor > Lay out Horizontally.
-
Add a new push button to the widget, name it modeButton, and set its text to Change mode.
-
Add a horizontal line to the widget.
-
Add a new vertical slider to the widget, name it soundSlider, and set its value to be 65.
-
Add a new tool button, name it soundButton, set the autoRaise property to True, and set the icon to be note.svg. After that, set the Normal No and Active On icons to be noteNo.svg. Place soundButton on the right side of soundSlider.
-
Add a new label, name it micSensitivityLabel, and set its text to be microphone\nsensitivity (where '\n' denotes a new line). Set the horizontal size policy to be fixed, so that the text label's vertical size is fixed.
-
Select the created soundSlider, soundButton, and micSensitivityLabel widgets. Choose Tools > Form Editor > Lay out Horizontally.
-
Select the whole widget. Choose Tools > Form Editor > Lay out Vertically.
Implementing GuitarTunerUI
The UI layout is finished. Now you will proceed to implement the elementary functionality of the GuitarTunerUI class.
-
Add implementation to the GuitarTunerUI constructor as follows. Code has already been added to set up the UI. You need to set up the class attributes to default values. Use the getVolumeFromSoundSlider method to acquire the proper value for volume and microphone sensitivity. Then set up the current tone index and use updateFrequencyByToneIndex to set up the frequency and the name for it. Finally, initialise the UI by calling toggleInputOrOutput for the first time.
Note that there are some unimplemented @TODOs left in this method on purpose. Do not implement these, because they will be implemented during the following steps. This same directive also applies for other steps of this hands-on lab module.
-
Add implementation to the GuitarTunerUI getVolumeFromSoundSlider method. This is just a method that returns the current value of soundSlider, divided by the maximum value of the slider.
-
Add implementation to the GuitarTunerUI updateFrequencyByToneIndex method. It must set m_currentToneFrequency and m_currentToneString according to the note index, and also set noteLabel to have m_currentToneString text.
-
Implement the GuitarTunerUI methods getVolume, getMuteState, getMicrophoneSensitivity, isInputModeActive, and getFrequency. They just return the corresponding class attributes m_outputVolumeLevel, m_muted, m_inputVolumeLevel, the negation of m_outputActive, and m_currentToneFrequency.
Now when you run the application, the following UI will be shown. In the next steps, the implementation of the UI will be modified so that only either soundButton or micSensitivityLabel is shown at a time.
If the application is not working properly, download and use the files in this zip file.