Tre*_*ith 2 graphics user-interface qt 2d
我有一个QGraphicsScene,我在场景中添加了几个QGraphicsItem.添加到场景中的所有项目都是QGraphicsPixmapItem.
我希望输出显示的场景对添加到场景中的每个项目具有"镜像"视觉效果.我希望"镜像"视觉效果能够在显示相册时对iTunes镜像产生影响:
来自http://www.steelskies.com/site/images/iTunesBrowserThumb.jpg的示例"镜像"效果
(注意:上面的图片来自" CoverFlow公司网站 ".我认为CoverFlow是实现iTunes相册显示"镜像"视觉效果的人.)
请注意此场景中的每个项目如何在其下方都有一个镜像.
如何为每个项目创建这种"镜像"视觉效果(如屏幕截图所示)?
我想我就是这样做的.根据评论,我想这对你也有好处.
我只需使用QPainter从原始图像创建反射的镜像图像并合并两者(再次使用QPainter).生成的图像是使用QGraphicsPixmapItem显示的图像.在下面的代码中,我创建了镜像的反射镜像版本.您需要做的就是调整参数并合并.
#include <QApplication>
#include <QLabel>
#include <QPixmap>
#include <QPainter>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Just to show the original image.
QLabel label;
QImage original(<place_an_image_path_here>);
label.setPixmap(QPixmap::fromImage(original));
label.show();
// Create the gradient that will be placed over the image.
QPoint start(0, 0);
QPoint end(0, original.height());
QLinearGradient gradient(start, end);
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(0.5, Qt::black);
// Create the mask to be used on the mirrored image.
QImage mask(original.size(), original.format());
QPainter painter(&mask);
// You may want to add additional opacity according
// to the sample image shown.
painter.setOpacity(0.8);
painter.fillRect(original.rect(), gradient);
painter.end();
// Create the mirrored reflection.
QImage reflection = original.mirrored();
reflection.setAlphaChannel(mask);
// Just to show the result.
QLabel labelReflection;
labelReflection.setPixmap(QPixmap::fromImage(reflection));
labelReflection.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
您可以在QGraphicsPixmapItem中加载生成的图像(两者合并的结果),然后您可以继续应用所需的所有转换.
编辑:我忘了你可能还想设置一个额外的不透明度,因为提供的图像似乎.我没试过,但也许可以使用QPixmaps做同样的事情.这应该可以提高性能甚至允许,取决于您使用的平台和油漆引擎,加速绘画.
EDIT2:根据要求,这是我的测试代码的输出:
(我希望这张图片不受某些版权或类似的影响,我试着检查但没有写入任何内容)