Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Displaying set of images based on time



  • Hello , i have a set of images that are locally stored in system and have a database which looks something like this

    0_1538476666857_db.png

    When the user opens the app , it should read the system time and get the image based on which is closest to current time .

    currently i am doing something like this , don't know if it's right way but if you guys have better more optimized way please suggest. Below is the code

    QString return_diff(QHash<QString, QString> mantra)
    {
        QTime sys_time;
        QHash<QString, QTime>result;
            sys_time =  QTime::currentTime();
        QHashIterator<QString, QString> i(mantra);
        while(i.hasNext())
        {
            QString server_timeID = i.key();
            QString server_time = i.value();
            QStringList splitted_time = server_time.split(":");
    
            QString hrs = splitted_time.at(0);
            QString min = splitted_time.at(1);
            int int_hrs =hrs.toInt();
            int int_min = min.toInt();
            QTime notify_time(int_hrs,int_min,0,0);
            if(notify_time >= sys_time)
            {
                result.insert(server_timeID,notify_time);
            }
    
        }
        QString image;
        QHashIterator<QString, QTime> j(result);
        QTime min(0,0,0,0);
        while(j.hasNext())
        {
            if(j.value() < min)
            {
                image = j.key();
            }
        }
    
    
        return image;
    
    
    }
    
    QString dbmanager::getImage_url()
    {
        QString db_path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
        QString email = "";
    
        qDebug() << db_path;
        QDir dir(db_path);
        dir.cd("MANTRA_appdata");
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
           db.setDatabaseName(dir.absoluteFilePath("MANTRA.db"));
           if(!db.open())
           {
               qDebug() <<"error in opening DB";
           }
           else
           {
               qDebug() <<"connected to DB" ;
    
           }
           QSqlQuery query;
           QString image;
    
           query.prepare("SELECT email from User where ID = 1");
    
    
           if(query.exec() && (query.size() > 0 || query.size() != -1))
           {
              qDebug() << "done";
              email = query.value(0).toString();
              QHash<QString, QString> mantra;
              query.prepare("SELECT mantra_type time FROM Mantra WHERE enabled = true AND email = '" + email + "'");
              while(query.next())
              {
                  mantra.insert(query.value(0).toString(),query.value(0).toString());
              }
    
              image = return_diff(mantra);
    
           }
           else
           {
               image = "Mantra1.png";
           }
           image = dir.absolutePath() + "/" + image;
    
    
           return image;
    
    }
    
    

Log in to reply