Using match instead of looping.
[emufront] / src / widgets / efcombobox.cpp
index 9246b8e..c30a371 100644 (file)
 #include <QSqlQueryModel>
 #include <QSqlRecord>
 #include <QDebug>
+#include <QTime>
+#include <QAbstractItemView>
 
 EFComboBox::EFComboBox(DatabaseManager *dbMan, QWidget *parent)
     : QComboBox(parent), dbManager(dbMan)
 {
     if (!dbManager)
         throw new EmuFrontException("Database manager is not available!");
+    setSizeAdjustPolicy(QComboBox::AdjustToContents);
     updateDataModel();
 }
 
@@ -76,17 +79,13 @@ void EFComboBox::setSelected(const EmuFrontObject *efo)
         << " [" << efo->getId() << "].";
     QSqlQueryModel *qmodel
         = dynamic_cast<QSqlQueryModel*>(model());
-    for (int i = 0; i < qmodel->rowCount(); i++){
-        QSqlRecord rec = qmodel->record(i);
-        int id = rec.value(dataModelIndex_id).toInt();
-        qDebug() << "Checking record with id " << rec.value(dataModelIndex_id)
-            << " and name " << rec.value(dataModelIndex_name);
-        if (id == efo->getId()){
-            qDebug() << "Found!";
-            setCurrentIndex(i);
-            show();
-            break;
-        }
-    }
+    QModelIndex idStart = qmodel->index(0, dataModelIndex_id);
+    int targetId = efo->getId();
 
+    QModelIndexList indLst = qmodel->match(idStart,Qt::DisplayRole, targetId, 1);
+    if (indLst.count() >= 1) {
+        QModelIndex ind = indLst.first();
+        view()->setCurrentIndex(ind);
+        setCurrentIndex(ind.row());
+    }
 }