xuetao
2025-05-19 eb0256a057c3aa03eca9726f7e6e36f1a80c0345
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
#include "errorexample.h"
 
#include <QFile>
#include <QLabel>
#include <QPushButton>
errorexample::errorexample() {
  // 1.δ°üº¬±ØÒªµÄÍ·Îļþ
  // ´íÎó£ºÈ±ÉÙ #include <QLabel>
  QLabel label("Hello");  // ±àÒë´íÎó£º'QLabel' was not declared in this scope
  label.show();
 
  //-----------Ö¸Õë³£¼ûÎÊÌâ---------------
 
  //¢Ù¿ÕÖ¸Õë½âÒýÓÃ
  QWidget *widget = nullptr;
  widget->show();  //¶ÔֵΪnullptrµÄÖ¸Õë½øÐзÃÎʲÙ×÷,»áÒý·¢±ÀÀ£
 
  //½â¾ö°ì·¨£º
  QWidget *widget1 = nullptr;
  if (widget1) {  //ʹÓÃÖ¸Õëǰ£¬ÏÈÈ·ÈÏÆäÊÇ·ñΪnullptr//ÕâÖÖÅжϷ½Ê½ÕýÈ·Âð£¿byxue
    widget1->show();
  }
 
  //¢ÚÒ°Ö¸Õë
  //Ö¸ÕëËùÖ¸ÏòµÄÄÚ´æÒѾ­±»ÊÍ·Å£¬µ«ºóÐøÈÔÔÚʹÓøÃÖ¸Õë
  QWidget *widget2 = new QWidget;
  delete widget2;
  widget2->show();  //´Ëʱwidget2ÒѾ­±ä³ÉÒ°Ö¸Õë
 
  //½â¾ö°ì·¨:
  //¶ÔÏóÊÍ·ÅÖ®ºó£¬¼°Ê±½«Ö¸ÕëÖÃΪnullptr(ÀûÓàQt
  //¶ÔÏóµÄ¸¸×Ó¹ØÏµ»úÖÆÀ´×Ô¶¯¹ÜÀíÄÚ´æ)
  QWidget *widget3 = new QWidget;
  delete widget3;
  widget3 = nullptr;  //±ÜÃâÒ°Ö¸Õë
 
  //¢ÛÄÚ´æÐ¹Â¶£ºÎ´ÊͷŶ¯Ì¬·ÖÅäµÄÄÚ´æ
  void createLeak() {//ÕâÖÖ·½·¨µÄ¶¨Ò巽ʽ¾ÍÓÐÎÊÌ⣬²»ÄÜÔÚ·½·¨ÀïÔÙ¶¨Òå·½·¨£¬½¨Òé°ÑÄÚ´æÐ¹Â¶×¨ÃÅдһ¸ö²âÊÔÎļþ
    int *ptr = new int(10);  //·ÖÅäºóδÊÍ·Å
  }
  //½â¾ö°ì·¨£º
  void createLeak() {//ÕâÖÖ·½·¨µÄ¶¨Ò巽ʽ¾ÍÓÐÎÊÌ⣬²»ÄÜÔÚ·½·¨ÀïÔÙ¶¨Òå·½·¨
    int *ptr = new int(10);  //·ÖÅäºóδÊÍ·Å
    delete ptr;
    ptr = nullptr;
  }
 
  //¢Ü¸¸×Ó¹ØÏµÎóÓÃ
  QWidget *parent = new QWidget;
  QPushButton *button = new QPushButton(parent);
  delete button;  // ÊÖ¶¯É¾³ý×Ó¶ÔÏó
  delete parent;  // ±ÀÀ££¡¸¸¶ÔÏóÊÔͼÔÙ´Îɾ³ýÒÑÊͷŵĠbutton
 
  //½â¾ö°ì·¨£ºÒÀÀµ¶ÔÏóÊ÷×Ô¶¯¹ÜÀí
  QWidget *parent2 = new QWidget;
  QPushButton *button2 = new QPushButton(parent);
  delete parent2;  // ×Ô¶¯É¾³ýËùÓÐ×Ó¶ÔÏ󣨰üÀ¨ button£©
 
  //¢ÝÖÇÄÜÖ¸ÕëÓë¶ÔÏóÊ÷³åÍ»
  //»ìºÏʹÓø¸¶ÔÏóºÍÖÇÄÜÖ¸Õë
  QWidget *parent3 = new QWidget;
  // ´íÎ󣺸¸¶ÔÏóÒѹÜÀí button£¬ÖÇÄÜÖ¸Õë»áµ¼ÖÂË«ÖØÊÍ·Å
  std::unique_ptr<QPushButton> button3(new QPushButton(parent));//ÖÇÄÜÖ¸ÕëµÄʹÓÃÓÐÎÊÌâ
 
  //½â¾ö°ì·¨1£º½öÓø¸¶ÔÏó¹ÜÀí
  QWidget *parent4 = new QWidget;
  QPushButton *button4 = new QPushButton(parent4);  // ¸¸¶ÔÏó¸ºÔðÊÍ·Å
 
  //½â¾ö°ì·¨2£ºÎÞ¸¸¶ÔÏóʱʹÓÃÖÇÄÜÖ¸Õë
  std::unique_ptr<QPushButton> button5 = std::make_unique<QPushButton>();
 
  //¢ÞÈÝÆ÷ÖеÄÖ¸Õë¹ÜÀí
  // Íü¼ÇÊÍ·ÅfilesÊý×é¼°ÆäÖеÄÖ¸ÕëËùÖ¸ÏòµÄÄÚ´æ
  QFile *files = new QFile[10];
  for (int i = 0; i < 10; ++i) {
    files[i].setFileName("file" + QString::number(i));
  }
 
  //½â¾ö°ì·¨£º
  QFile **files1 = new QFile *[10];
  for (int i = 0; i < 10; ++i) {
    files[i].setFileName("file" + QString::number(i));
  }
  for (int i = 0; i < 10; ++i) {
    delete files1[i];
  }
  delete[] files;
 
  //-----------³£¼ûÂß¼­´íÎó---------------
 
  //¢ÙÎÞÏÞÑ­»·
  //´íÎ󣺣¨ÎÞÖÕÖ¹Ìõ¼þ£©
  for (int i = 0; i < 10;) {
    qDebug() << "Looping...";
    // Íü¼Çдi++
  }
 
  //´íÎó£ºËÀÑ­»·£¨Ìõ¼þºãÎªÕæ£©
  while (true) {
    //´úÂë¿é          Î´ÌṩÍ˳ö»úÖÆ
  }
 
  //½â¾ö°ì·¨
  // Ôö¼Ói++
  for (int i = 0; i < 10; i++) {
    qDebug() << "Looping..." << i;
  }
 
  //ʹÓÃbreak»òreturnÍ˳öÑ­»·
  while (true) {
    if (data.isEmpty()) break;  // Ìõ¼þÂú×ãʱÍ˳ö
  }
 
  //¢ÚÊý¾Ý/ÈÝÆ÷Ô½½ç·ÃÎÊ
  // ´íÎ󣺷ÃÎÊÔ½½ç£¨Ë÷Òý0~9£¬µ«Ñ­»·µ½i=10£©
  QVector<int> numbers(10);
  for (int i = 0; i <= 10; ++i) {  // Ó¦Îªi < 10
    numbers[i] = i;                // i=10ʱԽ½ç
    // ´íÎó£ºÊ¹ÓÿÕÈÝÆ÷
    QStringList names;
    qDebug() << names[0];  // ·ÃÎÊ¿ÕÈÝÆ÷µÄµÚÒ»¸öÔªËØ
 
    //½â¾ö°ì·¨
    //ʹÓð²È«µÄ·ÃÎÊ·½·¨£¨Èçat()´úÌæ[]£©
    qDebug() << numbers.at(10);
    //¼ì²éÈÝÆ÷ÊÇ·ñΪ¿Õ
    if (!names.isEmpty()) {
      qDebug() << names[0];
    }
 
    // 2.δ³õʼ»¯Ö¸Õë
    QLabel *label4;
    label4->setText("Hello");  // ´íÎó£ºÊ¹ÓÃδ³õʼ»¯µÄÖ¸Õë
 
    // ÐÞ¸´£º³õʼ»¯Ö¸Õë
    QLabel *label5 = new QLabel;
 
    // 3.¸¸¶ÔÏó¹ÜÀí²»µ±µ¼ÖÂÄÚ´æÐ¹Â¶
    QLabel *label1 = new QLabel("Memory Leak");
    label1->show();
 
    // ÕýÈ·×ö·¨£ºÉèÖø¸¶ÔÏó£¬Qt»á×Ô¶¯¹ÜÀíÄÚ´æ
    QWidget *parent = new QWidget;
    QLabel *managedLabel = new QLabel("Managed", parent);
 
    // 4.ʹÓÃÒÑɾ³ýµÄ¶ÔÏó
    QLabel *label2 = new QLabel("Delete Me");
    delete label2;
 
    // ´íÎ󣺷ÃÎÊÒÑɾ³ýµÄ¶ÔÏó
    label2->setText("Oops");  // Î´¶¨ÒåÐÐΪ
 
    // 5.×Ö·û´®±àÂëÎÊÌâ
    QLabel *label3 = new QLabel;
    // ´íÎó£ºÓ²±àÂë·ÇASCII×Ö·û´®
    label3->setText("ÖÐÎÄÎı¾");  // ¿ÉÄÜÏÔʾÂÒÂ룬ȡ¾öÓÚÔ´Îļþ±àÂë
 
    // ÕýÈ·×ö·¨£ºÊ¹ÓÃtr()½øÐз­Òë
    label3->setText(tr("ÖÐÎÄÎı¾"));
 
    // 6.ÈÝÆ÷Ô½½ç·ÃÎÊ
    QList<int> list = {1, 2, 3};
 
    // ´íÎó£ºÔ½½ç·ÃÎÊ
    int value = list[3];  // Ë÷Òý×î´óΪ2
 
    // ÕýÈ·×ö·¨£ºÊ¹ÓÃat()²¢¼ì²é±ß½ç
    if (list.size() > 3) {
      int safeValue = list.at(3);  // at()»áÔÚÔ½½çʱÅ׳öÒì³£
    }
 
    // 7.ȱʧÖÕÖ¹Ìõ¼þµÄForÑ­»·
    for (int i = 0;; i++) {  // ´íÎó£ºÎÞÖÕÖ¹Ìõ¼þ
                             //Ñ­»·Ìå
    }
 
    // Õýȷд·¨£º
    for (int i = 0; i < 10; i++) {  // Ìí¼ÓÖÕÖ¹Ìõ¼þ
                                    //Ñ­»·Ìå
    }
 
    // 8.ÎóÓà= ´úÌæ ==
    if (x = 5) {  // Î󽫸³Öµ²Ù×÷µ±×÷±È½Ï£¬Ìõ¼þʼÖÕΪ true£¨³ý·Ç x ÊDz¼¶ûÀàÐÍ£©
                  // ...
    }
 
    // 9.ʼþ´¦Àíδµ÷ÓûùÀàʵÏÖ
    void CustomWidget::paintEvent(QPaintEvent * event) {
      // ´íÎó£ºÎ´µ÷ÓûùÀàʵÏÖ
      QPainter painter(this);
      // È±ÉÙ£ºQWidget::paintEvent(event);
    }
 
    // 10.ÐźŲ۲ÎÊý²»Æ¥Åä
    // ´íÎó£ºÐźŲÎÊýÓë²Û²ÎÊýÀàÐͲ»Æ¥Åä
    connect(sender, &Sender::valueChanged(int), receiver,
            &Receiver::updateValue(QString));
 
    // ÐÞ¸´£ºÈ·±£²ÎÊýÀàÐÍÒ»ÖÂ
    connect(sender, &Sender::valueChanged(int), receiver,
            &Receiver::updateValue(int));
 
    // 11.δʵÏÖ´¿Ð麯Êý
    class MyInterface {
     public:
      virtual void pureVirtual() = 0;
    };
 
    class MyClass : public MyInterface {
      // ´íÎó£ºÎ´ÊµÏÖ´¿Ð麯Êý
    };
 
    // ÐÞ¸´£ºÊµÏÖ´¿Ð麯Êý
    void MyClass::pureVirtual() { /* ÊµÏÖ */
    }
 
    // 12.δÕýȷʵÏÖ¿½±´¹¹Ô캯Êý
    class MyClass1 {
     public:
      QWidget *widget;
      MyClass1(const MyClass1 &other) {
        widget = other.widget;
      }  // ´íÎó£ºÇ³¿½±´
    };
 
    // ÐÞ¸´£ºÉ±´»ò½ûÓÿ½±´¹¹Ô캯Êý
    MyClass1(const MyClass1 &other) = delete;
  }