1

我有一个 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名,这是我的问题。allWorkplacesshiftmodelShiftModels

如果有人知道如何解决此问题或有任何想法要分享,请告诉我。谢谢你。

4

2 回答 2

1

问题是这样的:

if(workplace->workplaceName()==wp && shiftmodel->shiftModelName()==sm){
    workplace->usedShiftModel = shiftmodel;
    break;
}
else{
    workplace->usedShiftModel = 0;
    return;
}

如果工作场所名称不匹配或班次模型名称不匹配,则工作场所与其当前链接的班次模型之间的关系将被删除并且您的函数返回。

我可以为您重构两个 for 循环,但有一种更简单且不易出错的方法:

注意:我用“TODO”标记了一些代码路径,由于时间不够,我跳过了这些路径。不过,您应该能够自己弄清楚它们。

// Set up hashes for quick lookup
QHash< QString, Workplace* > workplaceHash;

QList<Workplace* > allWorkplaces = this->myProject->listMyWorkplaces();
foreach( Workplace* workplace, allWorkplaces )
{
    workplaceHash.insert( workplace, workplace->workplaceName() );
}

// TODO: Do a similar thing for the shift models here

// Find the selected workplace
if( !workplaceHash.contains( position ) )
{
    // TODO: Error handling (An unknown/No workplace was selected)
    return;
}
// else: A valid workplace was selected

Workplace* selectedWorkplace = workplaceHash.value( position );

// TODO: Retrieve the name of the shift model (stored in variable sm)

// Find the selected shiftmodel
if( !shiftplaceHash.contains( sm ) )
{
    // No shift model was selected
    selectedWorkplace->usedShiftModel= 0;
    return;
}
// Else: Both work place and shift model were selected

Shiftplace* selectedShiftModel = shiftplaceHash.value( sm );

selectedWorkplace->usedShiftModel = selectedShiftModel;

重构的一些想法:

  • 您可以在此方法之外创建散列并将它们存储在成员变量中。只需确保在添加或删除工作场所时始终更新哈希。
  • 您可以通过将部分代码提取到单独的方法中来更容易发现错误,例如QString getSelectedShiftModelName()等。
于 2013-09-02T13:44:10.003 回答
1

所以,最后我发现我的循环真的搞砸了......现在正在工作:

void workplacesdialog::changed(QString position){

    QList<Workplace* > allWorkplaces = this->myProject->listMyWorkplaces();
    QList<ShiftModel*> allShiftModels = this->myProject->myFactory->listShiftModels();

    qDebug() << allWorkplaces.size() << " workplaces";
    qDebug() << allShiftModels.size() << " ShiftModels";

    QString wp = position;
    QString sm;
    QTreeWidgetItem* item=(QTreeWidgetItem*)myWorkplaceUi->treeWidget->findItems(wp,Qt::MatchExactly,0).at(0);
    QComboBox *combo = (QComboBox*)myWorkplaceUi->treeWidget->itemWidget(item,2);
    if(combo && item){
        sm = combo->currentText();
        qDebug() << "selected shiftmodel "<< sm << " on workplace "<< wp;

    }else{
        qDebug() << "cast failed!";
        return;
    }

    foreach(Workplace* workplace, allWorkplaces){
        foreach(ShiftModel *shiftmodel, allShiftModels){
            qDebug() <<"workplace:"<< workplace->workplaceName();
            qDebug() <<"shiftmodel:"<< shiftmodel->shiftModelName();
            if(workplace->workplaceName()==wp && shiftmodel->shiftModelName()==sm){
                qDebug() << "found match!: "<< wp << " >>>>> " << sm;
                workplace->usedShiftModel = shiftmodel;
                return;
            }else if(workplace->workplaceName()==wp && sm=="None"){
                qDebug() << "clear match: "<< wp << " >>>>> " << sm;
                workplace->usedShiftModel = 0;
                return;
            }
        }
    }
}
于 2013-09-02T13:50:55.487 回答