从下拉框中选择其他项目时更改选项卡小部件的 currentIndex()
Changing the currentIndex() of a tab widget when a different item is selected from a drop-down box
我是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()) );
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 最小硬币更换问题(自上而下方法)
- Qt VTK交互风格的信号到小部件
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- Win32编译器选项和内存分配
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- 将"-01"替换为"-02" 英特尔编译器选项会导致 FPE 在较小的 for 循环行程计数中抛出
- 无法将样式添加到QT Tab小部件中的选项卡(bar)和Tab(窗格)
- Qt-在选项卡小部件中显示表单
- Qt QTabWidget :选项卡太小,无法容纳其中的文本
- 从主窗口的菜单向 qt 选项卡小部件发送文件名
- C++ 创建小部件数组
- 选项卡化所有停靠窗口小部件
- 修复在QT中的布局中插入小部件后的选项卡顺序
- 如何用按钮同步选项卡小部件
- Gtk Notebook小部件添加新选项卡错误
- 实现Qt无框小部件的大小调整选项
- 我如何确定我可以传递给编译器选项的最小和最大值
- 从下拉框中选择其他项目时更改选项卡小部件的 currentIndex()
- 从另一个选项卡FLTK中开始的线程隐藏小部件