mercredi 25 mars 2020

How to open widget after QCombobox is triggered

Below and here a minimal verifiable example that exactly replicates the problem I have.

I have aQTableWidget and on a specific cell there is a QCombobox that carries types to assign to fields, for example "INTEGER","DOUBLE","TEXT","DATE","TIME","BOOL","BLOB","ADD FIELD". If one of the types (e.g. LONG LONG or LONG BLOB) is not there, the QCombobox has the choice of ADD FIELD which should trigger a widget that opens up and allows to write and add the new field.

The expected behavior is like the one I show below that after choosing ADD FIELD, that triggers the opening of the add field widget dialog. but that is not happening. What am I missing?

add

Below the code:

main.cpp

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

newtabledialog.h

#include <QDialog>
#include "addfieldform.h"

QT_BEGIN_NAMESPACE
namespace Ui { class NewTableDialog; }
QT_END_NAMESPACE

class NewTableDialog : public QDialog
{
    Q_OBJECT
public:
    NewTableDialog(QWidget *parent = nullptr);
    ~NewTableDialog();
private slots:
    void on_addFields_combo_index_activated(int index);
private:
    Ui::NewTableDialog *ui;
    QAction *mActionField;
    QAction *mActionDeleteField;
    AddFieldForm *mNewField;
};
#endif // NEWTABLEDIALOG_H

newtabledialog.cpp

#include "newtabledialog.h"
#include "ui_newtabledialog.h"
#include <QAction>
#include <QComboBox>
#include <QRadioButton>
#include <QCheckBox>
#include <QPushButton>

NewTableDialog::NewTableDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::NewTableDialog)
{
    ui->setupUi(this);
    ui->tableWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
    mActionField = new QAction(QIcon(":"), tr("Add"), this);
    mActionDeleteField = new QAction(QIcon(":"), tr("Remove"), this);
    ui->tableWidget->addActions({mActionField, mActionDeleteField});

    auto newComboBox = [&](QWidget *parent) {
        auto combo = new QComboBox(parent);
        combo->addItems({"INTEGER","DOUBLE","TEXT","DATE","TIME","BOOL","BLOB", "ADD FIELD"});
        return combo;
    };

    connect(newComboBox, QOverload<int>::of(&QComboBox::activated),
        [=](int index) { on_addFields_combo_index_activated(index); });

    connect(mActionField, &QAction::triggered, [&]() {
        int rowCount = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(rowCount);
        ui->tableWidget->setCellWidget(rowCount, 1, newComboBox(this));
        ui->tableWidget->setCellWidget(rowCount, 2, new QRadioButton(this));
        ui->tableWidget->setCellWidget(rowCount, 3, new QCheckBox(this));
        ui->tableWidget->setCellWidget(rowCount, 4, new QCheckBox(this));
        ui->tableWidget->show();
    });
    connect(mActionDeleteField, &QAction::triggered, [&]() {
        ui->tableWidget->removeRow(ui->tableWidget->currentRow());
    });
}

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

addfieldform.h

#include <QWidget>
namespace Ui {
class AddFieldForm;
}
class AddFieldForm : public QWidget
{
    Q_OBJECT
public:
    explicit AddFieldForm(QWidget *parent = nullptr);
    ~AddFieldForm();
private:
    Ui::AddFieldForm *ui;
};

#endif // ADDFIELDFORM_H

addfieldform.cpp

#include "addfieldform.h"
#include "ui_addfieldform.h"
AddFieldForm::AddFieldForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddFieldForm)
{
    ui->setupUi(this);
}
AddFieldForm::~AddFieldForm(){delete ui;}

So far I have been trying to trigger the opening of the widget using the combobox but as soon as I release the mouse 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.

Aucun commentaire:

Enregistrer un commentaire