C.8. Chapter 8

C.8.1. Exercises

C.8.1.1. Make a dialog box that can be used to compose and send an email message. The dialog box must contain vertically aligned "From:", "To:", "Cc:", and "Subject:" labels each with a line edit widget to the right. The line edit widgets shall be able to display at least 20 characters regardless of the font size. Beneath the labels, add a multiline edit widget that uses the rest of the available space in the dialog box. The dialog box should have the following action buttons at the bottom: "Address", "Send", and "Cancel". Listing C.26 demonstrates how to create the dialog box and Figure C.1 shows this dialog box.
C.8.1.2. Change the dialog box so that it no longer contains the "Address" action button. Add a "Help" button instead. Add pushbuttons labeled "Choose…" to the right of the line edit widgets belonging to the "To:" and "Cc:" fields. Listing C.27 shows how to create this dialog (only the constructor has changed from Listing C.26), and Figure C.2 depicts the outcome.

C.8.1.1. Make a dialog box that can be used to compose and send an email message. The dialog box must contain vertically aligned "From:", "To:", "Cc:", and "Subject:" labels each with a line edit widget to the right. The line edit widgets shall be able to display at least 20 characters regardless of the font size. Beneath the labels, add a multiline edit widget that uses the rest of the available space in the dialog box. The dialog box should have the following action buttons at the bottom: "Address", "Send", and "Cancel". Listing C.26 demonstrates how to create the dialog box and Figure C.1 shows this dialog box.

   1 
   2   1: //
   3   2: // maildialog.h
   4   3: //
   5   4:
   6   5: #ifndef _MAIL_DIALOG_H_
   7   6: #define _MAIL_DIALOG_H_
   8   7:
   9   8: class QLineEdit;
  10   9: class QMultiLineEdit;
  11  10: #include <kdialogbase.h>
  12  11:
  13  12: class MailDialog : public KDialogBase
  14  13: {
  15  14:   Q_OBJECT
  16  15:
  17  16:   public:
  18  17:     MailDialog(QWidget *parent=0,const char *name=0,bool modal=true);
  19  18:
  20  19:   protected slots:
  21  20:     virtual void slotUser2();
  22  21:     virtual void slotUser1();
  23  22:
  24  23:   private:
  25  24:     QLineEdit *mFromLineEdit;
  26  25:     QLineEdit *mToLineEdit;
  27  26:     QLineEdit *mCcLineEdit;
  28  27:     QLineEdit *mSubjectLineEdit;
  29  28:     QMultiLineEdit *mBodyTextEdit;
  30  29: };
  31  30: #endif
  32  31:
  33  32: //
  34  33: // maildialog.cpp
  35  34: //
  36  35:
  37  36: #include <qlabel.h>
  38  37: #include <qlayout.h>
  39  38: #include <qlineedit.h>
  40  39: #include <qmultilineedit.h>
  41  40: #include <klocale.h>
  42  41:
  43  42: #include "maildialog.h"
  44  43:
  45  44: MailDialog::MailDialog( QWidget *parent, const char *name, bool modal )
  46  45:   : KDialogBase( parent, name, modal, i18n("Compose Mail"),
  47  46:                  User2|User1|Cancel, Ok, false, i18n("&Send"),
  48  47:  i18n("&Address") )
  49  48: {
  50  49:   setPlainCaption("Compose Mail");
  51  50:
  52  51:   QWidget *page = new QWidget( this );
  53  52:   setMainWidget(page);
  54  53:   QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
  55  54:
  56  55:   QGridLayout *glay = new QGridLayout(topLayout,4,2);
  57  56:   QLabel *fromLabel = new QLabel( i18n("From:"), page );
  58  57:   QLabel *toLabel = new QLabel( i18n("To:"), page );
  59  58:   QLabel *ccLabel = new QLabel( i18n("Cc:"), page );
  60  59:   QLabel *subjectLabel = new QLabel( i18n("Subject:"), page );
  61  60:
  62  61:   mFromLineEdit = new QLineEdit( page );
  63  62:   mToLineEdit = new QLineEdit( page );
  64  63:   mCcLineEdit = new QLineEdit( page );
  65  64:   mSubjectLineEdit = new QLineEdit( page );
  66  65:
  67  66:   glay->addWidget( fromLabel, 0, 0, AlignRight );
  68  67:   glay->addWidget( toLabel, 1, 0, AlignRight  );
  69  68:   glay->addWidget( ccLabel, 2, 0, AlignRight  );
  70  69:   glay->addWidget( subjectLabel, 3, 0, AlignRight  );
  71  70:   glay->addWidget( mFromLineEdit, 0, 1 );
  72  71:   glay->addWidget( mToLineEdit, 1, 1 );
  73  72:   glay->addWidget( mCcLineEdit, 2, 1 );
  74  73:   glay->addWidget( mSubjectLineEdit, 3, 1 );
  75  74:   mFromLineEdit->setMinimumWidth(fontMetrics().maxWidth()*20);
  76  75:
  77  76:   mBodyTextEdit = new QMultiLineEdit( page );
  78  77:   topLayout->addWidget( mBodyTextEdit, 10 );
  79  78:   mBodyTextEdit->setMinimumHeight(fontMetrics().lineSpacing()*10);
  80  79: }
  81  80:
  82  81: void
  83  82: MailDialog::slotUser1() // Send
  84  83: {
  85  84:   // Send your mail here
  86  85: }
  87  86:
  88  87: void
  89  88: MailDialog::slotUser2() // Addresses
  90  89: {
  91  90:   // Open your address book here
  92  91: }
  93  92:
  94  93:
  95  94: //
  96  95: // A main.cpp file used to test the dialog
  97  96: //
  98  97:
  99  98: #include <kcmdlineargs.h>
 100  99: #include "maildialog.h"
 101 100: int main( int argc, char **argv )
 102 101: {
 103 102:   KCmdLineArgs::init(argc, argv, "appname", 0, 0);
 104 103:   KApplication app;
 105 104:   MailDialog *dialog = new MailDialog;
 106 105:   dialog->show();
 107 106:   int result = app.exec();
 108 107:   return result;
 109 108: }
 110 


Figure C.1. The mail dialog.


C.8.1.2. Change the dialog box so that it no longer contains the "Address" action button. Add a "Help" button instead. Add pushbuttons labeled "Choose…" to the right of the line edit widgets belonging to the "To:" and "Cc:" fields. Listing C.27 shows how to create this dialog (only the constructor has changed from Listing C.26), and Figure C.2 depicts the outcome.

   1 
   2  1: MailDialog::MailDialog( QWidget *parent, const char *name, bool modal )
   3  2:   : KDialogBase( parent, name, modal, i18n("Compose Mail"),
   4  3:                  Help|User1|Cancel, Ok, false, i18n("&Send") )
   5  4: {
   6  5:   setPlainCaption("Compose Mail");
   7  6:
   8  7:   QWidget *page = new QWidget( this );
   9  8:   setMainWidget(page);
  10  9:   QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
  11 10:
  12 11:   QGridLayout *glay = new QGridLayout(topLayout,4,2);
  13 12:   QLabel *fromLabel = new QLabel( i18n("From:"), page );
  14 13:   QLabel *toLabel = new QLabel( i18n("To:"), page );
  15 14:   QLabel *ccLabel = new QLabel( i18n("Cc:"), page );
  16 15:   QLabel *subjectLabel = new QLabel( i18n("Subject:"), page );
  17 16:
  18 17:   mFromLineEdit = new QLineEdit( page );
  19 18:   mToLineEdit = new QLineEdit( page );
  20 19:   mCcLineEdit = new QLineEdit( page );
  21 20:   mSubjectLineEdit = new QLineEdit( page );
  22 21:
  23 22:   QPushButton *toPushButton = new QPushButton( i18n("Choose…"), page );
  24 23:   toPushButton->setAutoDefault( false );
  25 24:   QPushButton *ccPushButton = new QPushButton( i18n("Choose…"), page );
  26 25:   ccPushButton->setAutoDefault( false );
  27 26:
  28 27:   glay->addWidget( fromLabel, 0, 0, AlignRight );
  29 28:   glay->addWidget( toLabel, 1, 0, AlignRight  );
  30 29:   glay->addWidget( ccLabel, 2, 0, AlignRight  );
  31 30:   glay->addWidget( subjectLabel, 3, 0, AlignRight  );
  32 31:   glay->addMultiCellWidget( mFromLineEdit, 0, 0, 1, 2 );
  33 32:   glay->addWidget( mToLineEdit, 1, 1 );
  34 33:   glay->addWidget( mCcLineEdit, 2, 1 );
  35 34:   glay->addMultiCellWidget( mSubjectLineEdit, 3, 3, 1, 2 );
  36 35:   mFromLineEdit->setMinimumWidth(fontMetrics().maxWidth()*20);
  37 36:   glay->addWidget( toPushButton, 1, 2 );
  38 37:   glay->addWidget( ccPushButton, 2, 2 );
  39 38:
  40 39:   mBodyTextEdit = new QMultiLineEdit( page );
  41 40:   topLayout->addWidget( mBodyTextEdit, 10 );
  42 41:   mBodyTextEdit->setMinimumHeight(fontMetrics().lineSpacing()*10);
  43 42: }
  44 


Figure C.2. The mail dialog with the modified layout.