vendredi 26 juin 2015

QTable and checkbox signal/slot

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");
    }    
}




Aucun commentaire:

Enregistrer un commentaire