这个例子是加载
图片后,在
QLabel控件中逐渐
显示出完整图片的程序。
我的疑惑是为什么在Window的loadImage和addBlock方法中都要有label->setPixmap?我在loadImage时已经将图片加载到label控件中,为什么还要在addBlock中再次加载???
- 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);
- }
17行。
- 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);
- }
14行。