#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;
|
}
|