Embedded Development with Qt/Embedded 
by Matthias Kalle Dalheimer and Steffen Hansen


Listing One
#include "ExpenseDialog.h"
#include "Expense.h"

#include <qcombobox.h>
#include <qdatetimeedit.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qvalidator.h>

ExpenseDialog::ExpenseDialog( const QStringList& categories,
         const QStringList& currencies, QWidget* parent, const char*
name ) :
         QDialog( parent, name, true )
{
    // The top-level layout manager
    QVBoxLayout* toplevelVBL = new QVBoxLayout( this, 10 );

    // Create the widgets for data entry
    QGridLayout* dataGL = new QGridLayout( 5, 2, 10 );
    toplevelVBL->addLayout( dataGL );

    QLabel* dateLA = new QLabel( "Date:", this );
    dataGL->addWidget( dateLA, 0, 0 );
    _dateDE = new QDateEdit( this );
    _dateDE->setDate( QDate::currentDate() );

    dataGL->addWidget( _dateDE, 0, 1 );

    QLabel* descriptionLA = new QLabel( "Description:", this );
    dataGL->addWidget( descriptionLA, 1, 0 );
    _descriptionED = new QLineEdit( this );
    dataGL->addWidget( _descriptionED, 1, 1 );

    QLabel* categoryLA = new QLabel( "Category:", this );
    dataGL->addWidget( categoryLA, 2, 0 );
    _categoryCO = new QComboBox( this );
    _categoryCO->insertStringList( categories );
    dataGL->addWidget( _categoryCO, 2, 1 );

    QLabel* currencyLA = new QLabel( "Currency:", this );
    dataGL->addWidget( currencyLA, 3, 0 );
    _currencyCO = new QComboBox( this );
    _currencyCO->insertStringList( currencies );
    dataGL->addWidget( _currencyCO, 3, 1 );

    QLabel* amountLA = new QLabel( "Amount:", this );
    dataGL->addWidget( amountLA, 4, 0 );
    _amountED = new QLineEdit( this );
    _amountED->setValidator( new QDoubleValidator( _amountED ) );
    dataGL->addWidget( _amountED, 4, 1 );

    // Create the OK and Cancel buttons
    QHBoxLayout* buttonsHBL = new QHBoxLayout( 10 );
    toplevelVBL->addLayout( buttonsHBL );
    QPushButton* okPB = new QPushButton( "&OK", this );
    okPB->setDefault( true );
    connect( okPB, SIGNAL( clicked() ), this, SLOT( accept() ) );
    buttonsHBL->addWidget( okPB );
    QPushButton* cancelPB = new QPushButton( "&Cancel", this );
    connect( cancelPB, SIGNAL( clicked() ), this, SLOT( accept() ) );
    buttonsHBL->addWidget( cancelPB );

    // restrict even this dialog to 240x320
    setMaximumSize( 240, 320 );
}
void ExpenseDialog::setExpenseValues( Expense* expense )
{
    // preset the widgets with the values from the Expense object
    _dateDE->setDate( expense->date() );
    _descriptionED->setText( expense->description() );
    for( int i = 0; i < _categoryCO->count(); i++ )
        if( _categoryCO->text( i ) == expense->category() ) {
            _categoryCO->setCurrentItem( i );
            break;
        }
    for( int i = 0; i < _currencyCO->count(); i++ )
        if( _currencyCO->text( i ) == expense->currency() ) {
            _currencyCO->setCurrentItem( i );
            break;
        }
    _amountED->setText( QString::number( expense->amount(), 'f', 2 ) );
}
QDate ExpenseDialog::date() const
{
    return _dateDE->date();
}
QString ExpenseDialog::description() const
{
    return _descriptionED->text();
}
QString ExpenseDialog::category() const
{
    if( _categoryCO->count() > 0 )
        return _categoryCO->currentText();
    else
        return QString::null;
}
QString ExpenseDialog::currency() const
{
    if( _currencyCO->count() > 0 )
        return _currencyCO->currentText();
    else
        return QString::null;
}
double ExpenseDialog::amount() const
{
    double value = 0.0;
    bool ok;
    value = _amountED->text().toDouble( &ok );
    if( ok )
        return value;
    else
        return 0.0;
}


Listing Two
#include <qapplication.h>
#include "ExpensesMainWindow.h"

int main( int argc, char* argv[] )
{
    QApplication app( argc, argv );

    ExpensesMainWindow mainWindow;
    mainWindow.show();
    app.setMainWidget( &mainWindow );

    return app.exec();
}

Listing Three
TEMPLATE += app
TARGET += expenses

SOURCES += ExpensesMainWindow.cpp main.cpp Expense.cpp ExpenseDialog.cpp

HEADERS += ExpensesMainWindow.h Expense.h ExpenseDialog.h





3

