-
Notifications
You must be signed in to change notification settings - Fork 56
/
tut5.html
1969 lines (1627 loc) · 77.4 KB
/
tut5.html
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
<title>PyQt By Example: Session 5</title>
<style type="text/css">
/*
:Author: David Goodger
:Contact: [email protected]
:Date: $Date: 2008/01/29 22:14:02 $
:Revision: $Revision: 1.1.1.1 $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
customize this style sheet.
*/
/* used to remove borders from tables and images */
.borderless, table.borderless td, table.borderless th {
border: 0 }
table.borderless td, table.borderless th {
/* Override padding for "table.docutils td" with "! important".
The right padding separates the table cells. */
padding: 0 0.5em 0 0 ! important }
.first {
/* Override more specific margin styles with "! important". */
margin-top: 0 ! important }
.last, .with-subtitle {
margin-bottom: 0 ! important }
.hidden {
display: none }
a.toc-backref {
text-decoration: none ;
color: black }
blockquote.epigraph {
margin: 2em 5em ; }
dl.docutils dd {
margin-bottom: 0.5em }
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
*/
div.abstract {
margin: 2em 5em }
div.abstract p.topic-title {
font-weight: bold ;
text-align: center }
div.admonition, div.attention, div.caution, div.danger, div.error,
div.hint, div.important, div.note, div.tip, div.warning {
margin: 2em ;
border: medium outset ;
padding: 1em }
div.admonition p.admonition-title, div.hint p.admonition-title,
div.important p.admonition-title, div.note p.admonition-title,
div.tip p.admonition-title {
font-weight: bold ;
font-family: sans-serif }
div.attention p.admonition-title, div.caution p.admonition-title,
div.danger p.admonition-title, div.error p.admonition-title,
div.warning p.admonition-title {
color: red ;
font-weight: bold ;
font-family: sans-serif }
/* Uncomment (and remove this text!) to get reduced vertical space in
compound paragraphs.
div.compound .compound-first, div.compound .compound-middle {
margin-bottom: 0.5em }
div.compound .compound-last, div.compound .compound-middle {
margin-top: 0.5em }
*/
div.dedication {
margin: 2em 5em ;
text-align: center ;
font-style: italic }
div.dedication p.topic-title {
font-weight: bold ;
font-style: normal }
div.figure {
margin-left: 2em ;
margin-right: 2em ;
text-align: center;
max-width: 90%;
}
div.footer, div.header {
clear: both;
font-size: smaller }
div.line-block {
display: block ;
margin-top: 1em ;
margin-bottom: 1em }
div.line-block div.line-block {
margin-top: 0 ;
margin-bottom: 0 ;
margin-left: 1.5em }
div.sidebar {
margin-left: 1em ;
border: medium outset ;
padding: 1em ;
background-color: #ffffee ;
width: 40% ;
float: right ;
clear: right }
div.sidebar p.rubric {
font-family: sans-serif ;
font-size: medium }
div.system-messages {
margin: 5em }
div.system-messages h1 {
color: red }
div.system-message {
border: medium outset ;
padding: 1em }
div.system-message p.system-message-title {
color: red ;
font-weight: bold }
div.topic {
margin: 2em }
h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
margin-top: 0.4em }
h1.title {
text-align: center }
h2.subtitle {
text-align: center }
hr.docutils {
width: 75% }
img.align-left {
clear: left }
img.align-right {
clear: right }
ol.simple, ul.simple {
margin-bottom: 1em }
ol.arabic {
list-style: decimal }
ol.loweralpha {
list-style: lower-alpha }
ol.upperalpha {
list-style: upper-alpha }
ol.lowerroman {
list-style: lower-roman }
ol.upperroman {
list-style: upper-roman }
p.attribution {
text-align: right ;
margin-left: 50% }
p.caption {
font-style: italic }
p.credits {
font-style: italic ;
font-size: smaller }
p.label {
white-space: nowrap }
p.rubric {
font-weight: bold ;
font-size: larger ;
color: maroon ;
text-align: center }
p.sidebar-title {
font-family: sans-serif ;
font-weight: bold ;
font-size: larger }
p.sidebar-subtitle {
font-family: sans-serif ;
font-weight: bold }
p.topic-title {
font-weight: bold }
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
font-family: serif ;
font-size: 100% }
pre.literal-block, pre.doctest-block {
margin-left: 2em ;
margin-right: 2em ;
background-color: #eeeeee }
span.classifier {
font-family: sans-serif ;
font-style: oblique }
span.classifier-delimiter {
font-family: sans-serif ;
font-weight: bold }
span.interpreted {
font-family: sans-serif }
span.option {
white-space: nowrap }
span.pre {
white-space: pre }
span.problematic {
color: red }
span.section-subtitle {
/* font-size relative to parent (h1..h6 element) */
font-size: 80% }
table.citation {
border-left: solid 1px gray;
margin-left: 1px }
table.docinfo {
margin: 2em 4em }
table.docutils {
margin-top: 0.5em ;
margin-bottom: 0.5em }
table.footnote {
border-left: solid 1px black;
margin-left: 1px }
table.docutils td, table.docutils th,
table.docinfo td, table.docinfo th {
padding-left: 0.5em ;
padding-right: 0.5em ;
vertical-align: top }
table.docutils th.field-name, table.docinfo th.docinfo-name {
font-weight: bold ;
text-align: left ;
white-space: nowrap ;
padding-left: 0 }
h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
font-size: 100% }
/*
tt.docutils {
background-color: #eeeeee }
*/
ul.auto-toc {
list-style-type: none }
/* diff2html style */
TD.linenum { color: #909090;
text-align: right;
vertical-align: top;
font-weight: bold;
border-right: 1px solid black;
border-left: 1px solid black; }
TD.added { background-color: #DDDDFF; }
TD.modified { background-color: #BBFFBB; }
TD.removed { background-color: #FFCCCC; }
TD.normal { background-color: #FFFFE1; }
</style>
</head>
<body>
<div class="document" id="pyqt-by-example-session-5">
<h1 class="title">PyQt By Example: Session 5</h1>
<h2 class="subtitle" id="dialog">Dialog</h2>
<div class="section" id="requirements">
<h1>Requirements</h1>
<p>If you have not done it yet, please check the previous sessions:</p>
<ul class="simple">
<li><a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS47.html">Session 1</a></li>
<li><a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS48.html">Session 2</a></li>
<li><a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS49.html">Session 3</a></li>
<li><a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS50.html">Session 4</a></li>
</ul>
<p>All files for this session are here: <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/tree/master/session4">Session 5 at GitHub</a>. You can use them, or you can follow these instructions starting with the files from <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS50.html">Session 4</a> and see how well you worked!</p>
</div>
<div class="section" id="id1">
<h1>Dialog</h1>
<p>When we finished <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS50.html">session 4</a> we had a TODO application that was able of displaying your task list, and deleting a task, and had the proper UI for that.</p>
<div class="figure">
<img alt="window6.png" src="window6.png" />
<p class="caption">A very limited application</p>
</div>
<p>The obvious missing pieces are creating and modifying tasks, and that's what we will be implementing today. This is a longer session, because we will do <em>a lot of work</em>.</p>
<p>But first, let's consider a course of action, because there are several different ways to do this, and trying to implement something you have not thought about sucks.</p>
<div class="section" id="inline-editing">
<h2>Inline Editing</h2>
<p>When the user clicks (or double-clicks?) on a task, he could start editing the task properties in the task item itself.</p>
<p>Examples of this model of interaction include:</p>
<ul class="simple">
<li>Spreadsheets</li>
<li>File renaming in some file managers</li>
<li>Todo lists in some websites</li>
</ul>
<p>The good:</p>
<blockquote>
<ul class="simple">
<li>Minimal user interface.</li>
<li>Obvious if the user knows about it.</li>
<li>Direct manipulation of the task item is a good metaphor.</li>
<li>Works well in small screens (netbooks, phones)</li>
</ul>
</blockquote>
<p>The bad:</p>
<blockquote>
<ul class="simple">
<li>It's not trivial to implement, for example, a due date/time editor this way (feel free to prove me wrong ;-)</li>
<li>I don't like it much for task addition. Using this paradigm usually means you create a task using placeholders like "New Task" and then need to edit it. I prefer if the user creates the tasks "fully fledged".</li>
</ul>
</blockquote>
<p>So, maybe it's a good idea to implement this, but I am fully convinced.</p>
</div>
<div class="section" id="editor-dialog">
<h2>Editor Dialog</h2>
<p>When the user triggers the "edit task" action, a dialog pops up where he can set the task's properties.</p>
<p>Examples of this model of interaction:</p>
<ul class="simple">
<li>File properties in most file managers.</li>
<li>Configuration dialogs in most applications.</li>
</ul>
<p>The good:</p>
<blockquote>
<ul class="simple">
<li>A separate dialog allows use of a richer UI, because you are not limited by the small space used for inline editing.</li>
<li>The same dialog can be used for task creating with minimal changes.</li>
<li>It would let me show you how to create a proper dialog ;-)</li>
</ul>
</blockquote>
<p>The bad:</p>
<blockquote>
<ul class="simple">
<li>Breaks the metaphor. Only do that <a class="reference external" href="http://dot.kde.org/2004/09/27/kcalc-modest-usability-improvement">when it makes sense</a></li>
<li>Popup dialogs often obscure the main window. Then if you need to check a date from another task, you need to move the dialog aside.</li>
<li>Old fashioned?</li>
</ul>
</blockquote>
<p>All in all, not a fan of this option as the main mechanism for task editing.</p>
</div>
<div class="section" id="sliding-panels">
<h2>Sliding Panels</h2>
<p>This is a more modern interaction model: when a task editing action is triggered, a gadget slides into view from one of the window edges, containing the editing widgets.</p>
<p>Examples of this interaction:</p>
<ul class="simple">
<li>Firefox's search dialog (Thankfully many apps are using this now, popup search dialogs suck)</li>
<li>Boxee's menu</li>
<li>Some phone interfaces</li>
</ul>
<p>The good:</p>
<blockquote>
<ul class="simple">
<li>Rich UI like in a dialog</li>
<li>No popups (Modern?)</li>
<li>Works for adding tasks as well.</li>
</ul>
</blockquote>
<p>The bad:</p>
<blockquote>
<ul>
<li><p class="first">Could be confusing?</p>
</li>
<li><p class="first">Could not work great in small screens (needs testing!)</p>
<p>In my screen the widget is only 300px tall, so it should fit even a small form factor screen (A 800x480 netbook?, a QVGA phone?) nicely, although in those cases it will obscure the window app completely. In such small screens "paginated" interfaces are a good idea.</p>
</li>
<li><p class="first">If the main window's form factor is small (as it is for this app), a large panel will obscure most or all the window.</p>
</li>
</ul>
</blockquote>
<p>I think I like this option best, but I am not exactly overcome with enthusiasm. It should also work well as a teaching aid, since we will need to learn to do many things to make it work correctly!</p>
<p>Anyway: if you prefer another approach for this task, you can probably reuse some of the code, and I will be happy to present your alternative implementations here, so go ahead and code them!</p>
<div class="section" id="creating-the-form">
<h3>Creating the Form</h3>
<p>As usual when we do UI work, we start with designer. This time, we will not create a Main Window, but a Widget, and laying out the necessary widgetry. Just put them roughly where you think they should go, here is how I started:</p>
<div class="figure">
<img alt="editor1.png" src="editor1.png" />
<p class="caption">Just a draft</p>
</div>
<p>What we are creating here is a classical two-column form: labels on the left, widgets on the right. Almost every program has one of these!</p>
<p>The main goals are (in no particular order):</p>
<ol class="arabic simple">
<li>It should react nicely to resizing.</li>
<li>It should not look weird. On any platform, if you can do it.</li>
<li>The purpose of each thing in the screen should be obvious.</li>
<li>It should be usable by keyboard and mouse.</li>
</ol>
<p>And here's some steps towards those goals:</p>
</div>
</div>
<div class="section" id="layouts">
<h2>Layouts</h2>
<ol class="arabic">
<li><p class="first">You can think of this form as a a 2x3 grid.
Group things that go in a single cell using a layout. In our case, we don't have a multi-widget cell.</p>
</li>
<li><p class="first">Select the contents of each "cell" and put them in a form layout.</p>
<p>There are other ways to do this, like using a grid layout. Use a form layout instead, because it will look correct (or at least <em>more</em> correct) on other platforms which have rules like "align the labels left" or "align the labels right".</p>
<div class="figure">
<img alt="editor6.png" src="editor6.png" />
<p class="caption">No form layout is <strong>always</strong> right...</p>
</div>
</li>
<li><p class="first">Right-click on the background and lay out the contents of the widget vertically.</p>
</li>
</ol>
<div class="figure">
<img alt="editor2.png" src="editor2.png" />
<p class="caption">You can see the layouts outlined in red.</p>
</div>
</div>
<div class="section" id="keyboard-handling">
<h2>Keyboard Handling</h2>
<ol class="arabic">
<li><p class="first">Configure <a class="reference external" href="http://doc.trolltech.com/4.4/designer-buddy-mode.html">buddies</a>. This associates a label to a widget. It's very important, because then you can do the next item ;-)</p>
<div class="figure">
<img alt="editor3.png" src="editor3.png" />
<p class="caption">Buddies.</p>
</div>
</li>
<li><p class="first">Add shortcuts to the labels. If you change "Due Date" for "&Due Date", the "D" will be underlined and Alt-D will jump to the date editor. If a label is not a buddy for a widget, this does not work!</p>
<div class="figure">
<img alt="editor4.png" src="editor4.png" />
<p class="caption">Shortcuts.</p>
</div>
</li>
<li><p class="first"><a class="reference external" href="http://doc.trolltech.com/4.4/designer-tab-order.html">Tab order</a>. Usually, you want the tab order to just be the top-to-bottom order of your widgets. That is usually the <a class="reference external" href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment">least surprising</a> option.</p>
<div class="figure">
<img alt="editor5.png" src="editor5.png" />
<p class="caption">Tab order.</p>
</div>
</li>
</ol>
</div>
<div class="section" id="miscelaneous">
<h2>Miscelaneous</h2>
<p>Some chores:</p>
<ol class="arabic simple">
<li>Change the object names for the important widgets (that is, not for the labels).</li>
<li>Tweak attributes (if you have a good reason!)</li>
<li>Test it in different styles, see if aything bad happens</li>
<li>Rename</li>
<li>Add compiling <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session5/editor.ui">editor.ui</a> to our <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session5/build.sh">build.sh</a></li>
</ol>
<p>Let's consider our task editing/creating widget done. Now, we need to integrate it in our main window.</p>
</div>
</div>
<div class="section" id="working-on-the-main-window">
<h1>Working on the Main Window</h1>
<div class="section" id="actions">
<h2>Actions</h2>
<p>The user interaction is done via actions. We want to enable the user to:</p>
<ul class="simple">
<li>Create a new task: action "actionNew_Task".</li>
<li>Modify an existing task: action "actionEdit_Task".</li>
</ul>
<p>For details on why and how to do this, check <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS50.html">session 4</a>, but some UI notes: I placed a separator in the Task menu before "Delete Task" to give it some space, since it's a destructive action. Same thing in the tool bar.</p>
</div>
<div class="section" id="editor-widget">
<h2>Editor Widget</h2>
<p>Since the logic form factor for our app's window is "tall and skinny", it makes sense that the editor will slide in from the bottom.</p>
<p>We will use our editor widget as a "promoted widget" in the main window. That means we do all our designer work using a plain widget as a <a class="reference external" href="http://en.wikipedia.org/wiki/Stand-in">stand-in</a> and tell designer that it should "promote" it to the real thing later.</p>
<p>If you create a widget that can be reused by others, it's probably a better idea to make it work as a <a class="reference external" href="http://doc.trolltech.com/qq/qq26-pyqtdesigner.html">custom widget</a> instead, but for us that will not be necessary.</p>
<p>First, we need to create a class that inherits QWidget, which will be our task editor widget.</p>
<p>The simplest version of such a class would look like this (suppose this is in a file called <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session5/editor.py">editor.py</a>):</p>
<table class="highlighttable"><tr><td class="linenos"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></td><td class="code"><div class="highlight"><pre> <span style="color: #BA2121; font-style: italic">"""A custom widget that edits a task's properties"""</span>
<span style="color: #408080; font-style: italic"># Import the compiled UI module</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">editorUi</span> <span style="color: #008000; font-weight: bold">import</span> Ui_Form
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">editor</span>(QtGui<span style="color: #666666">.</span>QWidget):
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>,parent,task<span style="color: #666666">=</span><span style="color: #008000">None</span>):
QtGui<span style="color: #666666">.</span>QWidget<span style="color: #666666">.</span>__init__(<span style="color: #008000">self</span>,parent)
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">=</span>Ui_Form()
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>setupUi(<span style="color: #008000">self</span>)
</pre></div>
</td></tr></table><p>How can we use it in designer?</p>
<ol class="arabic">
<li><p class="first">Right-click the task list and break its layout.</p>
</li>
<li><p class="first">Leave some room below the task list, and put a QWidget there.</p>
</li>
<li><p class="first">Select the task list and the placeholder and lay them out in a vertical splitter (it's in the tool bar)</p>
</li>
<li><p class="first">Right-click on the form's background and lay it out vertically.</p>
<div class="figure">
<img alt="window7.png" src="window7.png" />
<p class="caption">A placeholder in place.</p>
</div>
</li>
<li><p class="first">Right click on the placeholder and select "Promote to..."</p>
<ul class="simple">
<li>Base class: QWidget</li>
<li>Promoted class name: editor</li>
<li>Header file: editor</li>
<li>Click "Add"</li>
<li>Click "Promote"</li>
</ul>
<p>You will not notice any changes. But if you recompile the window and try running <tt class="docutils literal"><span class="pre">python</span> <span class="pre">main.py</span></tt> you will see this:</p>
<div class="figure">
<img alt="window8.png" src="window8.png" />
<p class="caption">Yes, the editor is there</p>
</div>
</li>
</ol>
<p>Cool, isn't it? It's not difficult to reuse these widget aggregates in other applications, or in different contexts in the same app, for example, if I later decide to create a editor <em>dialog</em> I can just reuse this widget for both places.</p>
<p>However, this is not how we want our app to appear when first started! We only want the editor to be visible when the user is actually editing a task.</p>
<p>There are several ways to achieve this, but let's go with the simplest one, hide the editor in Main.__init__:</p>
<blockquote>
<table class="highlighttable"><tr><td class="linenos"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></td><td class="code"><div class="highlight"><pre><span style="color: #408080; font-style: italic"># Create a class for our main window</span>
<span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Main</span>(QtGui<span style="color: #666666">.</span>QMainWindow):
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>):
QtGui<span style="color: #666666">.</span>QMainWindow<span style="color: #666666">.</span>__init__(<span style="color: #008000">self</span>)
<span style="color: #408080; font-style: italic"># This is always the same</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">=</span>Ui_MainWindow()
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>setupUi(<span style="color: #008000">self</span>)
<span style="color: #408080; font-style: italic"># Start with the editor hidden</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>editor<span style="color: #666666">.</span>hide()
</pre></div>
</td></tr></table></blockquote>
<div class="section" id="adding-a-new-task">
<h3>Adding a New Task</h3>
<p>Since we now have a task editing widget, we can start using it!
First, let's use it to add new tasks.</p>
<p>As seen in <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS48.html">session 2</a> in order to react to the "actionAdd_Task" we ned to implement Main.on_actionNew_Task_triggered:</p>
<table class="highlighttable"><tr><td class="linenos"><pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17</pre></td><td class="code"><div class="highlight"><pre> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">on_actionNew_Task_triggered</span>(<span style="color: #008000">self</span>,checked<span style="color: #666666">=</span><span style="color: #008000">None</span>):
<span style="color: #008000; font-weight: bold">if</span> checked <span style="color: #AA22FF; font-weight: bold">is</span> <span style="color: #008000">None</span>: <span style="color: #008000; font-weight: bold">return</span>
<span style="color: #408080; font-style: italic"># Create a dummy task</span>
task<span style="color: #666666">=</span>todo<span style="color: #666666">.</span>Task(text<span style="color: #666666">=</span><span style="color: #BA2121">"New Task"</span>)
<span style="color: #408080; font-style: italic"># Create an item reflecting the task</span>
item<span style="color: #666666">=</span>QtGui<span style="color: #666666">.</span>QTreeWidgetItem([task<span style="color: #666666">.</span>text,<span style="color: #008000">str</span>(task<span style="color: #666666">.</span>date),<span style="color: #BA2121">""</span>])
item<span style="color: #666666">.</span>setCheckState(<span style="color: #666666">0</span>,QtCore<span style="color: #666666">.</span>Qt<span style="color: #666666">.</span>Unchecked)
item<span style="color: #666666">.</span>task<span style="color: #666666">=</span>task
<span style="color: #408080; font-style: italic"># Put the item in the task list</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>list<span style="color: #666666">.</span>addTopLevelItem(item)
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>list<span style="color: #666666">.</span>setCurrentItem(item)
<span style="color: #408080; font-style: italic"># Save it in the DB</span>
todo<span style="color: #666666">.</span>saveData()
<span style="color: #408080; font-style: italic"># Open it with the editor</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>editor<span style="color: #666666">.</span>edit(item)
</pre></div>
</td></tr></table><p>What this code is doing should be almost obvious. If you don't get it, check the explanation of Main.__init__ and <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session5/editor.py">todo.py</a> in <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS47.html">session 1</a>. The non-obvious is the call to editor.edit(item). What's that?</p>
<p>Since the goal of the task editor widget is editing tasks, it needs to know what task to edit, and it needs to be able to save the modifications.</p>
<p>I will implement this using "instant apply", which means there is no save button, no apply button and no ok button. You changed the tags? Well, then the tags are changed ;-)</p>
<p>Here's the full code for the editor widget:</p>
<table class="highlighttable"><tr><td class="linenos"><pre> 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</pre></td><td class="code"><div class="highlight"><pre> <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">editor</span>(QtGui<span style="color: #666666">.</span>QWidget):
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>,parent,task<span style="color: #666666">=</span><span style="color: #008000">None</span>):
QtGui<span style="color: #666666">.</span>QWidget<span style="color: #666666">.</span>__init__(<span style="color: #008000">self</span>,parent)
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">=</span>Ui_Form()
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>setupUi(<span style="color: #008000">self</span>)
<span style="color: #408080; font-style: italic"># Start with no task item to edit</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">=</span><span style="color: #008000">None</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">edit</span>(<span style="color: #008000">self</span>,item):
<span style="color: #BA2121; font-style: italic">"""Takes an item, loads the widget with the item's</span>
<span style="color: #BA2121; font-style: italic"> task contents, shows the widget"""</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">=</span>item
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>task<span style="color: #666666">.</span>setText(<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>text)
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>done<span style="color: #666666">.</span>setChecked(<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>done)
dt<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>date
<span style="color: #008000; font-weight: bold">if</span> dt:
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>dateTime<span style="color: #666666">.</span>setDate(QtCore<span style="color: #666666">.</span>QDate(dt<span style="color: #666666">.</span>year,dt<span style="color: #666666">.</span>month,dt<span style="color: #666666">.</span>day))
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>dateTime<span style="color: #666666">.</span>setTime(QtCore<span style="color: #666666">.</span>QTime(dt<span style="color: #666666">.</span>hour,dt<span style="color: #666666">.</span>minute))
<span style="color: #008000; font-weight: bold">else</span>:
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>dateTime<span style="color: #666666">.</span>setDateTime(QtCore<span style="color: #666666">.</span>QDateTime())
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>tags<span style="color: #666666">.</span>setText(<span style="color: #BA2121">','</span><span style="color: #666666">.</span>join( t<span style="color: #666666">.</span>name <span style="color: #008000; font-weight: bold">for</span> t <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>tags))
<span style="color: #008000">self</span><span style="color: #666666">.</span>show()
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">save</span>(<span style="color: #008000">self</span>):
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">==</span><span style="color: #008000">None</span>: <span style="color: #008000; font-weight: bold">return</span>
<span style="color: #408080; font-style: italic"># Save date and time in the task</span>
d<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>dateTime<span style="color: #666666">.</span>date()
t<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>dateTime<span style="color: #666666">.</span>time()
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>date<span style="color: #666666">=</span>datetime(
d<span style="color: #666666">.</span>year(),
d<span style="color: #666666">.</span>month(),
d<span style="color: #666666">.</span>day(),
t<span style="color: #666666">.</span>hour(),
t<span style="color: #666666">.</span>minute()
)
<span style="color: #408080; font-style: italic"># Save text in the task</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>text<span style="color: #666666">=</span><span style="color: #008000">unicode</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>task<span style="color: #666666">.</span>text())
<span style="color: #408080; font-style: italic"># Save tags.</span>
tags<span style="color: #666666">=</span>[s<span style="color: #666666">.</span>strip() <span style="color: #008000; font-weight: bold">for</span> s <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">unicode</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>tags<span style="color: #666666">.</span>text())<span style="color: #666666">.</span>split(<span style="color: #BA2121">','</span>)]
<span style="color: #408080; font-style: italic"># For each tag, see if it is in the DB. If it is not, create it. If you had</span>
<span style="color: #408080; font-style: italic"># a million tags, this would be very very wrong code</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>tags<span style="color: #666666">=</span>[]
<span style="color: #008000; font-weight: bold">for</span> tag <span style="color: #AA22FF; font-weight: bold">in</span> tags:
dbTag<span style="color: #666666">=</span>todo<span style="color: #666666">.</span>Tag<span style="color: #666666">.</span>get_by(name<span style="color: #666666">=</span>tag)
<span style="color: #008000; font-weight: bold">if</span> dbTag <span style="color: #AA22FF; font-weight: bold">is</span> <span style="color: #008000">None</span>: <span style="color: #408080; font-style: italic"># Tag is new, create it</span>
<span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">"Creating tag: "</span>,tag
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>tags<span style="color: #666666">.</span>append(todo<span style="color: #666666">.</span>Tag(name<span style="color: #666666">=</span>tag))
<span style="color: #008000; font-weight: bold">else</span>:
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>tags<span style="color: #666666">.</span>append(dbTag)
<span style="color: #408080; font-style: italic"># Display the data in the item</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>setText(<span style="color: #666666">0</span>,<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>text)
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>setText(<span style="color: #666666">1</span>,<span style="color: #008000">str</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>date))
<span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>setText(<span style="color: #666666">2</span>,<span style="color: #BA2121">u','</span><span style="color: #666666">.</span>join(t<span style="color: #666666">.</span>name <span style="color: #008000; font-weight: bold">for</span> t <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>tags))
todo<span style="color: #666666">.</span>saveData()
</pre></div>
</td></tr></table><p>As you can see, there is a save() method, but we are not calling it anywhere. That's because we will use signals and slots to invoke it.</p>
<p>Open <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session5/editor.ui">editor.ui</a> in designer and right-click on the Form QWidget. Then, select "Change signals/slots...", you will see this dialog. I have added the "save()" slot.</p>
<div class="figure">
<img alt="editor7.png" src="editor7.png" />
<p class="caption">The custom save() slot.</p>
</div>
<p>I did this so I can connect signals to my save() method using designer. So, let's connect every signal marking a change in the content of the dialog to this save() slot:</p>
<div class="figure">
<img alt="editor8.png" src="editor8.png" />
<p class="caption">Signals connected to save()</p>
</div>
<p>And then, just like that, it works:</p>
<div class="figure">
<img alt="window9.png" src="window9.png" />
<p class="caption">It's alive! ALIVE!!!!!</p>
</div>
</div>
<div class="section" id="editing-tasks">
<h3>Editing Tasks</h3>
<p>This will be anticlimactic:</p>
<table class="highlighttable"><tr><td class="linenos"><pre> 1
2
3
4
5
6
7
8
9
10
11</pre></td><td class="code"><div class="highlight"><pre> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">on_actionEdit_Task_triggered</span>(<span style="color: #008000">self</span>,checked<span style="color: #666666">=</span><span style="color: #008000">None</span>):
<span style="color: #008000; font-weight: bold">if</span> checked <span style="color: #AA22FF; font-weight: bold">is</span> <span style="color: #008000">None</span>: <span style="color: #008000; font-weight: bold">return</span>
<span style="color: #408080; font-style: italic"># First see what task is "current".</span>
item<span style="color: #666666">=</span><span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>list<span style="color: #666666">.</span>currentItem()
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #AA22FF; font-weight: bold">not</span> item: <span style="color: #408080; font-style: italic"># None selected, so we don't know what to edit!</span>
<span style="color: #008000; font-weight: bold">return</span>
<span style="color: #408080; font-style: italic"># Open it with the editor</span>
<span style="color: #008000">self</span><span style="color: #666666">.</span>ui<span style="color: #666666">.</span>editor<span style="color: #666666">.</span>edit(item)
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="coming-soon">
<h1>Coming Soon</h1>
<p>This was our longest session yet, and what have we achieved? No longer is our app useless. It does, in fact, kinda work. However, it is buggy as hell. In the next session I will walk you through some informal interface testing which will show where the defects live, and we will fix a lot of them.</p>
<div class="figure">
<img alt="window10.png" src="window10.png" />
</div>
<hr class="docutils" />
<p>Here you can see what changed between the old and new versions:</p>
<table>
<tr><td width="50%">
<table>
<tr>
<td class="modified">Modified lines: </td>
<td class="modified"><a href="#F1_60">60</a>, <a href="#F1_61">61</a>, <a href="#F1_62">62</a></td>
</tr>
<tr>
<td class="added">Added line: </td>
<td class="added"><a href="#F2_25">25</a>, <a href="#F2_26">26</a>, <a href="#F2_27">27</a>, <a href="#F2_62">62</a>, <a href="#F2_63">63</a>, <a href="#F2_68">68</a>, <a href="#F2_69">69</a>, <a href="#F2_70">70</a>, <a href="#F2_71">71</a>, <a href="#F2_72">72</a>, <a href="#F2_73">73</a>, <a href="#F2_74">74</a>, <a href="#F2_75">75</a>, <a href="#F2_76">76</a>, <a href="#F2_77">77</a>, <a href="#F2_78">78</a>, <a href="#F2_79">79</a>, <a href="#F2_80">80</a>, <a href="#F2_81">81</a>, <a href="#F2_82">82</a>, <a href="#F2_83">83</a>, <a href="#F2_84">84</a>, <a href="#F2_85">85</a>, <a href="#F2_86">86</a>, <a href="#F2_87">87</a>, <a href="#F2_88">88</a>, <a href="#F2_89">89</a>, <a href="#F2_90">90</a>, <a href="#F2_91">91</a>, <a href="#F2_92">92</a>, <a href="#F2_93">93</a>, <a href="#F2_94">94</a>, <a href="#F2_95">95</a>, <a href="#F2_96">96</a>, <a href="#F2_97">97</a>, <a href="#F2_98">98</a>, <a href="#F2_99">99</a>, <a href="#F2_100">100</a>, <a href="#F2_101">101</a>, <a href="#F2_102">102</a>, <a href="#F2_103">103</a>, <a href="#F2_104">104</a>, <a href="#F2_105">105</a></td>
</tr>
<tr>
<td class="removed">Removed line: </td>
<td class="removed">None</td>
</tr>
</table>
</td>
<td width="50%">
<i>Generated by <a href="http://diff2html.tuxfamily.org"><b>diff2html</b></a><br/>
© Yves Bailly, MandrakeSoft S.A. 2001<br/>
<b>diff2html</b> is licensed under the <a
href="http://www.gnu.org/copyleft/gpl.html">GNU GPL</a>.</i>
</td></tr>
</table>
<hr/>
<table>
<tr>
<th> </th>
<th width="45%"><strong><big>session4/main.py</big></strong></th>
<th> </th>
<th> </th>
<th width="45%"><strong><big>session5/main.py</big></strong></th>
</tr>
<tr>
<td width="16"> </td>
<td>
79 lines<br/>
2300 bytes<br/>
Last modified : Sat Mar 7 02:06:29 2009<br/>
<hr/>
</td>
<td width="16"> </td>
<td width="16"> </td>
<td>
122 lines<br/>
3788 bytes<br/>
Last modified : Sun Mar 15 22:01:04 2009<br/>
<hr/>
</td>
</tr>
<tr>
<td class="linenum">1</td>
<td class="normal"># -*- coding: utf-8 -*-</td>
<td width="16"> </td>
<td class="linenum">1</td>
<td class="normal"># -*- coding: utf-8 -*-</td>
</tr>
<tr>
<td class="linenum">2</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">2</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">3</td>
<td class="normal">"""The user interface for our app"""</td>
<td width="16"> </td>
<td class="linenum">3</td>
<td class="normal">"""The user interface for our app"""</td>
</tr>
<tr>
<td class="linenum">4</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">4</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">5</td>
<td class="normal">import os,sys</td>
<td width="16"> </td>
<td class="linenum">5</td>
<td class="normal">import os,sys</td>
</tr>
<tr>
<td class="linenum">6</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">6</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">7</td>
<td class="normal"># Import Qt modules</td>
<td width="16"> </td>
<td class="linenum">7</td>
<td class="normal"># Import Qt modules</td>
</tr>
<tr>
<td class="linenum">8</td>
<td class="normal">from PyQt4 import QtCore,QtGui</td>
<td width="16"> </td>
<td class="linenum">8</td>
<td class="normal">from PyQt4 import QtCore,QtGui</td>
</tr>
<tr>
<td class="linenum">9</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">9</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">10</td>
<td class="normal"># Import the compiled UI module</td>
<td width="16"> </td>
<td class="linenum">10</td>
<td class="normal"># Import the compiled UI module</td>
</tr>
<tr>
<td class="linenum">11</td>
<td class="normal">from windowUi import Ui_MainWindow</td>
<td width="16"> </td>
<td class="linenum">11</td>
<td class="normal">from windowUi import Ui_MainWindow</td>
</tr>
<tr>
<td class="linenum">12</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">12</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">13</td>
<td class="normal"># Import our backend</td>
<td width="16"> </td>
<td class="linenum">13</td>
<td class="normal"># Import our backend</td>
</tr>
<tr>
<td class="linenum">14</td>
<td class="normal">import todo</td>
<td width="16"> </td>
<td class="linenum">14</td>
<td class="normal">import todo</td>
</tr>
<tr>
<td class="linenum">15</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">15</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">16</td>
<td class="normal"># Create a class for our main window</td>