zxin
2025-05-14 d5fad640fb749ab6626cbf68ba2b7cdc1fd058f7
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
#include "errorexample.h"
 
#include <QLabel>
errorexample::errorexample() {
  // 1.δ°üº¬±ØÒªµÄÍ·Îļþ
  // ´íÎó£ºÈ±ÉÙ #include <QLabel>
  QLabel label("Hello");  // ±àÒë´íÎó£º'QLabel' was not declared in this scope
  label.show();
 
  // 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;
}