How to create a simple class in QT ?
-
wrote on 25 May 2013, 18:07 last edited by
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. -
wrote on 25 May 2013, 18:31 last edited by
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. :-)
-
wrote on 25 May 2013, 20:23 last edited by
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 -
Hi,
Then create a console application using the same code to connect to the database.
-
wrote on 25 May 2013, 20:36 last edited by
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.
-
wrote on 25 May 2013, 20:47 last edited by
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
-
wrote on 25 May 2013, 20:50 last edited by
You need to include QSqlDatabase as well as change your .pro file to reflect such library usage:
@ QT += sql @ -
wrote on 25 May 2013, 20:50 last edited by
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 -
wrote on 25 May 2013, 20:55 last edited by
Now it says:
SqlDatabase has not been declared in function bool connectDatabase -
wrote on 25 May 2013, 20:56 last edited by
Indeed. It is QSqlDatabase that is the name of the class, not SqlDatabase.
-
wrote on 25 May 2013, 21:05 last edited by
Thank You :)
It works now :)) -
wrote on 25 May 2013, 22:06 last edited by
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 -
wrote on 25 May 2013, 22:16 last edited by
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. :-)
4/13