#include "errorexample.h" #include #include #include errorexample::errorexample() { // 1.δ°üº¬±ØÒªµÄÍ·Îļþ // ´íÎó£ºÈ±ÉÙ #include QLabel label("Hello"); // ±àÒë´íÎó£º'QLabel' was not declared in this scope label.show(); //-----------Ö¸Õë³£¼ûÎÊÌâ--------------- //¢Ù¿ÕÖ¸Õë½âÒýÓà QWidget *widget = nullptr; widget->show(); //¶ÔֵΪnullptrµÄÖ¸Õë½øÐзÃÎʲÙ×÷,»áÒý·¢±ÀÀ£ //½â¾ö°ì·¨£º QWidget *widget1 = nullptr; if (widget1) { //ʹÓÃÖ¸Õëǰ£¬ÏÈÈ·ÈÏÆäÊÇ·ñΪnullptr 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 button3(new QPushButton(parent)); //½â¾ö°ì·¨1£º½öÓø¸¶ÔÏó¹ÜÀí QWidget *parent4 = new QWidget; QPushButton *button4 = new QPushButton(parent4); // ¸¸¶ÔÏó¸ºÔðÊÍ·Å //½â¾ö°ì·¨2£ºÎÞ¸¸¶ÔÏóʱʹÓÃÖÇÄÜÖ¸Õë std::unique_ptr button5 = std::make_unique(); //¢ÞÈÝÆ÷ÖеÄÖ¸Õë¹ÜÀí // Íü¼ÇÊÍ·Å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 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 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; }