0

我正在尝试使用 Qt 按数量过滤列表的元素。只是我按下过滤器按钮,没有任何反应。也许我在 SLOT 函数中做错了什么。我正在使用的过滤器功能在我没有使用 Qt 的其他程序中完美运行。这是窗户的样子:

http://i.imgur.com/dKbZ50R.png?1 - I uploaded the picture with the windows on imgur because I don't have 10 reputation to upload it from here.

void MedicineManagementGUI::openFilterWindow(){
    ui.filterWindow = new QWidget;
    uiFilterMedicine.setupUI(ui.filterWindow);
    QObject::connect(uiFilterMedicine.btnFilterQuantity,SIGNAL(clicked()),uiFilterMedicine.filterWindow,SLOT(filterAllByQuantity()));
    ui.filterWindow->show();
}

过滤器插槽

void MedicineManagementGUI::filterAllByQuantity(){
    uiFilterMedicine.medFilterList->clear();
    QString quantityQString = uiFilterMedicine.txtQuantity->text();
    int quantity = quantityQString.toInt();
    List<Medicine*> filtered = ctrl->filterByLessQuantity(quantity);
    for(int i=0; i < filtered.size(); i++){
        int ID = filtered.getElement(i)->getID();
        string name = filtered.getElement(i)->getName();
        float concentration = filtered.getElement(i)->getConcentration();
        int quantity = filtered.getElement(i)->getQuantity();

        QString iDQString = QString::number(ID);
        QString nameQString = QString::fromStdString(name);
        QString concentrationQString = QString::number(concentration);
        QString quantityQString = QString::number(quantity);

        QString medicineAsString = iDQString+" - "+nameQString+" - "+concentrationQString+" - "+quantityQString;
        QListWidgetItem* item = new QListWidgetItem(medicineAsString, uiFilterMedicine.medFilterList);
        item->setData(Qt::UserRole,iDQString);
    }
}

过滤器功能: 类:

class Filter{
public:
    virtual bool include(Medicine* m)=0;
    virtual ~Filter(){};
};

class LessThanQuantity: public Filter{
private:
    int quantity;
public:
    LessThanQuantity(int q){
        this->quantity=q;
    }
    bool include(Medicine* m){
        return m->getQuantity()<quantity;
    }
};

控制器的过滤功能:

List<Medicine*> Controller::filterBy(Filter* filter){
    List<Medicine*> rez = List<Medicine*>();
    List<Medicine*> medList = repo->getAll();
    Iterator<Medicine*> it = medList.getIterator();
    for(it.first(); it.valid(); it.urmator()){
        Medicine* m = it.element();
        if(filter->include(m)){
            rez.insert(rez.size(),m);
        }
    }
    return rez;
}

List<Medicine*> Controller::filterByLessQuantity(int quantity){
    LessThanQuantity* f = new LessThanQuantity(quantity);
    return filterBy(f);
}

我在哪里做错了?如果您需要更多信息,请告诉我。:)

4

1 回答 1

0

http://qt-project.org/doc/qt-4.8/debug.html

我会添加

#include <QDebug>

// ...

// at the top of each function that might not get called
qDebug() << Q_FUNC_INFO;

这应该回答您关于您的插槽是否被调用的问题。控制台也可以打印出运行时错误,例如QObject::connect调用失败等。

编辑:另外我会打印出任何可能影响进入循环的关键指标,例如......

List<Medicine*> filtered = ctrl->filterByLessQuantity(quantity);
// this line added before the loop
qDebug() << "filtered.size()" << filtered.size();

如果打印出的大小为零,则在您的filterByLessQuantity函数中添加一些调试语句并查看丢失元素的位置。

希望有帮助。

于 2013-05-17T22:21:48.283 回答