C++: GUI Enabling/Disabling of Controls

Bjarne-stroustrup
 

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.

The task: Similar to the task GUI component interaction write a program that presents a form with three components to the user: A numeric input field (“Value”) and two buttons (“increment” and “decrement”).

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.

file task.h

#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
file task.cpp

#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 )) ;
}
main.cpp

#include <QApplication>
#include "task.h"

int main( int argc, char *argv[ ] ) {
	QApplication app( argc , argv ) ;
	MyWidget theWidget ;
	theWidget.show( ) ;
	return app.exec( ) ;
}

SOURCE

Content is available under GNU Free Documentation License 1.2.