How to resolve the algorithm GUI enabling/disabling of controls step by step in the C++ programming language
How to resolve the algorithm GUI enabling/disabling of controls step by step in the C++ programming language
Table of Contents
Problem Statement
In addition to fundamental GUI component interaction, an application should dynamically enable and disable GUI components, to give some guidance to the user, and prohibit (inter)actions which are inappropriate in the current state of the application.
Similar to the task GUI component interaction, write a program that presents a form with three components to the user:
The field is initialized to zero. The user may manually enter a new value into the field, increment its value with the "increment" button, or decrement the value with the "decrement" button. The input field should be enabled only when its value is zero. The "increment" button only as long as the field's value is less then 10: When the value 10 is reached, the button should go into a disabled state. Analogously, the "decrement" button should be enabled only as long as the value is greater than zero. Effectively, the user can now either increment up to 10, or down to zero. Manually entering values outside that range is still legal, but the buttons should reflect that and enable/disable accordingly.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm GUI enabling/disabling of controls step by step in the C++ programming language
Class Definition MyWidget
:
- A custom widget derived from
QWidget
. It contains the graphical user interface elements and the logic for handling user interactions.
Constructor MyWidget::MyWidget
:
- Initializes the custom widget.
- Sets up the layout using
QVBoxLayout
andQHBoxLayout
. - Creates labels, buttons, and a line edit widget for user input.
- Establishes connections between GUI elements and defined slots (event handlers).
Slot MyWidget::buttonChange
:
- Called when the text in the line edit widget changes.
- Checks if the entered text is a number between 1 and 10.
- Enables or disables the "Increment" and "Decrement" buttons based on the entered number.
Slot MyWidget::addField
:
- Called when the "Increment" button is clicked.
- Increments the number in the line edit widget.
Slot MyWidget::subtractField
:
- Called when the "Decrement" button is clicked.
- Decrements the number in the line edit widget.
Main Function main
:
- Initializes the Qt application with
QApplication
. - Creates an instance of the custom widget
MyWidget
. - Shows the widget and starts the application event loop with
app.exec()
.
Usage:
- The widget displays an instruction label and a line edit widget for the user to enter a number between 1 and 10.
- If the entered number is above 10, the "Decrement" button is enabled, and the "Increment" button is disabled.
- If the entered number is below 1, the "Increment" button is enabled, and the "Decrement" button is disabled.
- The "Increment" button increments the entered number by 1, while the "Decrement" button decrements it by 1.
Source code in the cpp programming language
#ifndef TASK_H
#define TASK_H
#include <QWidget>
class QPushButton ;
class QString ;
class QLineEdit ;
class QLabel ;
class QVBoxLayout ;
class QHBoxLayout ;
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget( QWidget *parent = 0 ) ;
private slots:
void buttonChange( const QString & ) ;
void addField( ) ;
void subtractField( ) ;
private :
QVBoxLayout *thisWidgetLayout ;
QLabel *instruction ;
QPushButton *increment ;
QPushButton *decrement ;
QLineEdit *entryField ;
QHBoxLayout *lowerPart ;
} ;
#endif
#include <QtGui>
#include <QString>
#include "task.h"
MyWidget::MyWidget ( QWidget *parent )
: QWidget( parent ) {
thisWidgetLayout = new QVBoxLayout ( this ) ;
instruction = new QLabel ;
instruction->setText( "Enter a number between 1 and 10 ! Numbers above 10 are decremented, below 0 incremented!" ) ;
instruction->setWordWrap( true ) ;
lowerPart = new QHBoxLayout ;
entryField = new QLineEdit( "0" ) ;
increment = new QPushButton( "Increment" ) ;
decrement = new QPushButton( "Decrement" ) ;
increment->setDefault( true ) ;
connect( entryField , SIGNAL ( textChanged ( const QString & ) ) ,
this , SLOT ( buttonChange( const QString & )) ) ;
connect( entryField , SIGNAL ( textEdited ( const QString & ) ) ,
this , SLOT ( buttonChange( const QString & )) ) ;
connect( increment , SIGNAL ( clicked( ) ) , this ,
SLOT ( addField( ) )) ;
connect( decrement , SIGNAL ( clicked( ) ) , this ,
SLOT ( subtractField( ))) ;
lowerPart->addWidget( entryField ) ;
lowerPart->addWidget( increment ) ;
lowerPart->addWidget( decrement ) ;
thisWidgetLayout->addWidget( instruction ) ;
thisWidgetLayout->addLayout( lowerPart ) ;
setLayout( thisWidgetLayout ) ;
}
void MyWidget::buttonChange( const QString & text ) {
bool ok ;
increment->setEnabled( text.toInt( &ok, 10 ) < 10 ) ;
increment->setDisabled( text.toInt( &ok, 10 ) > 9 ) ;
decrement->setEnabled( text.toInt( &ok, 10 ) > 0 ) ;
decrement->setDisabled( text.toInt( &ok, 10 ) <= 0 ) ;
if ( ! ( text == "0" ) )
entryField->setReadOnly( true ) ;
}
void MyWidget::addField( ) {
bool ok ;
int number = entryField->text( ).toInt( &ok , 10 ) ;
number++ ;
entryField->setText( QString("%1").arg( number )) ;
}
void MyWidget::subtractField( ) {
bool ok ;
int number = entryField->text( ).toInt( &ok , 10 ) ;
number-- ;
entryField->setText( QString("%1").arg( number )) ;
}
#include <QApplication>
#include "task.h"
int main( int argc, char *argv[ ] ) {
QApplication app( argc , argv ) ;
MyWidget theWidget ;
theWidget.show( ) ;
return app.exec( ) ;
}
You may also check:How to resolve the algorithm GUI/Maximum window dimensions step by step in the Haskell programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the Zig programming language
You may also check:How to resolve the algorithm Empty program step by step in the EGL programming language
You may also check:How to resolve the algorithm IBAN step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the ALGOL 60 programming language