Java 基礎一 【註釋】 comment 對代碼進行解釋說明1.Java規範的註釋有3種單行註釋://多行註釋:/* */文檔註釋(java特有)2.單行註釋和多行註釋的作用:對所寫的程式進行解釋說明,增強可讀性。方便自己,方便別人。可以調試所寫的代碼3.特點單行註釋和多行註釋,註釋了的內容不參與 ...
我做的儀錶盤控制項是個功能簡單的顯示控制項。該控制項在VS2015和Qt5.9上測試可用。它預設的顯示範圍是[0, 100],不能修改它的顯示範圍,因為不同的數值顯示在前界面上的效果不同,可能導致顯示重疊。成員函數只有一個setValue方法用來設置當前顯示值。
代碼如下,頭文件:
class MDashboard : public QWidget { Q_OBJECT public: MDashboard(QWidget* parent = nullptr); void setValue(qreal ivalue); private: void paintEvent(QPaintEvent *event) override; private: const static int radius; const static int maxv; const static int minv; qreal value; };
CPP文件:
const int MDashboard::radius = 80; const int MDashboard::minv = 0; const int MDashboard::maxv = 100; MDashboard::MDashboard(QWidget* parent) : QWidget(parent) { value = 0; } void MDashboard::setValue(qreal ivalue) { value = ivalue; update(); } void MDashboard::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QPoint center(width() / 2, height() / 2); painter.save(); /* 繪製背景圓形 */ painter.translate(center); painter.setPen(Qt::NoPen); painter.setBrush(QColor(143, 143, 143)); const int bkRadius = radius + 3; painter.drawEllipse(QPoint(), bkRadius, bkRadius); /* 繪製長短刻度線 */ painter.rotate(-135); const int longStep = 10; const int shortStep = 50; for (int i = 0; i <= shortStep; i++) { /* 前80%綠色刻度線,後20%紅色 */ painter.setPen((i < shortStep * 0.8) ? QColor(32, 243, 32) : QColor(243, 32, 32)); if (i % (shortStep / longStep) != 0) { QPoint p1(0, -(radius - 8)); QPoint p2(0, -radius); painter.drawLine(p1, p2); } else { QPoint pl(0, -(radius - 12)); QPoint p2(0, -radius); painter.drawLine(pl, p2); } painter.rotate(270.0 / shortStep); } /* 繪製表盤名字和它的背景矩形 */ painter.restore(); QFontMetrics fm = painter.fontMetrics(); int tx = center.x(); int ty = int(center.y() + (radius / 1.4142)); QString title = u8"儀錶盤"; QSize tsz = fm.size(0, title); QRect trect(QPoint(tx - tsz.width() / 2, ty - tsz.height() / 2), tsz); painter.setPen(QColor(127, 127, 127)); painter.setBrush(QColor(173, 163, 163)); painter.drawRoundedRect(trect.adjusted(-6, -4, 6, 4), 3, 3); painter.setPen(QColor(243, 243, 243)); painter.setBrush(Qt::NoBrush); painter.drawText(trect, title);/* 繪製每個長刻度線對應的文字 */ painter.setPen(QColor(243, 243, 243)); const int hand = radius - 21; for (int i = 0; i <= longStep; i++) { qreal angle = qDegreesToRadians(-135.0 + i * (270.0 / longStep)); QString etext = QString::number(minv + i * (maxv - minv) / longStep); /* 註意這裡整形運算 */ QPointF dirVec(qSin(angle), -qCos(angle)); QPointF ecenter(center.x() + hand * dirVec.x(), center.y() + hand * dirVec.y()); QSize esz = fm.size(0, etext); QRectF erect(QPointF(ecenter.x() - esz.width() / 2, ecenter.y() - esz.height() / 2), esz); painter.drawText(erect, etext); } /* 繪製表盤指針 */ painter.save(); QPoint triangle[] = /* 向上的三角形 */ { { -5, 0 }, { 0, 25 - radius }, { 5, 0 }, }; qreal degree = -135.0 + 270.0 * (value - minv) / (maxv - minv); painter.translate(center); painter.rotate(degree); painter.setPen(Qt::NoPen); painter.setBrush(Qt::red); painter.drawPolygon(triangle, 3); painter.setPen(QPen(QColor(213, 0, 0), 2)); painter.drawEllipse(QPoint(), 7, 7); painter.restore(); }