Home > Back-end >  Send parameters with dynamically generated QComboBox
Send parameters with dynamically generated QComboBox

Time:10-18

I want to insert a QComboBox inside a QTableWidget. When I change the Index of the CB I'll call a methode to change the status in the sqlite table. But for this, I need do pass two parameters to the methode. The ID(First element of the row), and the current index of the CB.

I generate the QTableWidget like that:

...
for(int i = 0; i < dbModel->rowCount();i  ){
    row = dbModel->record(i);
    ui->tW_Services->setItem(i,0,new QTableWidgetItem(row.field(0).value().toString()));
    ui->tW_Services->setItem(i,1,new QTableWidgetItem(row.field(1).value().toString()));
    ui->tW_Services->setItem(i,2,new QTableWidgetItem(row.field(2).value().toString()));
    ui->tW_Services->setItem(i,3,new QTableWidgetItem(row.field(3).value().toString()));
    ui->tW_Services->setItem(i,4,new QTableWidgetItem(row.field(4).value().toString()   "€"));
    ui->tW_Services->setItem(i,5,new QTableWidgetItem(row.field(5).value().toString()   "€"));
    //ui->tW_Services->setItem(i,6,new QTableWidgetItem(row.field(6).value().toString()));

    QComboBox* combo = new QComboBox();
    combo->addItem("open");
    combo->addItem("paid");
    if(row.field(6).value().toString() != "paid") combo->setCurrentIndex(0);
    else combo->setCurrentIndex(1);
    connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboChanged(int)));
    ui->tW_Services->setCellWidget(i,6,combo);
}
...

and the slot looks like that:

    void MainWindow::onComboChanged(int ID)
{
    qDebug() << "ID:" << QString::number(ID);
}

But when I try to create the method with 2 args:

    void MainWindow::onComboChanged(int ID,int index)
{
    qDebug() << "ID:" << QString::number(ID);
}

and attach a second parameter and calling the slot like:

connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboChanged(row.field(0).value().toInt(),int)));

I get:

qt.core.qobject.connect: QObject::connect: No such slot MainWindow::onComboChanged(row.field(0).value().toInt(),int)

A minimal reproducible example: There's just a single pushbutton and a qtablewidget

mainwindow.h:

    #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

    void onComboChanged(int ID, int index);

    void onComboChanged2(int index);

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QComboBox>

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

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


void MainWindow::on_pushButton_clicked()
{
    ui->tableWidget->setColumnCount(4);
    ui->tableWidget->setRowCount(3);

    for(int i = 0; i < 3;i  ){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem("Test"));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem("Test 2"));

        QComboBox* combo = new QComboBox();
        combo->addItem("open");
        combo->addItem("paid");
        combo->setCurrentIndex(0);
        int j = i 1;

        //NOT WORKING
        //connect(combo, SIGNAL(currentIndexChanged(int)),this,
        //        [=](int index) {this->onComboChanged(j, index);});

        //WORKING
        //connect(combo, SIGNAL(currentIndexChanged(int)),SLOT(onComboChanged2(int)));
        ui->tableWidget->setCellWidget(i,3,combo);
    }
}

void MainWindow::onComboChanged(int ID, int index)
{
    qDebug() << "ID: " << ID << "index: "<< index;
}

void MainWindow::onComboChanged2(int index)
{
    qDebug() << "ID: " << index;
}

CodePudding user response:

Since currentIndexChanged only has one parameter, your slot cannot capture more than that. But, since the row ID does not change on emission, you can wrap onComboChanged into a lambda as shown here, which captures the row by copy:

connect(combo, &QComboBox::currentIndexChanged,
        this, [=](int index) {this->onComboChanged(i, index);});

Since your code is not a minimal reproducible example, I could not test this. Let me know, if it doesn't work.

  • Related