我正在尝试使用 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);
}
我在哪里做错了?如果您需要更多信息,请告诉我。:)