选择QComboBox后如何打开对话框 [英] How to open a dialog after QComboBox choice

查看:59
本文介绍了选择QComboBox后如何打开对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 QComboBox ,在 QToolBar 上有多个选择. QComboBox 的每种选择都会打开一个特定的对话框窗口.我遇到的问题是,在组合框上选择了首选索引后,没有对话框打开.为了简化示例,我将同一对话框链接到所有选项:

I have a QComboBox with several choices on a QToolBar. Every choice of the QComboBox will open a specific dialog window. The problem I have is that after I choose the preferred index on the combobox no dialogs opens up. For simplicity of the example I am linking the same dialog to all choices:

dredgewindow.h

这是头文件

namespace Ui {
class DredgeWindow;
}

class DredgeWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit DredgeWindow(QWidget *parent = nullptr);
    ~DredgeWindow();

private:
    Ui::DredgeWindow *ui;
    DredgeDB *mDredgeDB;
};

dredgewindow.cpp

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

    QComboBox* myComboBox = new QComboBox;
    ui->toolBarControls->addWidget(myComboBox);
    myComboBox->addItem("Please Select");
    myComboBox->addItem("Bucket");
    myComboBox->addItem("Scow");
    myComboBox->addItem("Hopper Dredger");

    switch(myComboBox->currentIndex()){
      case 0:
        // do nothing
        break;
      case 1:
        // Go to Bucket
        mDredgeDB = new DredgeDB();
        mDredgeDB->show();
        break;
      case 2:
        // Go to Scow...
        mDredgeDB = new DredgeDB();
        mDredgeDB->show();
        break;
    case 3:
        // Go to Hopper Dredger
        mDredgeDB = new DredgeDB();
        mDredgeDB->show();
      default:
        break;
    }
}

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

到目前为止,我一直在尝试使用组合框触发对话框的打开,但是,一旦我松开鼠标(因此,我 switch - case ),我希望对话框会打开,但是什么也不会发生.此来源是有用的,即使它不是在c ++中.但是我还是用它来理解一般方法.

So far I have been trying to trigger the opening of the dialogs using the combobox but as soon as I release the mouse (and therefore I switch - case) I expect the dialog to open but nothing happens. This source was useful even though it was not in c++. But still I used it to understand the general approach.

此方法触发组合框并将其设置为活动状态但除此之外,没有任何具体说明.

This approach triggers the combobox and set it active but other than that there is no specific indication.

预先感谢您指出解决此问题的正确方向.

Thanks in advance for pointing to the right direction for solving this problem.

推荐答案

您必须连接

You have to connect QComboBox::activated() signal to some slot.

信号&Qt5中的插槽
新信号槽语法
qOverload<>

当用户在组合框中选择一个项目时发送此信号.这项目的索引已传递.请注意,即使选择没有改变.如果您需要知道实际的选择时间更改,请使用信号currentIndexChanged().

This signal is sent when the user chooses an item in the combobox. The item's index is passed. Note that this signal is sent even when the choice is not changed. If you need to know when the choice actually changes, use signal currentIndexChanged().

注意:此类中的激活信号过载.连接到此通过使用函数指针语法发出信号,Qt提供了一个方便的方法获取功能指针的帮助器- qOverload<> .

Note: Signal activated is overloaded in this class. To connect to this signal by using the function pointer syntax, Qt provides a convenient helper for obtaining the function pointer - qOverload<>.

class DredgeWindow : public QMainWindow
{
    Q_OBJECT
    ...
private slots:
    void on_combo_index_activated(int index);
    ...
};

DredgeWindow::DredgeWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ...
    connect(myComboBox, QOverload<int>::of(&QComboBox::activated),
        [=](int index) { on_combo_index_activated(index); });
    ...
}

void DredgeWindow::on_combo_index_activated(int index)
{
    switch (index)
    {
    case 0:
        // do nothing
        break;

    case 1:
        // Go to Bucket
        mDredgeDB = new DredgeDB();
        mDredgeDB->show();
        break;

    case 2:
        // Go to Scow...
        mDredgeDB = new DredgeDB();
        mDredgeDB->show();
        break;

    case 3:
        // Go to Hopper Dredger
        mDredgeDB = new DredgeDB();
        mDredgeDB->show();

    default:
        break;
    }
}

这篇关于选择QComboBox后如何打开对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆