Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    (solved)Database in c++ and exposing in qml

    QML and Qt Quick
    3
    5
    2846
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      daljit97 last edited by

      Hi, I'm working on a listview. I have created a database in javascript to enable persistent storage. But I've noticed that the performances aren't very goos as the app takes long time to storage simple data. So i decided to make the database in pure C++, but i don't know how to do it (note that I am a beginner), I've searched and googled very much but I only find static databases and models that can't be edited directly from qml.
      Can anyone help me?

      Update : i've created the following database but it creates many errors like this : undefined refernce to...
      @#include "bookmarks.h"
      #include "QDateTime"
      #include "QtSql/QSqlQuery"
      Bookmarks::Bookmarks(QSqlDatabase database,QObject *parent) :
      QSqlTableModel(parent, database)
      {
      connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SIGNAL(countChanged()));
      connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SIGNAL(countChanged()));
      setEditStrategy(OnManualSubmit);
      }
      void Bookmarks::generateRoleNames()
      {
      for (int i = 0; i < this->columnCount(); i++)
      m_roles[Qt::UserRole + i] = this->headerData(i, Qt::Horizontal).toByteArray();

      setRoleNames(m_roles);
      

      }
      QString Bookmarks::tableCreateQuery() const
      {
      return QLatin1String("CREATE TABLE IF NOT EXISTS bookmarks (id INTEGER PRIMARY KEY AUTOINCREMENT,"
      "name VARCHAR, url VARCHAR, dateAdded DATE, thumbnail BLOB DEFAULT NULL);");
      }

      void Bookmarks::setFilter(const QString& filter)
      {
      QSqlTableModel::setFilter(filter);
      }

      bool Bookmarks::select()
      {
      return QSqlTableModel::select();
      }

      void Bookmarks::insert(const QString& name, const QString& url)
      {
      QModelIndex index = QModelIndex();
      beginInsertRows(index, rowCount(index), rowCount(index));
      QDateTime dateTime = QDateTime::currentDateTime();
      QString dateTimeString = dateTime.toString("hh_mm_ss yyyy_MMM_dd");
      QSqlQuery sqlQuery;
      sqlQuery.prepare("INSERT INTO bookmarks (name, url, dateAdded) VALUES (?, ?, ?)");
      sqlQuery.addBindValue(name);
      sqlQuery.addBindValue(url);
      sqlQuery.addBindValue(dateTimeString);
      sqlQuery.exec();
      select();
      endInsertRows();
      submitAll();
      }

      void Bookmarks::remove(const QString& url)
      {
      if (!contains(url))
      return;

      QSqlQuery sqlQuery;
      sqlQuery.prepare("SELECT id FROM bookmarks WHERE url = ?");
      sqlQuery.addBindValue(url);
      sqlQuery.exec&#40;&#41;;
      sqlQuery.first(&#41;;
      int indexToDelete = -1;
      for (int row = 0; row < rowCount(); ++row) {
          if (createIndex(row, 0).data(Qt::DisplayRole).toInt() == sqlQuery.value(0).toInt()) {
              indexToDelete = row;
              break;
          }
      }
      
      if (indexToDelete >= 0) {
          beginRemoveRows(createIndex(indexToDelete, 0), indexToDelete, indexToDelete);
          removeRow(indexToDelete);
          submitAll();
          endRemoveRows();
      }
      

      }

      void Bookmarks::togglePin(const QString& url)
      {
      if (contains(url))
      remove(url);
      else
      insert(url, url);
      }

      void Bookmarks::update(int index, const QString& name, const QString& url)
      {
      QModelIndex nameIndex = createIndex(index, 1);
      QModelIndex urlIndex = createIndex(index, 2);
      QModelIndex dateAddedIndex = createIndex(index, 3);
      setData(nameIndex, name);
      setData(urlIndex, url);
      setData(dateAddedIndex, QDateTime::currentDateTime);
      emit dataChanged(nameIndex, dateAddedIndex);
      submitAll();
      }

      bool Bookmarks::contains(const QString& url)
      {
      QSqlQuery sqlQuery;
      sqlQuery.prepare(QLatin1String("SELECT id FROM bookmarks WHERE url = ?"));
      sqlQuery.addBindValue(url);
      sqlQuery.exec();
      return sqlQuery.first();
      }@

      1 Reply Last reply Reply Quote 0
      • C
        chriadam last edited by

        Are you using QtQuick 2.0? If so, perhaps the "local storage api":http://doc-snapshot.qt-project.org/5.0/qtquick-modelviewsdata-localstorage.html will help.

        As for the errors you're seeing, they are link time errors, I presume? That usually means you haven't implemented some of the functions specified in the class declaration.

        Cheers,
        Chris.

        1 Reply Last reply Reply Quote 0
        • D
          daljit97 last edited by

          Thanks for your help, but i'm not using QtQuick 2.0.
          Update : i've resolved "undefined" type errors (i forgot to add database = QSqlDatabase::addDatabase("QSQLITE");) but now it says : collect: id returned 1 exit status error

          1 Reply Last reply Reply Quote 0
          • A
            AleksandarJovanov last edited by

            It is not id,but ld.
            Ld is the standard GNU linker program.
            It used to link yout program with libraries it uses.
            If you use QML then you need the declarative libraries.
            If you use QSockets then you need networking librararies,etc.
            The most probable reason for your problem is that your project file does
            not contain references to Qt sql database libs.
            Open your project file (the one ending in .pro) and find the starting with QT
            at the end of the line add sql
            So it looks like :
            QT += networking sql (NOTE: This is an example your line does not have to be exactly like this one)
            If you cannot find such line then add it like this
            QT += sql
            This was taken from http://stackoverflow.com/questions/3315833/how-can-i-add-the-qtsql-library-in-qt-creater
            Because you are a beginner,I would advise reading some articles about C++ linking.
            Google for c++ linkers help.

            1 Reply Last reply Reply Quote 0
            • D
              daljit97 last edited by

              Oh, thanks. I've resolved. I forgot a "+", i wrote QT = sql. now it's working

              1 Reply Last reply Reply Quote 0
              • First post
                Last post