QTable and checkbox signal/slot

2.9k views Asked by At

I'm newbie with QT and have read many topics, but I just don't get it. I hope that someone could help me with this.

So, I have created a loop where i add QTables in QGroupBoxes. Table's 2nd column is meant for QCheckBoxes and first column is meant for condition text, which changes when checkbox is ticked. Everything is working except the text won't change. So problem should be in the signal. I just can't figure it out :(

I would be glad for any help :)

    inputBox = new QGroupBox();
    QScrollArea *boxScroll = new QScrollArea();
    QHBoxLayout *boxLayout = new QHBoxLayout();
    boxTable = new QTableWidget();

    inputBox->setLayout(boxLayout);
    boxLayout->addWidget(boxTable);

    boxTable->verticalHeader()->setVisible(false);
    boxTable->setRowCount(24);
    boxTable->setColumnCount(5);
    boxTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    boxTable->setHorizontalHeaderItem(0, new QTableWidgetItem("ID"));
    boxTable->setHorizontalHeaderItem(1, new QTableWidgetItem("State"));
    boxTable->setHorizontalHeaderItem(2, new QTableWidgetItem("Enable"));


    for (int i=0; i<24; i++)
    {
        inCheck = new QCheckBox();
        iCheckLabel = new QLabel();
        QTableWidgetItem *id = new QTableWidgetItem();
        QTableWidgetItem *state = new QTableWidgetItem();
        QTableWidget *checkWidget = new QTableWidget();
        QHBoxLayout *checkLayout = new QHBoxLayout();

        checkLayout->setAlignment(Qt::AlignCenter);
        checkLayout->setContentsMargins(0,0,0,0);
        checkLayout->addWidget(inCheck);

        checkWidget->setLayout(checkLayout);

        id->setText(QString::number(i));
        id->setTextAlignment(Qt::AlignCenter);
        id->setFlags(id->flags() & ~Qt::ItemIsEditable);

        state->setText("Off");
        state->setTextAlignment(Qt::AlignCenter);
        state->setFlags(state->flags() & ~Qt::ItemIsEditable);
        state->setTextColor(Qt::red);

        boxTable->setItem(i, 0, id);
        boxTable->setItem(i, 1, state);
        boxTable->setCellWidget(i, 2, checkWidget);

        connect(checkWidget, SIGNAL(cellChanged(int,int)), this, SIGNAL(inCheckChecked(int, int)));

    }

    inputBox->setMinimumSize(350, 450);
    inputBox->setTitle(title);
    ui->scrollAreaWidgetContents->layout()->addWidget(inputBox);



void Project::inCheckChecked(int row, int col)
{
    QTableWidgetItem *item = boxTable->item(row, col);
    if (item->checkState() == true)
    {
        qDebug("is checked");
    }    
}
2

There are 2 answers

0
thuga On BEST ANSWER

You do something very strange here. You set a layout to your table widget and then add a check box to that layout, then you add this table widget to another table widget. Why? You could just use QTableWidget::setItem(int row, int column, QTableWidgetItem * item) to add a check box, like you use it for id and state items. Just make it a checkable item.

for (int i=0; i<24; i++)
{
    QTableWidgetItem *id = new QTableWidgetItem();
    QTableWidgetItem *state = new QTableWidgetItem();
    QTableWidgetItem *checkItem = new QTableWidgetItem();
    ...

    checkItem->setCheckState(Qt::Unchecked); // shuold be enough to make it checkable.. 
    //you can also set the needed flags

    boxTable->setItem(i, 0, id);
    boxTable->setItem(i, 1, state);
    boxTable->setItem(i, 2, checkItem);
}

Then you just connect the signal from boxTable to your slot, which will notify you when the checkbox state is changed.

5
Amol Saindane On

In this code, there is bug in below line

 connect(checkWidget, SIGNAL(cellChanged(int,int)), this, SIGNAL(inCheckChecked(int, int)));

For using connect, according to your implementation, you want to connect one signal and one slot, that consumes that signal. It should be as below :

 connect(checkWidget, SIGNAL(cellChanged(int,int)), this, SLOT(inCheckChecked(int, int)));

Get More info on Signals and Slots here