1
00:00:00,720 --> 00:00:05,790
Hey, in this video, I'll show you the solution to the search functionality, which allows users to

2
00:00:05,790 --> 00:00:09,630
search for a pseudo name.

3
00:00:09,630 --> 00:00:14,040
And when they press on search, that student will be selected on the table.

4
00:00:14,460 --> 00:00:19,500
It can be one student with that name or multiple students having the same name.

5
00:00:19,740 --> 00:00:20,430
Right.

6
00:00:20,460 --> 00:00:21,930
Let me show you the code.

7
00:00:21,960 --> 00:00:29,310
Now, since I'm using Gates, I'll use the get compare functionality by clicking on the last commits

8
00:00:29,310 --> 00:00:30,600
and then on Main.

9
00:00:30,900 --> 00:00:38,850
So the compare functionality to show you the actual new code that I added because this allows us to

10
00:00:38,850 --> 00:00:41,600
see the new code highlighted in green.

11
00:00:41,610 --> 00:00:48,170
So that's the old code we were coding before and this is the code that you haven't seen yet.

12
00:00:48,180 --> 00:00:50,760
So the solution of the project.

13
00:00:50,760 --> 00:00:56,640
So first I added this line, the Q class, which will need later on in the code.

14
00:00:57,390 --> 00:01:01,590
Then next in line 17, you see this is a new line I added.

15
00:01:01,590 --> 00:01:04,560
I created the added menu item.

16
00:01:05,400 --> 00:01:10,220
So that's this edit menu item.

17
00:01:10,230 --> 00:01:10,890
Right.

18
00:01:11,200 --> 00:01:12,030
And then.

19
00:01:13,690 --> 00:01:17,770
I created a search action using Q action.

20
00:01:17,770 --> 00:01:23,470
So Search and I connected that to edit menu item using an ADD action methods.

21
00:01:23,860 --> 00:01:27,010
So that's the search action we created up here.

22
00:01:27,910 --> 00:01:38,050
And then when the user selects that search action, the self, the search method will be called.

23
00:01:39,580 --> 00:01:41,500
Now serve that search.

24
00:01:41,500 --> 00:01:44,560
We are in main window here, right?

25
00:01:44,560 --> 00:01:48,580
So self represents the main window instance.

26
00:01:48,580 --> 00:01:50,800
Therefore search is.

27
00:01:52,290 --> 00:01:55,380
This method is here the method of main window.

28
00:01:55,380 --> 00:01:56,820
So this is also a new method.

29
00:01:56,820 --> 00:01:57,440
I added.

30
00:01:57,450 --> 00:02:03,480
Here I instantiate a search dialog class which I created further down.

31
00:02:04,560 --> 00:02:07,830
So that's a class this class creates.

32
00:02:09,120 --> 00:02:11,160
That search dialog window.

33
00:02:11,160 --> 00:02:12,840
So this one in here.

34
00:02:14,400 --> 00:02:18,390
So this dialog window has this in its methods.

35
00:02:18,750 --> 00:02:24,150
And then we set the parameters like the size and the title of the dialog window.

36
00:02:24,420 --> 00:02:31,620
I used a vertical box layout since we only have these two widgets stacked vertically.

37
00:02:31,860 --> 00:02:34,950
So one widget is a queue line edit widget.

38
00:02:35,340 --> 00:02:38,280
It's created in here, that first one.

39
00:02:38,280 --> 00:02:41,100
And the second one is the button widget.

40
00:02:41,910 --> 00:02:44,990
This button here and then we set the layout.

41
00:02:45,000 --> 00:02:48,900
So this layout is set as the layout of.

42
00:02:49,560 --> 00:02:52,110
The search dialogue, right?

43
00:02:52,680 --> 00:02:54,630
That was in its method.

44
00:02:54,630 --> 00:03:01,320
Now we go to the most important methods of all the search methods.

45
00:03:01,830 --> 00:03:05,420
Let me give you more room here to view.

46
00:03:05,430 --> 00:03:06,790
That's the entire method.

47
00:03:06,900 --> 00:03:09,300
That's the entire code of the method.

48
00:03:09,780 --> 00:03:13,950
So let me go through each line one by one what this method does.

49
00:03:13,980 --> 00:03:21,840
First of all, we grab the name that the user enters in the box here, so if they enter John Smith,

50
00:03:22,290 --> 00:03:27,210
then name variable will be equal to John Smith, that string.

51
00:03:27,600 --> 00:03:30,420
Then we establish a connection to the database.

52
00:03:30,420 --> 00:03:33,180
We create a cursor and then.

53
00:03:34,300 --> 00:03:38,710
Let me expand here so we hear the cursor.

54
00:03:38,860 --> 00:03:43,420
Then we exit a query, an SQL query.

55
00:03:43,420 --> 00:03:49,240
This is the query select all from students from the students table.

56
00:03:49,240 --> 00:03:56,440
But we have a condition here where the name column is equal to the name variable.

57
00:03:56,440 --> 00:03:58,360
The name variable is this one here.

58
00:03:58,360 --> 00:04:03,730
So if the user entered John Smith, that will be equal to John Smith.

59
00:04:03,730 --> 00:04:08,290
So we select all those rows and we stole them in result.

60
00:04:08,560 --> 00:04:11,320
This will be in the form of a generator.

61
00:04:11,320 --> 00:04:17,980
So we need to convert them into a list and what we get is a list of tuples.

62
00:04:17,980 --> 00:04:19,060
I can show you that.

63
00:04:19,060 --> 00:04:25,060
So let's say that we search for John Smith, we press on, search in the command line.

64
00:04:25,330 --> 00:04:28,090
We should see that being printed out.

65
00:04:28,090 --> 00:04:31,990
So we have a list of two tuples, one tuple here.

66
00:04:31,990 --> 00:04:38,080
The first occurrence of John Smith with ID one and the second occurrence of John Smith with ID 12.

67
00:04:38,920 --> 00:04:42,730
So that line was printed out by this.

68
00:04:44,150 --> 00:04:45,440
Print function.

69
00:04:45,500 --> 00:04:51,710
Then this line here items is equal to main window dot, table, dot fine item.

70
00:04:52,340 --> 00:04:56,090
What it does is it refers to main window.

71
00:04:56,090 --> 00:04:57,320
What is main window?

72
00:04:57,320 --> 00:05:00,230
Main window is a variable we created in here.

73
00:05:00,440 --> 00:05:04,220
This variable refers to the main window instance.

74
00:05:04,220 --> 00:05:04,820
Right?

75
00:05:04,850 --> 00:05:07,310
A main window is the class.

76
00:05:08,230 --> 00:05:15,120
The class of the main window rights, and this class has a table attribute rate.

77
00:05:15,130 --> 00:05:17,830
So by doing.

78
00:05:21,930 --> 00:05:23,460
This main window.

79
00:05:23,460 --> 00:05:24,300
That table.

80
00:05:24,300 --> 00:05:27,090
We access the table over the main window.

81
00:05:27,120 --> 00:05:29,040
The actual table object.

82
00:05:29,550 --> 00:05:34,800
And then this table object is a Q table widget type, right?

83
00:05:35,370 --> 00:05:37,290
It is an instance of that type.

84
00:05:37,290 --> 00:05:46,290
Therefore, that type Q table widget does have a find items method which allows you to enter these two

85
00:05:46,290 --> 00:05:47,350
attributes.

86
00:05:47,400 --> 00:05:50,880
The first one is the name you want to search.

87
00:05:51,000 --> 00:05:53,850
So that's John Smith and this is a flag.

88
00:05:54,060 --> 00:05:57,540
So I imported cute up in the code.

89
00:05:57,750 --> 00:05:59,770
That's the first thing I showed to you.

90
00:05:59,790 --> 00:06:01,830
And then that match flag.

91
00:06:01,830 --> 00:06:03,150
That match fixed strings.

92
00:06:03,150 --> 00:06:05,370
So that will match that string.

93
00:06:05,760 --> 00:06:08,460
And that will give us some sort of.

94
00:06:09,430 --> 00:06:13,900
Generator objects and we need to iterate over that generator.

95
00:06:13,990 --> 00:06:17,620
And now each item looks like this.

96
00:06:17,620 --> 00:06:20,690
So that's the first item and that's the second item.

97
00:06:20,710 --> 00:06:27,220
So those are basically items sales over the table, in other words.

98
00:06:27,910 --> 00:06:29,410
But they look like this.

99
00:06:29,410 --> 00:06:37,030
So what we need to do is you refer to main window again, that table dot item.

100
00:06:37,450 --> 00:06:43,900
So item is again a method of table, and then this method gets two arguments.

101
00:06:45,060 --> 00:06:48,780
This is one argument and this is the second argument.

102
00:06:49,020 --> 00:06:57,020
Now, item zero gives us the index of the row of the current row, right?

103
00:06:57,020 --> 00:06:58,170
So for example, zero.

104
00:06:58,180 --> 00:07:03,450
So this will be zero for John for the first occurrence of John Smith.

105
00:07:03,600 --> 00:07:07,620
And then one is the index of the column.

106
00:07:07,620 --> 00:07:08,280
Right.

107
00:07:09,400 --> 00:07:17,920
So we want to select the sell in column with index one and to row with index zero.

108
00:07:18,340 --> 00:07:21,310
So the first row has an index of zero, Right?

109
00:07:21,340 --> 00:07:22,750
This is just the idea here.

110
00:07:22,750 --> 00:07:24,280
One, two, three, etc..

111
00:07:24,790 --> 00:07:29,440
So these are the coordinates basically zero and one.

112
00:07:29,830 --> 00:07:31,680
And then we say set selected.

113
00:07:31,690 --> 00:07:34,430
So we select that particular cell.

114
00:07:34,450 --> 00:07:37,170
So that's represents a cell rate.

115
00:07:37,180 --> 00:07:39,190
And then we say select that cell.

116
00:07:39,490 --> 00:07:42,670
And since we have two cells, when we search for John Smith.

117
00:07:43,840 --> 00:07:45,340
This loop will run to time.

118
00:07:45,340 --> 00:07:46,550
So two items, right?

119
00:07:46,570 --> 00:07:47,770
It will run two times.

120
00:07:47,770 --> 00:07:51,760
We have two items and it will select two cells.

121
00:07:52,030 --> 00:07:52,610
And that's it.

122
00:07:52,630 --> 00:07:53,900
We close the cursor.

123
00:07:53,920 --> 00:07:55,330
We close the connection.

124
00:07:55,570 --> 00:07:56,890
And that's all the code.

125
00:07:56,900 --> 00:07:58,810
So thanks for following.

126
00:07:58,900 --> 00:08:02,560
We have more interesting code to write, so I'll see you in the next one.

