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");
}
}
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.Then you just connect the signal from
boxTable
to your slot, which will notify you when the checkbox state is changed.