QT开发中文网

使用C++qt 在QWidget窗口中存放了一个ui_QTableWidget显示几个表数据,怎么使用QList存储tab的索引,实现ui_QTableWidget的历史记录功能,最多存20页,并通过按下“前进”和“后退”按钮来实现界面的多次前进和多次返回功能,通过切换tab的信号来响应tabindex切换的槽函数

  1. 声明QList存储tab的索引

在窗口类中声明一个QList来存储历史记录,例如:

class MyWindow : public QWidget {
    Q_OBJECT
public:
    explicit MyWindow(QWidget *parent = nullptr);

private:
    QList<int> m_history;
};
  1. 添加历史记录

在切换tab时,将当前tab的索引加入历史记录中。为了限制历史记录最多只能存储20页,可以在添加前先判断历史记录是否已经达到最大值,如果是,则删除最早的一项。

void MyWindow::onTabChanged(int index) {
    // 添加历史记录
    m_history.append(index);
    if (m_history.size() > 20) {
        m_history.removeFirst();
    }
}
  1. 响应“前进”和“后退”按钮

将“前进”和“后退”按钮的clicked信号与对应的槽函数连接,用当前tab索引更新历史记录中的索引,然后再根据所选方向(前进或后退)获取下一个或上一个索引,并将其设置为当前tab的索引。

void MyWindow::onBackwardClicked() {
    if (!m_history.isEmpty()) {
        int currentIndex = ui->tableWidget->currentIndex().column();
        m_history.append(currentIndex);
        currentIndex = m_history.takeLast();
        ui->tableWidget->setCurrentCell(0, currentIndex);
    }
}

void MyWindow::onForwardClicked() {
    if (!m_history.isEmpty()) {
        int currentIndex = ui->tableWidget->currentIndex().column();
        m_history.append(currentIndex);
        currentIndex = m_history.takeFirst();
        ui->tableWidget->setCurrentCell(0, currentIndex);
    }
}
  1. 响应tab切换

将ui_QTableWidget的currentChanged信号连接到一个槽函数,用于更新历史记录。

connect(ui->tableWidget, SIGNAL(currentChanged(int,int,int,int)), this, SLOT(onTabChanged(int)));
  1. 完整示例代码
class MyWindow : public QWidget {
    Q_OBJECT
public:
    explicit MyWindow(QWidget *parent = nullptr);

private slots:
    void onBackwardClicked();
    void onForwardClicked();
    void onTabChanged(int index);

private:
    QList<int> m_history;
};

MyWindow::MyWindow(QWidget *parent)
    : QWidget(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);

    // 创建ui_QTableWidget并添加表格数据
    QTableWidget *tableWidget = new QTableWidget(this);
    tableWidget->setRowCount(3);
    tableWidget->setColumnCount(3);
    tableWidget->setItem(0, 0, new QTableWidgetItem("1"));
    tableWidget->setItem(0, 1, new QTableWidgetItem("2"));
    tableWidget->setItem(0, 2, new QTableWidgetItem("3"));
    tableWidget->setItem(1, 0, new QTableWidgetItem("4"));
    tableWidget->setItem(1, 1, new QTableWidgetItem("5"));
    tableWidget->setItem(1, 2, new QTableWidgetItem("6"));
    tableWidget->setItem(2, 0, new QTableWidgetItem("7"));
    tableWidget->setItem(2, 1, new QTableWidgetItem("8"));
    tableWidget->setItem(2, 2, new QTableWidgetItem("9"));

    // 将ui_QTableWidget添加到窗口布局中
    layout->addWidget(tableWidget);

    // 添加“前进”和“后退”按钮,并连接到槽函数
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    QPushButton *backwardButton = new QPushButton("后退", this);
    QPushButton *forwardButton = new QPushButton("前进", this);
    buttonLayout->addWidget(backwardButton);
    buttonLayout->addWidget(forwardButton);
    layout->addLayout(buttonLayout);
    connect(backwardButton, SIGNAL(clicked()), this, SLOT(onBackwardClicked()));
    connect(forwardButton, SIGNAL(clicked()), this, SLOT(onForwardClicked()));

    // 将ui_QTableWidget的currentChanged信号连接到onTabChanged槽函数,用于更新历史记录
    connect(tableWidget, SIGNAL(currentChanged(int,int,int,int)), this, SLOT(onTabChanged(int)));
}

void MyWindow::onBackwardClicked() {
    if (!m_history.isEmpty()) {
        int currentIndex = ui->tableWidget->currentIndex().column();
        m_history.append(currentIndex);
        currentIndex = m_history.takeLast();
        ui->tableWidget->setCurrentCell(0, currentIndex);
    }
}

void MyWindow::onForwardClicked() {
    if (!m_history.isEmpty()) {
        int currentIndex = ui->tableWidget->currentIndex().column();
        m_history.append(currentIndex);
        currentIndex = m_history.takeFirst();
        ui->tableWidget->setCurrentCell(0, currentIndex);
    }
}

void MyWindow::onTabChanged(int index) {
    // 添加历史记录
    m_history.append(index);
    if (m_history.size() > 20) {
        m_history.removeFirst();
    }
}

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?