How to open a dialog after QComboBox choice

让人想犯罪 __ 提交于 2021-02-11 14:50:40

问题


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

This is the header file

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;
}

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.


回答1:


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

Signals & Slots in Qt5
New Signal Slot Syntax
qOverload<>

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().

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;
    }
}


来源:https://stackoverflow.com/questions/60726294/how-to-open-a-dialog-after-qcombobox-choice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!