C++: GUI Component Interaction

Bjarne-stroustrup
 

Almost every application needs to communicate with the user in some way. Therefore, a substantial part of the code deals with the interaction of program logic with GUI components.

Typically, the following is needed:

  • put values into input fields under program control
  • read and check input from the user
  • pop up dialogs to query the user for further information

The task: For a minimal “application”, write a program that presents a form with three components to the user: A numeric input field (“Value”) and two buttons (“increment” and “random”).

The field is initialized to zero. The user may manually enter a new value into the field, or increment its value with the “increment” button. Entering a non-numeric value should be either impossible, or issue an error message.

Pressing the “random” button presents a confirmation dialog, and resets the field’s value to a random value if the answer is “Yes”.

(This task may be regarded as an extension of the task Simple windowed application).

with library Qt 4.4 , using (under Linux) first qmake -project and then qmake -o Makefile <projectfile>, then make

file interaction.h

#ifndef INTERACTION_H
#define INTERACTION_H
#include <QWidget>

class QPushButton ;
class QLineEdit ;
class QVBoxLayout ;
class MyWidget : public QWidget {
	Q_OBJECT 

	public :
	MyWidget( QWidget *parent = 0 ) ;
	private :
	QLineEdit *entryField ;
	QPushButton *increaseButton ;
	QPushButton *randomButton ;
	QVBoxLayout *myLayout ;
	private slots :
	void doIncrement( ) ;
	void findRandomNumber( ) ;
} ;
#endif
file interaction.cpp

#include <QPushButton>
#include <QLineEdit>
#include <QMessageBox>
#include <QString>
#include <QRegExpValidator>
#include <QVBoxLayout>
#include <QRegExp>
#include <ctime> //for the srand initialization
#include <cstdlib> //for the random number
#include "interaction.h" 

MyWidget::MyWidget (QWidget *parent ) : QWidget( parent ) {
	myLayout = new QVBoxLayout( ) ;
	entryField = new QLineEdit( "0" ) ;
	QRegExp rx( "\\d+" ) ;
	QValidator *myvalidator = new QRegExpValidator( rx , this ) ;
	entryField->setValidator( myvalidator ) ;
	increaseButton = new QPushButton( "increase" ) ;
	connect( increaseButton, SIGNAL( clicked( ) ) ,
	this , SLOT( doIncrement( ) )) ;
	randomButton = new QPushButton( "random" ) ;
	connect( randomButton , SIGNAL( clicked( ) ) ,
	this , SLOT ( findRandomNumber( ) )) ;
	myLayout->addWidget( entryField ) ;
	myLayout->addWidget( increaseButton ) ;
	myLayout->addWidget( randomButton ) ;
	setLayout( myLayout ) ;
}

void MyWidget::doIncrement( ) {
	bool ok ;
	int zahl = entryField->text( ).toInt( &ok, 10 ) ;
	entryField->setText( QString( "%1").arg( ++zahl ) ) ;
}

void MyWidget::findRandomNumber( ) {
	QMessageBox msgBox( this ) ;
	msgBox.setText( "Do you want to create a random number ?" ) ;
	msgBox.setStandardButtons( QMessageBox::Yes | QMessageBox::No ) ;
	int ret = msgBox.exec( ) ;
	switch ( ret ) {
	case QMessageBox::Yes :
		srand( time( 0 ) ) ;
		int zahl = random( ) ;
		entryField->setText( QString( "%1" ).arg( zahl )) ;
		break ;
	}
}
file main.cpp

#include <QApplication>
#include "interaction.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.