在粤嵌arm板滑动切换图片程序
时间: 2024-03-25 10:35:32 浏览: 77
嵌入式系统/ARM技术中的嵌入式图片滑动的3D桌面设计方案
以下是一个基于Qt的在粤嵌ARM板上实现图片滑动切换的程序示例:
```cpp
#include <QGuiApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPixmap>
#include <QMouseEvent>
class ImageView : public QGraphicsView
{
public:
ImageView(QWidget *parent = nullptr)
: QGraphicsView(parent)
{
// 设置场景和视图大小
m_scene = new QGraphicsScene(this);
m_scene->setSceneRect(0, 0, 800, 480);
setScene(m_scene);
setFixedSize(800, 480);
// 加载图片
m_images.append(QPixmap("/opt/images/image1.jpg"));
m_images.append(QPixmap("/opt/images/image2.jpg"));
m_images.append(QPixmap("/opt/images/image3.jpg"));
m_images.append(QPixmap("/opt/images/image4.jpg"));
// 显示第一张图片
m_imageIndex = 0;
m_scene->addPixmap(m_images.at(m_imageIndex));
}
protected:
void mousePressEvent(QMouseEvent *event) override
{
// 记录下鼠标按下的位置
m_lastPos = event->pos();
}
void mouseMoveEvent(QMouseEvent *event) override
{
// 计算出偏移量
QPoint offset = event->pos() - m_lastPos;
// 移动图片
QRectF rect = sceneRect();
rect.translate(-offset.x(), -offset.y());
setSceneRect(rect);
m_lastPos = event->pos();
}
void mouseReleaseEvent(QMouseEvent *event) override
{
// 判断是否切换到下一张或上一张图片
int diff = event->pos().x() - m_lastPos.x();
if (diff > 50 && m_imageIndex < m_images.size() - 1) {
// 切换到下一张图片
m_imageIndex++;
m_scene->clear();
m_scene->addPixmap(m_images.at(m_imageIndex));
setSceneRect(m_scene->itemsBoundingRect());
} else if (diff < -50 && m_imageIndex > 0) {
// 切换到上一张图片
m_imageIndex--;
m_scene->clear();
m_scene->addPixmap(m_images.at(m_imageIndex));
setSceneRect(m_scene->itemsBoundingRect());
}
}
private:
QGraphicsScene *m_scene;
QList<QPixmap> m_images;
int m_imageIndex;
QPoint m_lastPos;
};
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
ImageView view;
view.show();
return app.exec();
}
```
在这个程序中,与前面的程序相比,唯一的区别是图片的路径需要修改为在粤嵌ARM板上的实际路径。这里我们将图片放在了`/opt/images`目录下,可以根据实际情况进行修改。
阅读全文