【问题标题】:Changing the currentIndex() of a tab widget when a different item is selected from a drop-down box从下拉框中选择不同项目时更改选项卡小部件的 currentIndex()
【发布时间】:2013-01-13 09:17:24
【问题描述】:

我是 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();
    }

这里是 mainwindow.h

    #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

最后是 mainwindow.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-&gt;comboBox-&gt;changeEvent()),但我可能也用错了。

【问题讨论】:

    标签: c++ python qt porting


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 不,没有真正使用计时器,老实说我只是在尝试。一开始我不是一个程序员,更不用说c++了。在 python 中,我设置它的方式我不必担心事件和信号,所以到目前为止我只是在这一部分上遇到了麻烦。
    • 非常感谢,这正是我想要的。
    【解决方案2】:

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

    public slots:
        void changeIndex();
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多