首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

关于Qt连接数据库的有关问题

2012-09-01 
关于Qt连接数据库的问题,求高手指教问题描述:我原本想把createConnection函数封装在类中,课是当我定义变量

关于Qt连接数据库的问题,求高手指教
问题描述:我原本想把createConnection函数封装在类中,课是当我定义变量new studentTable 时却出项了下列问题,导致我无法把createConnection函数封装在类中,问题我已经注释在语句上了,求高人指点,要如何才能把createConnection函数封装在类中 ????

C/C++ code
#include <QtGui>#include <QtSql>#include <iostream>#include <QTextCodec>using namespace std;#include "student_table.h"bool createConnection(){       QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");    db.setHostName("localhost");    db.setDatabaseName("edu_sys");    db.setUserName("root");    db.setPassword("123456");    if (!db.open())    {        QMessageBox::critical(0,QObject::tr("Database Error"), db.lastError().text());        return false;    }    return true;}int main(int argc, char *argv[]){    QApplication app(argc, argv);    QTextCodec *codec = QTextCodec::codecForName("GBK");    QTextCodec::setCodecForLocale(codec);    QTextCodec::setCodecForCStrings(codec);    QTextCodec::setCodecForTr(codec);        //StudentTable *studentTable = new StudentTable;//为什么在这边定义就无法连接到数据库,而在下面定义却可以???    if (!createConnection())    {        cout<<"error";        return 1;    }    StudentTable *studentTable = new StudentTable;    studentTable->show();    studentTable->resize(900,200);        return app.exec();}


[解决办法]
好好看看书

热点排行