QT 寫一個手機頁面滑動的範例?

在 QT 中,你可以使用 QListView 或 QTableView 控制項來實現手機頁面滑動的效果。


以下是使用 QListView 實現手機頁面滑動的範例:

#include <QApplication>
#include <QListView>
#include <QStandardItemModel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 創建模型
    QStandardItemModel model;
    for (int i = 1; i <= 20; i++) {
        model.appendRow(new QStandardItem(QString("Item %1").arg(i)));
    }

    // 創建 QListView
    QListView view;
    view.setModel(&model);
    view.setFlow(QListView::LeftToRight); // 設定瀑布流的方向
    view.setWrapping(true); // 允許滑動到最後一頁時循環顯示
    view.setViewMode(QListView::IconMode); // 設定圖示模式
    view.setMovement(QListView::Static); // 設定不允許拖動改變順序
    view.setResizeMode(QListView::Adjust); // 設定大小自適應
    view.setSpacing(10); // 設定圖示間的間隔
    view.setUniformItemSizes(true); // 設定所有圖示大小相同
    view.setSelectionMode(QAbstractItemView::NoSelection); // 設定不允許選擇
    view.setGridSize(QSize(200, 200)); // 設定圖示的大小
    view.show();

    return a.exec();
}

上面的程式碼會創建一個包含 20 個項目的手機頁面滑動視圖。你可以通過滑動視圖來查看不同的頁面。


如果你想使用 QTableView 實現手機頁面滑動,可以參考以下程式碼:

#include <QApplication>
#include <QTableView>
#include <QStandardItemModel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // 創建模型
    QStandardItemModel model(4, 4); // 4 行 4 列
    for (int row = 0; row < 4; row++) {
        for (int column = 0; column < 4; column++) {
            QStandardItem *item = new QStandardItem(QString("Item %1").arg((row * 4) + column + 1));
            model.setItem(row, column, item);
        }
    }

    // 創建 QTableView
    QTableView view;
    view.setModel(&model);
    view.horizontalHeader()->setVisible(false); // 隱藏水平表頭
    view.verticalHeader()->setVisible(false); // 隱藏垂直表頭
    view.setShowGrid(false); // 隱藏網格
    view.setSelectionMode(QAbstractItemView::NoSelection); // 設定不允許選擇
    view.setVerticalScrollMode(QAbstractItemView::ScrollPerItem); // 設定垂直滾動模式
    view.setHorizontalScrollMode(QAbstractItemView::ScrollPerItem); // 設定水平滾動模式
    view.resizeColumnsToContents(); // 調整列的寬度
    view.resizeRowsToContents(); // 調整行的高度
    view.show();

    return a.exec();
}

上面的程式碼會創建一個包含 4 行 4 列的手機頁面滑動視圖。你可以通過滑動視圖來查看不同的頁面。

希望這些範例能幫助你了解如何使用 QT 實現手機頁面滑動。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
qt 屏翻页效果C++程序 #ifndef PICTUREFLOW_H #define PICTUREFLOW_H #include class PictureFlowPrivate; /*! Class PictureFlow implements an image show widget with animation effect like Apple's CoverFlow (in iTunes and iPod). Images are arranged in form of slides, one main slide is shown at the center with few slides on the left and right sides of the center slide. When the next or previous slide is brought to the front, the whole slides flow to the right or the right with smooth animation effect; until the new slide is finally placed at the center. */ class PictureFlow : public QWidget { Q_OBJECT Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor) Q_PROPERTY(QSize slideSize READ slideSize WRITE setSlideSize) Q_PROPERTY(int slideCount READ slideCount) Q_PROPERTY(int centerIndex READ centerIndex WRITE setCenterIndex) public: enum ReflectionEffect { NoReflection, PlainReflection, BlurredReflection }; /*! Creates a new PictureFlow widget. */ PictureFlow(QWidget* parent = 0); /*! Destroys the widget. */ ~PictureFlow(); /*! Returns the background color. */ QColor backgroundColor() const; /*! Sets the background color. By default it is black. */ void setBackgroundColor(const QColor& c); /*! Returns the dimension of each slide (in pixels). */ QSize slideSize() const; /*! Sets the dimension of each slide (in pixels). */ void setSlideSize(QSize size); /*! Returns the total number of slides. */ int slideCount() const; /*! Returns QImage of specified slide. */ QImage slide(int index) const; /*! Returns the index of slide currently shown in the middle of the viewport. */ int centerIndex() const; /*! Returns the effect applied to the reflection. */ ReflectionEffect reflectionEffect() const; /*! Sets the effect applied to the reflection. The default is PlainReflection. */ void setReflectionEffect(ReflectionEffect effect); public slots: /*! Adds a new slide. */ void addSlide(const QImage& image); /*! Adds a new slide. */ void addSlide(const QPixmap& pixmap); /*! Sets an image for specified slide. If the slide already exists, it will be replaced. */ void setSlide(int index, const QImage& image); /*! Sets a pixmap for specified slide. If the slide already exists, it will be replaced. */ void setSlide(int index, const QPixmap& pixmap); /*! Sets slide to be shown in the middle of the viewport. No animation effect will be produced, unlike using showSlide. */ void setCenterIndex(int index); /*! Clears all slides. */ void clear(); /*! Shows previous slide using animation effect. */ void showPrevious(); /*! Shows next slide using animation effect. */ void showNext(); /*! Go to specified slide using animation effect. */ void showSlide(int index); /*! Rerender the widget. Normally this function will be automatically invoked whenever necessary, e.g. during the transition animation. */ void render(); /*! Schedules a rendering update. Unlike render(), this function does not cause immediate rendering. */ void triggerRender(); signals: void centerIndexChanged(int index); protected: void paintEvent(QPaintEvent *event); void keyPressEvent(QKeyEvent* event); void mousePressEvent(QMouseEvent* event); void resizeEvent(QResizeEvent* event); private slots: void updateAnimation(); private: PictureFlowPrivate* d; }; #endif // PICTUREFLOW_H

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值