jeudi 26 mars 2020

How to pass QTableWidget data type to QTableView header fields via QPushButton

On a QTableWidget I am creating fields for a table. After I am done and hit "Accept" QPushButton, I would like to pass those values as headers of a QTableView. How do I do that? Basically the expected result I am trying to achieve is the one shown below:

pass

Below the most important part of the code:

ROSLaserScanDialog::ROSLaserScanDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ROSLaserScanDialog)
{
    ui->setupUi(this);
    // TABLE VIEW ACTIONS
    setWindowTitle("new table");
    mModel = new QSqlTableModel(this);
    mModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    mModel->setTable("new table");
    mModel->select();
    ui->tableView->setModel(mModel);
    ui->tableView->setContextMenuPolicy(Qt::ActionsContextMenu);
    ui->tableView->addActions({addRow, eraseRow});
    // TABLE WIDGET ACTIONS ....
    // ......

    // TABLE WIDGET CONNECTIONS
    auto laserTypeComboBox = [&](QWidget *parent) {
      auto laserCombo = new QComboBox(parent);
      laserCombo->addItems({"INTEGER","DOUBLE","TEXT","DATE","TIME","BOOL","BLOB"});
      return laserCombo;
    };
    connect(mActionAddField, &QAction::triggered, [&]() {
       int rowCount = ui->tableWidget->rowCount();
       ui->tableWidget->insertRow(rowCount);
       ui->tableWidget->setCellWidget(rowCount, 1, laserTypeComboBox(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();
    });
}

QString ROSLaserScanDialog::result() const
{
    QString res = QString("CREATE TABLE IF NOT EXISTS %1 (").arg(ui->lineEdit->text());
    int rowCount = ui->tableWidget->rowCount();
    for (int i = 0; i<rowCount; ++i) {
        auto name = ui->tableWidget->item(i, 0)->text();   // this is the name of the Reference of the table
        auto type = static_cast <QComboBox *> (ui->tableWidget->cellWidget(i, 1))->currentText();
        bool pk = static_cast <QRadioButton *> (ui->tableWidget->cellWidget(i, 2))->isChecked();
        bool nn = static_cast <QCheckBox *> (ui->tableWidget->cellWidget(i, 3))->isChecked();
        bool ai = static_cast <QCheckBox *> (ui->tableWidget->cellWidget(i, 4))->isChecked();
        //bool detail = static_cast <QPushButton *> (ui->tableWidget->cellWidget(i,5))->isEnabled();

        res += name + " " + type;
        if (pk) {
            res += " PRIMARY KEY";
        }
        if (ai) {
            res += " AUTOINCREMENT";
        }
        if (nn) {
            res += " NOT NULL";
        }
        res += ",";
    }
    res[res.size()-1] = ')';
    return res;
}


void ROSLaserScanDialog::setName(QString newName)
{
    ui->lineEdit->setText(newName);
}


void ROSLaserScanDialog::on_acceptBtn_clicked()
{
    mModel->submitAll();
}

What I have done so far:

Posts that I looked at but that were not able to solve the problem were: this one, this one, and also this. However all posts did not face exactly the problem as they were more providing explanations. I am not sure if the link between the two should be a connect statement via SIGNAL and SLOT (of course using the latest notation), or if there should be a different way and the problem is much simpler than what I think.

I didn'tr stop there and dug more into the problem and arrived to this post which is super useful in terms of creating headets for a QTableWidget, but how do I pass those values as headers of a QTableView?

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

Aucun commentaire:

Enregistrer un commentaire