qt 实现画廊展示

    在完成网易云部分,要用到画廊动画,感谢公孙二狗的指导,给的简单例子,自己改写代码,完成下面效果。

    其实想法很简单,就是我们使用两个Qwidget,一个是显示,一个是存放加载的资源,然后,在通过QPropertyAnimation,移动到对应的位置,就可以达到这样的基本效果。

点击下载:    代码下载连接

GalleryItem的布局

Widget布局

 

代码部分:

主要部分:

#ifndef GALLERYPRIVATE_H
#define GALLERYPRIVATE_H

#include"galleryitem.h"
#include <QList>
#include <QWidget>
#include <QPropertyAnimation>
/***********************************************************************************************************************
 *                                                    GalleryPrivate                                                   *
 **********************************************************************************************************************/
class GalleryPrivate
{
public:
    GalleryPrivate();
    //输入item对象的宽与长 以及要放入的对象
    void setItemRes(int itemWidth, int itemHeight, const QList<GalleryItem*>& itemData );
    //上一个的索引
    int getPreviousItemIndex();
    //下一个的索引
    int getNextItemIndex();
    //是否有下一个
    bool hasNextItem();
    //是否有上一个
    bool hasPreviousItem();
    //显示对应位置
    void showImage(int index);
    //获取view 的指针
    QWidget * getItemViewport() const;
private:
    //对象的宽
    int itemWidth;
    //对象的高
    int itemHeght;
    //对象的个数
    int itemCount;
    //当前的对象的索引
    int currentItem;
    // 动画的播放时间,单位为毫秒
    int animationDuraton;
    //能够看到对象的区域
    QWidget * itemViewport;
    //对象的全部区域
    QWidget * itemContainer;

};

#endif // GALLERYPRIVATE_H

 cpp

#include "galleryprivate.h"

const int Padding = 15;

GalleryPrivate::GalleryPrivate()
{
    itemWidth = 0;
    itemHeght = 0;
    itemCount = 0;
    currentItem = 0;
    animationDuraton = 200; //200毫秒
}

void GalleryPrivate::setItemRes(int itemWidth, int itemHeight, const QList<GalleryItem *> &itemData)
{
    currentItem = 0;
    this->itemWidth = itemWidth;
    this->itemHeght = itemHeight;
    int nSize = itemData.size();
    itemCount = nSize;
    //viewport 的固定大小 显示5个item 与中间4个间隔 每个固定15
    itemViewport = new QWidget();
    itemViewport->setFixedSize(this->itemWidth*5+Padding*4,this->itemHeght);

    // itemContainer 的大小为所有item排在一起的大小
    itemContainer = new QWidget(itemViewport);
    itemContainer->resize(nSize*this->itemWidth + (nSize-1) * Padding,this->itemHeght);
    itemContainer->move(0,0);

    //让对象添加到 对应的位置。
    for (int index = 0; index< itemCount; index++) {
        itemData[index]->setParent(itemContainer);
        {
           itemData[index]->move(index*this->itemWidth+(index) *Padding,0);
        }
    }

    itemViewport->setStyleSheet("QWidget#itemViewport{border:1px; background:white; }");
    itemContainer->setStyleSheet("QWidget#itemContainer{border:1px; background:white; }");

}

int GalleryPrivate::getPreviousItemIndex()
{
    if(hasPreviousItem())
    {
        currentItem--;
    }

    return  currentItem;
}

int GalleryPrivate::getNextItemIndex()
{
    if(hasNextItem())
    {
        currentItem++;
    }
    return currentItem;
}

bool GalleryPrivate::hasNextItem()
{
    return  currentItem < itemCount - 5;
}

bool GalleryPrivate::hasPreviousItem()
{
    return  currentItem > 0;
}

void GalleryPrivate::showImage(int index)
{
    if(index>=0 && index<itemCount - 4)
    {
        QPropertyAnimation *animation = new QPropertyAnimation(itemContainer,"pos");
        animation->setDuration(animationDuraton);
        if(index == 0)
        {
            animation->setEndValue(QPoint(0,0));
        }
        else {
            animation->setEndValue(QPoint(-(index*this->itemWidth+(index)*Padding),0));
        }
        animation->start(QAbstractAnimation::DeleteWhenStopped);
    }
}

QWidget *GalleryPrivate::getItemViewport() const
{
    return itemViewport;
}

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实时展示HTML配置,可以使用Qt中的QWebView件。QWebView控件是一个基于WebKit的浏览器控件,可以加载和显示网页。以下是一个简单的示例: ```cpp #include <QtWidgets/QApplication> #include <QtWebKitWidgets/QWebView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWebView* webView = new QWebView(); webView->show(); QString html = "<html><body><h1>Hello World!</h1></body></html>"; webView->setHtml(html); return a.exec(); } ``` 在这个示例中,我们创建了一个QWebView控件,并将其展示出来。然后,我们定义了一个简单的HTML字符串,并使用setHtml()方法将其加载到QWebView控件中。这样,当应用程序运行时,它将显示一个包含“Hello World!”标题的网页。 如果你想实现实时展示HTML配置,可以将setHtml()方法放在一个定时器中,定时器每隔一段时间就会更新HTML内容。例如: ```cpp #include <QtWidgets/QApplication> #include <QtWebKitWidgets/QWebView> #include <QtCore/QTimer> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWebView* webView = new QWebView(); webView->show(); QTimer* timer = new QTimer(); QObject::connect(timer, &QTimer::timeout, [=]() { QString html = "<html><body><h1>Hello World!</h1></body></html>"; webView->setHtml(html); }); timer->start(1000); // 定时器每隔1秒更新一次HTML内容 return a.exec(); } ``` 在这个示例中,我们使用QTimer定时器来更新HTML内容。每当定时器超时时,我们重新定义HTML字符串并将其加载到QWebView控件中。这样,QWebView控件将实时更新并显示新的HTML内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值