staic代码行只执行一次吗?
“static QSize szTarget = this->size()”之所以前面加个修饰static,因为size()只需要取值一次。
调试时,代码第二次还是执行了这一行。这是为何?
void prePaintRect(){ if (NULL == m_pPixmapSource) { return; } m_rcPixmapSource = m_pPixmapSource->rect(); int sourceW = m_rcPixmapSource.width(); int sourceH = m_rcPixmapSource.height(); static QSize szTarget = this->size(); int targetW = szTarget.width(); int targetH = szTarget.height(); int preferL = 0; int preferT = 0; int preferW = targetW; int preferH = targetH; //尺寸最佳调整 if ((sourceW<=targetW) && (sourceH<=targetH)) { //图片原始大小小于显示区域大小,1:1放置 //水平垂直居中显示 preferL = (targetW - sourceW) / 2; preferT = (targetH - sourceH) / 2; preferW = sourceW; preferH = sourceH; } else { //图片原始大小大于显示区域大小,需缩放 //缩放宽度 int newW = sourceW * targetH / sourceH; //缩放高度 int newH = sourceH * targetW / sourceW; if (newW < targetW) { //宽度需要缩放 preferL = (targetW - newW) / 2; preferT = 0; preferW = newW; preferH = targetH; } else { //高度需要缩放 preferL = 0; preferT = (targetH - newH) / 2; preferW = targetW; preferH = newH; } } m_rcPixmapTarget = QRect(preferL, preferT, preferW, preferH); int redlineW = 0; int redlineH = 0; if ((preferW<CAPTURE_WIDTH) && (preferH>CAPTURE_HEIGHT)) { redlineW = preferW; redlineH = CAPTURE_HEIGHT * preferW / CAPTURE_WIDTH; } else if ((preferW>CAPTURE_WIDTH) && (preferH<CAPTURE_HEIGHT)) { redlineW = CAPTURE_WIDTH * preferH / CAPTURE_HEIGHT; redlineH = preferH; } else if ((preferW<CAPTURE_WIDTH) && (preferH<CAPTURE_HEIGHT)) { redlineH = preferH * CAPTURE_HEIGHT / CAPTURE_WIDTH; redlineW = preferW * CAPTURE_WIDTH / CAPTURE_HEIGHT; if (redlineH > preferH) { redlineH = preferH; } if (redlineW > preferW) { redlineW = preferW; } } else { redlineW = CAPTURE_WIDTH; redlineH = CAPTURE_HEIGHT; } m_rectOfRedLine = QRect(preferL, preferT, redlineW, redlineH); calculateMiniRect();}