QT开发中文网

给一个qt c++ tab 页面切换 点击tab标签 tab页内容从右向左滑动 tab页标签处有一个checkbox 用于勾选 的示例代码

以下是一个实现Qt C++中tab页面切换的示例代码,其中使用了QStackedWidget和QCheckBox控件。

首先,在.ui文件中添加一个QTabWidget和一个QStackedWidget。然后,在QTabWidget中添加多个标签页,并将每个标签页与QStackedWidget中的相应页面关联起来。

接下来,在mainwindow.cpp文件中,我们可以使用QCheckBox的clicked()信号来实现勾选框的点击事件。在该事件中,我们可以使用setCurrentIndex()函数来切换页面,并使用QPropertyAnimation类来实现内容从右向左滑动的动画效果。

以下是完整的示例代码:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStackedWidget>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_checkBox_clicked();

private:
    Ui::MainWindow *ui;
    QStackedWidget *stackedWidget;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QCheckBox>
#include <QPropertyAnimation>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    stackedWidget = new QStackedWidget(this);
    stackedWidget->addWidget(ui->page1);
    stackedWidget->addWidget(ui->page2);
    stackedWidget->addWidget(ui->page3);
    stackedWidget->addWidget(ui->page4);
    stackedWidget->addWidget(ui->page5);
    stackedWidget->setCurrentIndex(0);
    ui->verticalLayout->addWidget(stackedWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_checkBox_clicked()
{
    QCheckBox *checkBox = qobject_cast<QCheckBox*>(sender());
    if (checkBox == nullptr) {
        return;
    }

    int index = checkBox->property("index").toInt();
    if (checkBox->isChecked()) {
        QPropertyAnimation *animation = new QPropertyAnimation(stackedWidget, "geometry");
        animation->setDuration(300);
        animation->setStartValue(stackedWidget->geometry());
        QRect endRect = stackedWidget->geometry();
        endRect.setLeft(endRect.left() - endRect.width());
        animation->setEndValue(endRect);
        animation->start();
        stackedWidget->setCurrentIndex(index);
        QPropertyAnimation *animation2 = new QPropertyAnimation(stackedWidget, "geometry");
        animation2->setDuration(300);
        QRect startRect = stackedWidget->geometry();
        startRect.setLeft(startRect.left() + startRect.width());
        animation2->setStartValue(startRect);
        animation2->setEndValue(stackedWidget->geometry());
        animation2->start();
    }
}

其中,on_checkBox_clicked()函数是QCheckBox的clicked()信号的槽函数。在该函数中,我们首先获取点击的勾选框所对应的页面索引,然后根据勾选状态来切换页面,并使用QPropertyAnimation类来实现动画效果。

在.ui文件中,需要为每个tab页面添加一个QCheckBox控件,并设置其属性“index”为该页面在QStackedWidget中的索引。例如,在第一个tab页面中添加的QCheckBox的属性设置如下:

objectName: checkBox
geometry: QRect(70, 20, 101, 21)
text: "Show Page 2"
setProperty: index, 1

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=77

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?