从下拉框中选择其他项目时更改选项卡小部件的 currentIndex()

Changing the currentIndex() of a tab widget when a different item is selected from a drop-down box

本文关键字:小部 选项 currentIndex 选择 项目 其他      更新时间:2023-10-16

我是C++新手,我刚刚开始将最初使用python/Qt的程序移植到C++/Qt以便利用我可以嵌入到程序中的更好的终端小部件。现在我有点卡住了,我正在尝试设置如果选择了下拉框中的不同项目,则选项卡小部件的currentIndex()会相应地更改。

这是我到目前为止的代码:

    //main.cpp
    #include <QApplication>
    #include "mainwindow.h"
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        return a.exec();
    }

这里是主窗口。

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QTimer>

    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        QTimer *timer;
        void startMyTimer()
        {
            timer = new QTimer();
            timer->setInterval(1);
            timer->start();
            QObject::connect(timer,SIGNAL(timeout()),this,SLOT(changeIndex()));
        }
    private:
        Ui::MainWindow *ui;
        void changeIndex();
         };
    #endif // MAINWINDOW_H

最后是主窗口.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        changeIndex();
    }
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    void MainWindow::changeIndex()
    {
        if (ui->comboBox->currentText() == "add-apt-repository")
        {
            ui->stackedWidget->setCurrentIndex(0);
            ui->checkBox->setCheckState(Qt::Checked);
        }
        if (ui->comboBox->currentText() == "apt-get")
        {
            ui->stackedWidget->setCurrentIndex(1);
            ui->checkBox->setCheckState(Qt::Checked);
        }
        if (ui->comboBox->currentText() == "aptitude")
        {
           ui->stackedWidget->setCurrentIndex(2);
           ui->checkBox->setCheckState(Qt::Checked);
        }
        if (ui->comboBox->currentText() == "bzr")
        {
            ui->stackedWidget->setCurrentIndex(3);
            ui->checkBox->setCheckState(Qt::Unchecked);
        }
        if (ui->comboBox->currentText() == "cd")
        {
            ui->stackedWidget->setCurrentIndex(4);
            ui->checkBox->setCheckState(Qt::Unchecked);
        }
        if (ui->comboBox->currentText() == "chmod")
        {
            ui->stackedWidget->setCurrentIndex(5);
            ui->checkBox->setCheckState(Qt::Checked);
        }
    }

我看过一堆QTimer的例子,但我不知所措。我也尝试过做if (ui->comboBox->changeEvent())但我可能也用错了。

首先,您可能必须changeIndex()标记为插槽,如下所示:

class MainWindow : public QMainWindow
{
    Q_OBJECT
    // ...
    private slots: 
        void changeIndex();
    private:
        Ui::MainWindow *ui;
}

这也需要你调用Qt元对象编译器。如果你使用 qmake,那已经为你完成了。否则,这取决于您的构建系统。

其次,使用计时器有什么特别的原因吗?您还可以连接到组合框的currentIndexChanged信号之一。

放下计时器,这里没有用。相反,通过将 changeIndex() 放入"私有插槽:"部分来使其成为插槽:

public slots:
    void changeIndex();

然后将组合框的 currentIndexChanged 信号连接到您的插槽,在 MainWindow 构造函数中:

connect( ui->combobox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeIndex()) );