vendredi 8 janvier 2021

How to set the header of a QTableWidget on the second row?

I am trying to set up a simple but particular QTableWidget as follows:

On the first row I would like all QRadioButton On the second row I would like to see the header of the QTableWidget On the remaining cells I just put some example data.

How do I set the header of a QTableWidget on the second row?

I have written a minimal example code that you can see below:

#include <QApplication>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QRadioButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTableWidget *tableWidget = new QTableWidget;
    QVBoxLayout *lay = new QVBoxLayout();
    tableWidget->setRowCount(6);
    tableWidget->setColumnCount(6);

    QStringList strings; // Setting up the headers
    strings << "inputA" << "inputB" << "inputC" << "inputD"<< "inputE" << "inputF";
    tableWidget->setHorizontalHeaderLabels(strings);

    // Some example data
    for(int counterA = 0; counterA < tableWidget->rowCount(); counterA++) {
        for(int counterB = 0; counterB < tableWidget->columnCount(); counterB++) {
            tableWidget->setItem(counterA, counterB, new QTableWidgetItem(QString("blah").arg(counterA)));

            // Setting radio buttons on the first row
            QRadioButton *radioBtn = new QRadioButton(tableWidget);
            tableWidget->setCellWidget(0, counterA, radioBtn);
        }
    }
    lay->addWidget(tableWidget);
    tableWidget->show();
    return a.exec();
}

Unfortunately after doing some research I was not able to solve the problem and was wondering if anyone had the same problem and could please provide some guidance or point to it.

I studied this post but could not find a good way to implement that as no clear guidance is explained there.

Aucun commentaire:

Enregistrer un commentaire