我有一个 QTreeWidget,其中的每个项目在一列中都有一个 QComboBox。我已经将它连接到带有 QSignalMapper 的插槽,并且当它被触发时,我成功地检索了组合框中的项目和索引。我是这样做的:
foreach(Workplace *wp, allWorkplaces){
QTreeWidgetItem *workplaceItem = new QTreeWidgetItem;
workplaceItem->setText(0, wp->workplaceName());
workplaceItem->setText(1, wp->workplaceDescription());
myWorkplaceUi->treeWidget->addTopLevelItem(workplaceItem);
QComboBox *combo = new QComboBox();
combo->addItems(allShiftModels);
combo->setAutoFillBackground(true);
ShiftModel *shiftModel = qobject_cast<ShiftModel *>(wp->usedShiftModel);
myWorkplaceUi->treeWidget->setItemWidget(workplaceItem,2, combo);
if(shiftModel && !shiftModel->shiftModelName().isEmpty()){
qDebug()<<"after the cast: "<< shiftModel->shiftModelName();
combo->setCurrentIndex(combo->findText(shiftModel->shiftModelName(), Qt::MatchExactly));
}else{
combo->setCurrentIndex(combo->findText("None", Qt::MatchExactly));
}
connect(combo, SIGNAL(currentIndexChanged(int)), signalMapper, SLOT(map()));
signalMapper->setMapping(combo, QString("%1").arg(wp->workplaceName()));
}
connect(signalMapper, SIGNAL(mapped(const QString &)),this, SLOT(changed(const QString &)));
我的目标是,在检索Workplace
和之后ShiftModel
,在我已经创建的 Workplaces 的实例中更新它们。所以,基本上,我尝试找到选中的 Workplace 和 ShiftModel,因为根据选中的 ShiftModel,我将更改 Workplace 类中指向 ShiftModel 的指针:
class Workplace : public QObject
{
Q_OBJECT
public:
(...)
ShiftModel *usedShiftModel;
(...)
}
和changed
插槽:
void workplacesdialog::changed(QString position){
QList<Workplace* > allWorkplaces = this->myProject->listMyWorkplaces();
QList<ShiftModel*> allShiftModels = this->myProject->myFactory->listShiftModels();
foreach(Workplace* workplace, allWorkplaces){
foreach(ShiftModel *shiftmodel, allShiftModels){
qDebug() <<"workplace:"<< workplace->workplaceName();
qDebug() <<"shiftmodel:"<< shiftmodel->shiftModelName();
QString wp = position;
QTreeWidgetItem* item=(QTreeWidgetItem*)myWorkplaceUi->treeWidget->findItems(wp,Qt::MatchExactly,0).at(0);
QComboBox *combo = (QComboBox*)myWorkplaceUi->treeWidget->itemWidget(item,2);
if(combo && item){
QString sm = combo->currentText();
qDebug() << "selected shiftmodel "<< sm << " on workplace "<< wp;
if(workplace->workplaceName()==wp && shiftmodel->shiftModelName()==sm){
workplace->usedShiftModel = shiftmodel;
break;
}
else{
workplace->usedShiftModel = 0;
return;
}
}else{
qDebug() << "cast failed!";
return;
}
}
}
}
因此,我的问题是,当我单击其中一个组合框时,成功检索了所选项目和索引,但是,当我尝试使用foreach
插槽中的两个循环遍历它们时,它无法按预期工作。我希望每次单击组合框中的一个索引时,都会调用它,并且确实如此。尽管出于某种原因,我用来将用户选择的内容与已安装的内容相匹配的方法不起作用。
此外,它看起来只击中列表中的第 1 名和列表中的第 1workplace
名,这是我的问题。allWorkplaces
shiftmodel
ShiftModels
如果有人知道如何解决此问题或有任何想法要分享,请告诉我。谢谢你。