当前位置: 代码迷 >> QT开发 >> Qt5.3自带例证queuedcustomtype的一点疑问
  详细解决方案

Qt5.3自带例证queuedcustomtype的一点疑问

热度:153   发布时间:2016-04-25 03:06:22.0
Qt5.3自带例子queuedcustomtype的一点疑问
本帖最后由 weiweiqiao 于 2014-09-12 09:18:01 编辑
这个例子是加载图片后,在QLabel控件中逐渐显示出完整图片的程序。
我的疑惑是为什么在Window的loadImage和addBlock方法中都要有label->setPixmap?我在loadImage时已经将图片加载到label控件中,为什么还要在addBlock中再次加载???
codeA:
    void Window::loadImage(const QImage &image)
    {
        QDesktopWidget desktop;
        QImage useImage;
        QRect space = desktop.availableGeometry();
        if ((image.width() > 0.75 * space.width())
                || (image.height() > 0.75*space.height())) {
            useImage = image.scaled(0.75*space.width(), 0.75*space.height(),
                                    Qt::KeepAspectRatio, Qt::SmoothTransformation);
        } else {
            useImage = image;
        }
        pixmap = QPixmap(useImage.width(), useImage.height());
        pixmap.fill(qRgb(255, 255, 255));
        label->setPixmap(pixmap);
        loadButton->setEnabled(false);
        resetButton->setEnabled(true);
        thread->processImage(useImage);
    }

第15行。
codeB:
    void Window::addBlock(const Block& block)
    {
        //get color from block and set the alpha parameter of color.
        QColor color = block.color();
        color.setAlpha(64);
        QPainter painter;
        painter.begin(&pixmap);
        painter.fillRect(block.rect(), color);
        painter.end();
        const QPixmap* p1 = label->pixmap();
        QPixmap* p2 = &pixmap;
        //add pixmap to label widget and to show.
        label->setPixmap(pixmap);
    }

第13行。

而且codeA的代码//后,程序也可正常运行,但是codeB的//后,label什么都不会显示。我就疑问,如果codeA中已经将pixmap添加到label中,为什么codeB中必须要手动将pixmap添加到label中?
------解决思路----------------------
setpixmap添加的图片不一定会一直被label持有,可能会被其他设置清除,比如文本或动画,
  相关解决方案