-
Notifications
You must be signed in to change notification settings - Fork 56
/
tut2.html
1028 lines (829 loc) · 35.3 KB
/
tut2.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 2</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-2">
<h1 class="title">PyQt by Example: Session 2</h1>
<h2 class="subtitle" id="plugging-things">Plugging Things</h2>
<div class="section" id="requirements">
<h1>Requirements</h1>
<p>If you haven't yet, check <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS47.html">Session 1</a> first.</p>
<p>All files for this session are here: <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/tree/master/session2">Session 2 at GitHub</a></p>
</div>
<div class="section" id="session-2-plugging-things">
<h1>Session 2: Plugging Things</h1>
<p>In <a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS47.html">Session 1</a> we created our application's main window, a skeleton application that displays said window, and a simple <a class="reference external" href="http://elixir.ematia.de">Elixir</a> based backend.</p>
<p>We did not, however, connect both things. And connecting things is the important step we are taking in this session.</p>
<p>We will be working first on our <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session2/main.py">main.py</a>.</p>
<div class="section" id="loading-data">
<h2>Loading Data</h2>
<p>The first thing is that we need to use our backend, so we have to import todo.py.</p>
<p>We do this in <a class="reference external" href="#F2_13">line 13</a></p>
<p>Then, in <a class="reference external" href="#F2_25">line 25</a> we do the first real new work: we get the list of tasks and put them in our list.</p>
<p>Let's examine that in detail. Here is the code from lines <a class="reference external" href="#F2_25">25</a> to <a class="reference external" href="#F2_35">35</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: #408080; font-style: italic"># Let's do something interesting: load the database contents</span>
<span style="color: #408080; font-style: italic"># into our task list widget</span>
<span style="color: #008000; font-weight: bold">for</span> task <span style="color: #AA22FF; font-weight: bold">in</span> todo<span style="color: #666666">.</span>Task<span style="color: #666666">.</span>query()<span style="color: #666666">.</span>all():
tags<span style="color: #666666">=</span><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> task<span style="color: #666666">.</span>tags])
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),tags])
item<span style="color: #666666">.</span>task<span style="color: #666666">=</span>task
<span style="color: #008000; font-weight: bold">if</span> task<span style="color: #666666">.</span>done:
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>Checked)
<span style="color: #008000; font-weight: bold">else</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)
<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)
</pre></div>
</td></tr></table><p>Remember our backend, <a class="reference external" href="http://github.com/ralsina/pyqt-by-example/blob/master/session2/todo.py">todo.py</a>? In it we defined Task objects that are stored in a database. If you are not familiar with <a class="reference external" href="http://elixir.ematia.de">Elixir</a>, <tt class="docutils literal"><span class="pre">todo.Task.query().all()</span></tt> gets a list of all Task objects from the database.</p>
<p>Then we assign the tags separated by commas to <tt class="docutils literal"><span class="pre">tags</span></tt>, which will look something like <tt class="docutils literal"><span class="pre">"home,important"</span></tt>.</p>
<p>And now, we see our first Qt-related code.</p>
<p>First: self.ui.list is a widget. Everything we put on the window using designer is accessible using self.ui.object_name and the object_name is set using designer. If you right-click on the big widget in our window you can see that the widget's object name is <strong>list</strong>:</p>
<div class="figure">
<img alt="object_name.png" src="object_name.png" />
<p class="caption">The object name is <strong>list</strong>.</p>
</div>
<p>You can also see it (and change it) in the Object Inspector and in the Property Editor.</p>
<p>That is not just a widget. It's a QTreeWidget, useful for showing fancy multi-column lists and trees.</p>
<p>You can read a lot about this widget in its <a class="reference external" href="http://doc.trolltech.com/4.4/qtreewidget.html#details">manual</a> but let's do the very short version:</p>
<ul class="simple">
<li>We create <a class="reference external" href="http://doc.trolltech.com/4.4/qtreewidgetitem.html">QTreeWidgetItem</a> objects. These take a list of strings, which are displayed in the widget's columns. In this case, we are adding the task text ("Buy groceries"), the due date, and the tags.</li>
<li>We set item.task to task. This is so later on we know what task is described by a specific item. Not the most elegant way, perhaps, but by far the easiest.</li>
<li>We add each item to the widget using <a class="reference external" href="http://doc.trolltech.com/4.4/qtreewidget.html#addTopLevelItem">addTopLevelItem</a> so they are all at the same level (not hierarchical, as childs and parents)</li>
<li>It will work as a list</li>
</ul>
<p>Then, if the task is done (<tt class="docutils literal"><span class="pre">task.done==True</span></tt>) we make a checkmark next to the task. If it's not done, we do not.</p>
<p>For many simple programs, this is all you need to know about QTreeWidget. It's a rather complex widget, and very powerful, but this will do for our task. Investigate!</p>
<p>So, what does it do? Let's run <tt class="docutils literal"><span class="pre">python</span> <span class="pre">main.py</span></tt> and find out!</p>
<div class="figure">
<img alt="window3.png" src="window3.png" />
<p class="caption">The task list with our sample tasks.</p>
</div>
<p>If your window is empty, try running <tt class="docutils literal"><span class="pre">python</span> <span class="pre">todo.py</span></tt> first, which will create some sample tasks.</p>
<p>You can even check a task to mark it done! That's because we have also implemented (partial) saving of modified tasks...</p>
</div>
<div class="section" id="saving-data">
<h2>Saving Data</h2>
<p>So, what we want is that when the user clicks on the checkbox, the task should be modified accordingly, and stored in the database.</p>
<p>In most toolkits you would be looking for callbacks. Here things are a bit different. When a Qt widget wants to make you notice something, like "The user clicked this button" or "The Help menu is activated", or whatever, they <em>emit signals</em> (go along for a minute).</p>
<p>In particular, here we are interested in the itemChanged signal of QTreeWidget:</p>
<blockquote>
<p>QTreeWidget.itemChanged ( item, column ) [signal]</p>
<p>This signal is emitted when the contents of the column in the specified item changes.</p>
</blockquote>
<p>And, whenever you click on the checkbox, this signal is emitted. Why's that important? Because you can connect your own code to a signal, so that whenever that signal is emitted, your code is executed! Your code is called a <em>slot</em> in Qt slang.</p>
<p>It's like a callback, except that:</p>
<ol class="loweralpha simple">
<li>The signal doesn't know what is connected to it (or if there is anything connected at all)</li>
<li>You can connect as many slots to a signal as you want.</li>
</ol>
<p>This helps keep your code <a class="reference external" href="http://en.wikipedia.org/wiki/Loosely_coupled">loosely coupled</a>.</p>
<p>We <em>could</em> define a method in the Main class, and connect it to the itemChanged signal, but there is no need because we can use AutoConnect. If you add to Main a method with a specific name, it will be connected to that signal. The name is on_objectname_signalname.</p>
<p>Here is the code (lines <a class="reference external" href="#F2_37">37</a> to <a class="reference external" href="#F2_42">42</a>):</p>
<table class="highlighttable"><tr><td class="linenos"><pre>1
2
3
4
5
6</pre></td><td class="code"><div class="highlight"><pre><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">on_list_itemChanged</span>(<span style="color: #008000">self</span>,item,column):
<span style="color: #008000; font-weight: bold">if</span> item<span style="color: #666666">.</span>checkState(<span style="color: #666666">0</span>):
item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>done<span style="color: #666666">=</span><span style="color: #008000">True</span>
<span style="color: #008000; font-weight: bold">else</span>:
item<span style="color: #666666">.</span>task<span style="color: #666666">.</span>done<span style="color: #666666">=</span><span style="color: #008000">False</span>
todo<span style="color: #666666">.</span>saveData()
</pre></div>
</td></tr></table><p>See how we use item.task to reflect the item's checkState (whether it's checked or not) into the task state?</p>
<p>The <tt class="docutils literal"><span class="pre">todo.saveData()</span></tt> at the end makes sure all data is saved in the database via Elixir. That function is a bit gnarly because I wanted it to work with two different versions of Elixir.</p>
<p>AutoConnect is what you will use 90% of the time to add behaviour to your application. Most of the time, you will just create the window, add the widgets, and hook signals via AutoConnect.</p>
<p>In some occasions that will not be enough. But we are not there yet.</p>
<p>This was a rather short session, but be prepared for the next one: we will be doing <strong>Important Stuff with Designer</strong> (TM)!</p>
<p>In the meantime, you may want to check these pages:</p>
<ul class="simple">
<li>Learn about <a class="reference external" href="http://doc.trolltech.com/4.4/gallery.html">widgets</a> available in Qt</li>
<li>Learn about <a class="reference external" href="http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#signal-and-slot-support">signals and slots</a></li>
<li><a class="reference external" href="http://lateral.netmanagers.com.ar/stories/BBS49.html">Session 3</a></li>
</ul>
<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_34">34</a></td>
</tr>
<tr>
<td class="added">Added line: </td>
<td class="added"><a href="#F2_13">13</a>, <a href="#F2_14">14</a>, <a href="#F2_15">15</a>, <a href="#F2_25">25</a>, <a href="#F2_26">26</a>, <a href="#F2_27">27</a>, <a href="#F2_28">28</a>, <a href="#F2_29">29</a>, <a href="#F2_30">30</a>, <a href="#F2_31">31</a>, <a href="#F2_32">32</a>, <a href="#F2_33">33</a>, <a href="#F2_34">34</a>, <a href="#F2_35">35</a>, <a href="#F2_36">36</a>, <a href="#F2_37">37</a>, <a href="#F2_38">38</a>, <a href="#F2_39">39</a>, <a href="#F2_40">40</a>, <a href="#F2_41">41</a>, <a href="#F2_42">42</a>, <a href="#F2_43">43</a>, <a href="#F2_44">44</a>, <a href="#F2_46">46</a>, <a href="#F2_47">47</a>, <a href="#F2_48">48</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>session1/main.py</big></strong></th>
<th> </th>
<th> </th>
<th width="45%"><strong><big>session2/main.py</big></strong></th>
</tr>
<tr>
<td width="16"> </td>
<td>
34 lines<br/>
754 bytes<br/>
Last modified : Mon Mar 2 01:29:24 2009<br/>
<hr/>
</td>
<td width="16"> </td>
<td width="16"> </td>
<td>
60 lines<br/>
1557 bytes<br/>
Last modified : Thu Mar 5 02:03:03 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"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_13">13</a></td>
<td class="added"># Import our backend</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_14">14</a></td>
<td class="added">import todo</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_15">15</a></td>
<td class="added"></td>
</tr>
<tr>
<td class="linenum">13</td>
<td class="normal"># Create a class for our main window</td>
<td width="16"> </td>
<td class="linenum">16</td>
<td class="normal"># Create a class for our main window</td>
</tr>
<tr>
<td class="linenum">14</td>
<td class="normal">class Main(QtGui.QMainWindow):</td>
<td width="16"> </td>
<td class="linenum">17</td>
<td class="normal">class Main(QtGui.QMainWindow):</td>
</tr>
<tr>
<td class="linenum">15</td>
<td class="normal"> def __init__(self):</td>
<td width="16"> </td>
<td class="linenum">18</td>
<td class="normal"> def __init__(self):</td>
</tr>
<tr>
<td class="linenum">16</td>
<td class="normal"> QtGui.QMainWindow.__init__(self)</td>
<td width="16"> </td>
<td class="linenum">19</td>
<td class="normal"> QtGui.QMainWindow.__init__(self)</td>
</tr>
<tr>
<td class="linenum">17</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">20</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">18</td>
<td class="normal"> # This is always the same</td>
<td width="16"> </td>
<td class="linenum">21</td>
<td class="normal"> # This is always the same</td>
</tr>
<tr>
<td class="linenum">19</td>
<td class="normal"> self.ui=Ui_MainWindow()</td>
<td width="16"> </td>
<td class="linenum">22</td>
<td class="normal"> self.ui=Ui_MainWindow()</td>
</tr>
<tr>
<td class="linenum">20</td>
<td class="normal"> self.ui.setupUi(self)</td>
<td width="16"> </td>
<td class="linenum">23</td>
<td class="normal"> self.ui.setupUi(self)</td>
</tr>
<tr>
<td class="linenum">21</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">24</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_25">25</a></td>
<td class="added"> # Let's do something interesting: load the database contents</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_26">26</a></td>
<td class="added"> # into our task list widget</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_27">27</a></td>
<td class="added"> for task in todo.Task.query().all():</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_28">28</a></td>
<td class="added"> tags=','.join([t.name for t in task.tags])</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_29">29</a></td>
<td class="added"> item=QtGui.QTreeWidgetItem([task.text,str(task.date),tags])</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_30">30</a></td>
<td class="added"> item.task=task</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_31">31</a></td>
<td class="added"> if task.done:</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_32">32</a></td>
<td class="added"> item.setCheckState(0,QtCore.Qt.Checked)</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_33">33</a></td>
<td class="added"> else:</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_34">34</a></td>
<td class="added"> item.setCheckState(0,QtCore.Qt.Unchecked)</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_35">35</a></td>
<td class="added"> self.ui.list.addTopLevelItem(item)</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_36">36</a></td>
<td class="added"></td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_37">37</a></td>
<td class="added"> def on_list_itemChanged(self,item,column):</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_38">38</a></td>
<td class="added"> if item.checkState(0):</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_39">39</a></td>
<td class="added"> item.task.done=True</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_40">40</a></td>
<td class="added"> else:</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_41">41</a></td>
<td class="added"> item.task.done=False</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_42">42</a></td>
<td class="added"> todo.saveData()</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_43">43</a></td>
<td class="added"></td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_44">44</a></td>
<td class="added"></td>
</tr>
<tr>
<td class="linenum">22</td>
<td class="normal">def main():</td>
<td width="16"> </td>
<td class="linenum">45</td>
<td class="normal">def main():</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_46">46</a></td>
<td class="added"> # Init the database before doing anything else</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_47">47</a></td>
<td class="added"> todo.initDB()</td>
</tr>
<tr>
<td class="linenum"> </td>
<td class="added"> </td>
<td width="16"> </td>
<td class="linenum"><a name="F2_48">48</a></td>
<td class="added"></td>
</tr>
<tr>
<td class="linenum">23</td>
<td class="normal"> # Again, this is boilerplate, it's going to be the same on</td>
<td width="16"> </td>
<td class="linenum">49</td>
<td class="normal"> # Again, this is boilerplate, it's going to be the same on</td>
</tr>
<tr>
<td class="linenum">24</td>
<td class="normal"> # almost every app you write</td>
<td width="16"> </td>
<td class="linenum">50</td>
<td class="normal"> # almost every app you write</td>
</tr>
<tr>
<td class="linenum">25</td>
<td class="normal"> app = QtGui.QApplication(sys.argv)</td>
<td width="16"> </td>
<td class="linenum">51</td>
<td class="normal"> app = QtGui.QApplication(sys.argv)</td>
</tr>
<tr>
<td class="linenum">26</td>
<td class="normal"> window=Main()</td>
<td width="16"> </td>
<td class="linenum">52</td>
<td class="normal"> window=Main()</td>
</tr>
<tr>
<td class="linenum">27</td>
<td class="normal"> window.show()</td>
<td width="16"> </td>
<td class="linenum">53</td>
<td class="normal"> window.show()</td>
</tr>
<tr>
<td class="linenum">28</td>
<td class="normal"> # It's exec_ because exec is a reserved word in Python</td>
<td width="16"> </td>
<td class="linenum">54</td>
<td class="normal"> # It's exec_ because exec is a reserved word in Python</td>
</tr>
<tr>
<td class="linenum">29</td>
<td class="normal"> sys.exit(app.exec_())</td>
<td width="16"> </td>
<td class="linenum">55</td>
<td class="normal"> sys.exit(app.exec_())</td>
</tr>
<tr>
<td class="linenum">30</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">56</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">31</td>
<td class="normal"></td>
<td width="16"> </td>
<td class="linenum">57</td>
<td class="normal"></td>
</tr>
<tr>
<td class="linenum">32</td>
<td class="normal">if __name__ == "__main__":</td>
<td width="16"> </td>
<td class="linenum">58</td>
<td class="normal">if __name__ == "__main__":</td>
</tr>
<tr>
<td class="linenum">33</td>
<td class="normal"> main()</td>