How to create a simple class in QT ?



  • Hi everyone,i'm trying to make a c++ program in qt,but i dont want to use Gui in this moment.
    Can anyone explain me how to create a simple that ,that would be like the other classes that I may create in visual basic ?
    Thank You.



  • 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. :-)



  • 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


  • Lifetime Qt Champion

    Hi,

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



  • 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.



  • 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



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



  • 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



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



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



  • Thank You :)
    It works now :))



  • 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



  • 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. :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.