1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Using Dialog Boxes</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.54"/><LINK
REL="HOME"
TITLE="KDE 2.0 Development"
HREF="index.html"/><LINK
REL="UP"
TITLE="Advanced KDE Widgets and UI Design Techniques"
HREF="p02.html"/><LINK
REL="PREVIOUS"
TITLE="Exercises"
HREF="ch07lev1sec7.html"/><LINK
REL="NEXT"
TITLE="Dialog Layout the Simple Way"
HREF="ch08lev1sec2.html"/><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"/><LINK
REL="stylesheet"
HREF="kde-common.css"
TYPE="text/css"/><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"/><META
HTTP-EQUIV="Content-Language"
CONTENT="en"/><LINK
REL="stylesheet"
HREF="kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"/><LINK
REL="stylesheet"
HREF="kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"/></HEAD
><BODY
CLASS="chapter"
LINK="#336699"
VLINK="#336699"
ALINK="#336699"
BGCOLOR="#FFFFFF"
><DIV
ALIGN="RIGHT"
CLASS="NAVBAR"
><P
><A
HREF="ch07lev1sec7.html"
>Prev</A
> <A
HREF="ch08lev1sec2.html"
>Next</A
> <A
HREF="index.html"
>Table of Contents</A
></P
></DIV
><DIV
CLASS="chapter"
><H1
><A
NAME="ch08"
>Chapter 8. Using Dialog Boxes</A
></H1
><P
><I
CLASS="emphasis"
>by Espen Sand</I
></P
><DIV
CLASS="highlights"
><A
NAME="AEN11876"
></A
><P
><B
>In This Chapter</B
></P
><UL
COMPACT="COMPACT"
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08.html#ch08lev1sec1"
>Getting Started with the Dialog Widgets</A
></I
></P
></LI
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08lev1sec2.html"
>Dialog Layout the Simple Way</A
></I
></P
></LI
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08lev1sec3.html"
>Dialog Modality—Modal or Modeless Dialogs</A
></I
></P
></LI
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08lev1sec4.html"
>KDE User-Interface Library (kdeui)</A
></I
></P
></LI
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08lev1sec5.html"
>Dialog Style and <TT
CLASS="literal"
>KDialogBase</TT
></A
></I
></P
></LI
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08lev1sec6.html"
>A Larger Example: The Option Dialog in KEdit</A
></I
></P
></LI
><LI
STYLE="list-style-type: disc"
><P
><I
CLASS="emphasis"
><A
HREF="ch08lev1sec7.html"
>User Interface Design Rules for Dialogs</A
></I
></P
></LI
></UL
></DIV
><P
>A dialog is a very important part of an application. If the dialogs are not well-designed or suited for their task, the usefulness of the application is often greatly reduced. This chapter describes how to successfully create dialogs that are easy to use, that have a distinct KDE look and feel, and that are simple to develop and later extend and maintain by the developer. The KDE interface library (kdeui) contains several building blocks and widgets that, combined with the regular widgets and layout managers of the Qt library, provide you with what you need to get an optimal result. I emphasize the use of a framework widget named <TT
CLASS="literal"
>KDialogBase</TT
>. The use of the <TT
CLASS="literal"
>KDialogBase</TT
> class greatly simplifies dialog writing because it takes care of much of the tedious work that has to be repeated for every dialog you make.</P
><P
>Although several interface builders are available that can create dialogs for you (see <A
HREF="ch18.html"
>Chapter 18, <SPAN
CLASS="QUOTE"
>"The KDevelop IDE: The Integrated Development Enviroment for KDE"</SPAN
></A
>), the KDE interface library widgets are designed to simplify a hand-coded design process. In addition, they automatically give your dialogs the look and feel recommended by the KDE style guide, which is available on the Web page: <A
HREF="http://developer.kde.org/documentation/standards/kde/style/basics"
TARGET="_top"
>http://developer.kde.org/documentation/standards/kde/style/basics</A
>. Several examples illustrate how to use these basic widgets.</P
><DIV
CLASS="section"
><TABLE
WIDTH="100%"
CELLPADDING="0"
CELLSPACING="0"
BORDER="0"
ALIGN="CENTER"
><TR
><TD
WIDTH="90%"
><H1
CLASS="section"
><A
NAME="ch08lev1sec1"
>8.1. Getting Started with the Dialog Widgets</A
></H1
><P
>Let's start with an example. <A
HREF="ch08.html#ch08fig01"
>Figure 8.1</A
> shows a very simple dialog that is used in the standard KDE hex editor—KHexEdit. The code in <A
HREF="ch08.html#ch08list01"
>Listing 8.1</A
> shows how the dialog class is derived from <TT
CLASS="literal"
>KDialogBase</TT
>. Most of the initialization of <TT
CLASS="literal"
>KDialogBase</TT
> class takes place in the constructor of that class. The class definition is normally placed in a separate header file, but for simplicity it is shown here together with the regular code.</P
><DIV
CLASS="figure"
><HR/><A
NAME="ch08fig01"
></A
><P
><B
>Figure 8.1. This dialog is used to display the number of bytes currently selected in KHexEdit's editor window. The value is printed in decimal and hexadecimal.</B
></P
><DIV
CLASS="mediaobject"
><P
><IMG
SRC="graphics/08fig01.gif"
></IMG
></P
></DIV
><HR/></DIV
><DIV
CLASS="example"
><HR/><A
NAME="ch08list01"
></A
><P
><B
>Example 8.1. Simplified Listing of the <TT
CLASS="literal"
>SelectDialog</TT
> Dialog Class</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: class SelectDialog : public KDialogBase
3 2: {
4 3: Q_OBJECT
5 4:
6 5: public:
7 6: SelectDialog( QWidget *parent=0, const char *name=0,
8 7: bool modal=false );
9 8: ~SelectDialog( void );
10 9:
11 10: private:
12 11: void setSelectionSize( uint selectionSize );
13 12:
14 13: private:
15 14: QLineEdit *mSelectSizeEdit;
16 15: uint mSelectionSize;
17 16: };
18 17:
19 18: SelectDialog::SelectDialog( QWidget *parent, const char *name,
20 19: bool modal )
21 20: :KDialogBase( parent, name, modal, i18n("Select Indicator"),
22 21: Cancel, Cancel )
23 22: {
24 23: QWidget *page = new QWidget(this);
25 24: CHECK_PTR( page );
26 25: setMainWidget( page );
27 26:
28 27: QVBoxLayout *topLayout = new QVBoxLayout( page, 0, spacingHint() );
29 28: CHECK_PTR( page );
30 29:
31 30: QLabel *label = new QLabel( i18n("Selection size [bytes]:"), page );
32 31: CHECK_PTR( label );
33 32: topLayout->addWidget( label );
34 33:
35 34: mSelectSizeEdit = new QLineEdit( page );
36 35: CHECK_PTR( mSelectSizeEdit );
37 36: mSelectSizeEdit->setMinimumWidth( fontMetrics().maxWidth()*17 );
38 37: mSelectSizeEdit->setFocusPolicy( QWidget::NoFocus );
39 38: topLayout->addWidget( mSelectSizeEdit );
40 39:
41 40: topLayout->addStretch(10);
42 41:
43 42: setSelectionSize(0);
44 43: }
45 44:
46 45: SelectDialog::~SelectDialog()
47 46: {
48 47: }
49 48:
50 49: void
51 50: SelectDialog::setSelectionSize( uint selectionSize )
52 51: {
53 52: mSelectionSize = selectionSize;
54 53: QString msg;
55 54:
56 55: msg.sprintf( "%08u, %04x:%04x", mSelectionSize, mSelectionSize>>16,
57 56: mSelectionSize&0x0000FFFF );
58 57: mSelectSizeEdit->setText( msg );
59 58: }
60 59:
61 60: // The dialog used as the main application window
62 61:
63 62: #include <kcmdlineargs.h>
64 63: int main( int argc, char **argv )
65 64: {
66 65: KCmdLineArgs::init(argc, argv, "khexedit", 0, 0);
67 66: KApplication app;
68 67: SelectDialog *dialog = new SelectDialog();
69 68: dialog->show();
70 69: int result = app.exec();
71 70: return( result );
72 71: }
73 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>The <TT
CLASS="literal"
>KDialogBase</TT
> widget is described in more detail in the section <A
HREF="ch08lev1sec4.html#ch08lev2sec3"
><SPAN
CLASS="QUOTE"
>"Building Blocks (Manager Widgets)"</SPAN
></A
> later in this chapter. Notice the signature of the constructor on line 18. These are the arguments you should at least provide when making a dialog. Note as well that in the class definition (line 6), the argument has been assigned default values. The values shown in the code are the most commonly used in KDE and Qt code and is in many respects assumed to be the standard implementation. The <TT
CLASS="literal"
>parent</TT
> widget is the widget around which the dialog is centered. Normally, you use the top-level widget or your application as the parent of a dialog. The dialog will then be positioned in the center of your main application window. If the <TT
CLASS="literal"
>parent</TT
> is 0 (null), the dialog is centered with respect to the desktop. The <TT
CLASS="literal"
>name</TT
> is the name of the dialog widget. It should not be used for the dialog title string (often called the caption) because it is not of type <TT
CLASS="literal"
>QString</TT
> (the Unicode string class). The <TT
CLASS="literal"
>name</TT
> is used to identify the widget during development and is very handy if you need to dump a widget hierarchy. You can safely assign 0 to the <TT
CLASS="literal"
>name</TT
> if you don't need it. The last argument, <TT
CLASS="literal"
>modal</TT
>, determines the modality of the dialog. See the section <A
HREF="ch08lev1sec3.html"
><SPAN
CLASS="QUOTE"
>"Dialog Modality—Modal or Modeless Dialogs"</SPAN
></A
> later in this chapter for an extended description and a description of the implications of modal and modeless dialog behavior.</P
></TD
><TD
WIDTH="10%"
VALIGN="BOTTOM"
ALIGN="CENTER"
><ANNMARK
NAME="ch08lev1sec1"/></TD
></TR
><ANNOTATION
NAME="ch08lev1sec1"
TITLE="Getting Started with the Dialog Widgets"/></TABLE
></DIV
></DIV
><DIV
ALIGN="RIGHT"
CLASS="NAVBAR"
><P
><A
HREF="ch07lev1sec7.html"
>Prev</A
> <A
HREF="ch08lev1sec2.html"
>Next</A
> <A
HREF="index.html"
>Table of Contents</A
></P
></DIV
><HR
WIDTH="100%"
SIZE="2"
ALIGN="CENTER"
NOSHADE="NOSHADE"/></BODY
></HTML
>
|