在arm板滑动切换图片程序
时间: 2024-03-25 17:35:31 浏览: 34
以下是一个基于Qt的在ARM板上实现图片滑动切换的程序示例:
```cpp
#include <QApplication>
#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(":/images/image1.jpg"));
m_images.append(QPixmap(":/images/image2.jpg"));
m_images.append(QPixmap(":/images/image3.jpg"));
m_images.append(QPixmap(":/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[])
{
QApplication app(argc, argv);
ImageView view;
view.show();
return app.exec();
}
```
在这个程序中,我们继承了QGraphicsView类,重载了鼠标事件函数,并在构造函数中加载了多张图片。在鼠标移动过程中,我们根据偏移量移动图片,并在鼠标释放时判断是否需要切换到下一张或上一张图片。注:这里的图片路径需要根据实际情况进行修改。
阅读全文