0

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.

EsoMars
  • 337
  • 3
  • 14
  • Can you add an example of the result you would like to achieve? (Maybe a screenshot?) Also, what is the "in order to" of your task? (Just wondering if the question you are asking is addressing the problem you are trying to solve.) – hcc23 Jan 08 '21 at 18:01
  • hi @hcc23, I tried to post a screenshot but I need at least 10 point reputation so I can't do that. What I am trying to achieve is having a `QTableWidget` with the headers on the second row instead of the first row, because on the first row I only want `QRadioButton`. I hope it is clearer. Sorry again for the image, I will upload it as soon as I have the reputation. – EsoMars Jan 08 '21 at 19:58

0 Answers0