我是QT的新手并且已经阅读了很多主题,但我还是没有得到它。我希望有人可以帮助我。
所以,我创建了一个循环,我在QGroupBox中添加了QTables。表的第2列用于QCheckBoxes,第1列用于条件文本,当复选框被勾选时,它会更改。 一切正常,除了文本不会改变。所以问题应该出在信号中。我无法弄明白:(
我很乐意提供任何帮助:)
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");
}
}
答案 0 :(得分:1)
你在这里做一些非常奇怪的事。您将布局设置为表窗口小部件,然后向该布局添加复选框,然后将此窗口窗口小部件添加到另一个窗口窗口小部件。为什么?您可以使用QTableWidget::setItem(int row, int column, QTableWidgetItem * item)
添加复选框,就像将其用于id和state项一样。只需将其作为可检查项目即可。
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);
}
然后您只需将boxTable
的信号连接到您的插槽,当复选框状态发生变化时,它会通知您。
答案 1 :(得分:0)
在此代码中,下面的行中存在错误
connect(checkWidget, SIGNAL(cellChanged(int,int)), this, SIGNAL(inCheckChecked(int, int)));
对于使用connect
,根据您的实施,您希望连接一个signal
和一个slot
,消耗signal
。它应该如下:
connect(checkWidget, SIGNAL(cellChanged(int,int)), this, SLOT(inCheckChecked(int, int)));
获取有关Signals and Slots
here