Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. How to create a simple class in QT ?
Forum Updated to NodeBB v4.3 + New Features

How to create a simple class in QT ?

Scheduled Pinned Locked Moved Language Bindings
13 Posts 3 Posters 5.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Alek Śmierciak
    wrote on last edited by
    #2

    It is hard to understand what you intend to do with such a simple class. Could you specify your use case or a goal?

    Besides, take note it's Qt, not QT. :-)

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Amarda_4
      wrote on last edited by
      #3

      I am working to do an e-voting system.
      I have to create some classes.for example Voter
      This Voter has to do the identification with MySql database.
      My problem is that when I create a Gui application my program connects with MySql database
      On the other hand when I create a c++ source file on Qt i cannot connect my program with Mysql databse

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Hi,

        Then create a console application using the same code to connect to the database.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Alek Śmierciak
          wrote on last edited by
          #5

          GUI itself should not guarantee database connection; the business logic classes should. If you need help with connecting to a MySQL database show us some code you have come up with so far.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Amarda_4
            wrote on last edited by
            #6

            I try this function to make the connection:

            @
            bool connectDatabase()
            {
            QSqlDatabase db = SqlDatabase::addDatabase("QMYSQL");
            db.setHostName("localhost");
            db.setDatabaseName("evoting");
            db.setUserName("root");
            db.setPassword("");
            if (!db.open())
            {
            return false; }
            return true;
            }
            @

            It shows me this error : QSqlDatabase was not declared in this scope

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Alek Śmierciak
              wrote on last edited by
              #7

              You need to include QSqlDatabase as well as change your .pro file to reflect such library usage:
              @ QT += sql @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Amarda_4
                wrote on last edited by
                #8

                I try this function to make the connection:
                @
                bool connectDatabase()
                { QSqlDatabase db = SqlDatabase::addDatabase(“QMYSQL”);
                db.setHostName(“localhost”);
                db.setDatabaseName(“evoting”);
                db.setUserName(“root”);
                db.setPassword(”“);
                if (!db.open()) {
                return false; }
                return true;
                }
                @
                It shows me this error : QSqlDatabase was not declared in this scope

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Amarda_4
                  wrote on last edited by
                  #9

                  Now it says:
                  SqlDatabase has not been declared in function bool connectDatabase

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Alek Śmierciak
                    wrote on last edited by
                    #10

                    Indeed. It is QSqlDatabase that is the name of the class, not SqlDatabase.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Amarda_4
                      wrote on last edited by
                      #11

                      Thank You :)
                      It works now :))

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Amarda_4
                        wrote on last edited by
                        #12

                        I have another problem :(
                        I made a query that will do the comparison for the voter id that a voter has in database and the id that the voter inputs.
                        the code is :
                        @
                        QSqlQuery query;
                        query.prepare("SELECT* FROM evoting WHERE v_id =? ");
                        query.addBindValue.....???
                        query.exec();
                        if (query.size()>0)
                        {
                        query.next();
                        cout<<"ID is correct";
                        }
                        else
                        { cout<<"Incorrect ID";
                        }
                        @

                        The problem is that I dont know how to take the value of Id from my constructor and put it to the query to make the comparison..
                        Please help me

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Alek Śmierciak
                          wrote on last edited by
                          #13

                          It would be appropriate for you to create another forum thread for this different issue. This way, the moderators will live longer and anyone with similar issues might find such a thread easier. I am not sure about the scope of this forums, though - the question you ask is isolated and has no ties to Qt.

                          Basically, if you want to execute a SELECT query for a given value, you compare this value with the name of the table's column:
                          @SELECT * FROM evoting WHERE v_id = found_id;@

                          Please also do use "@ for code wrapping":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01 whenever you want to paste a block of code into your post. :-)

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved