QT中的編譯報的一些錯誤

這篇是在10年測試QT過程中遇到的問題:

1、中文顯示問題:

#include <QApplication>
#include <QLabel>
#include <QTextCodec>

int main(int argc, char* argv[])
{
  QApplication app(argc,argv);
  QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
  QLabel *label = new QLabel(tr("這裏是中文"));
  label->Show();
  return app.exec();
}
編譯代碼,得到的錯誤是: 'tr'在此作用域中尚未聲明。
昨天爲什麼沒有出現這種錯誤呢?因爲昨天的代碼是從qt creator生成的MainWindow中挑出來的,tr被聲明爲QObject的一個static方法,因此在MainWindow中使用tr不會有問題。
把上面的
QLabel *label=new QLabel(tr("這裏是中文"));
改爲
QLabel *label=new QLabel(QObject::tr("這裏是中文"));

2、中文問題:

使用sqlite數據庫顯示亂碼的問題

本人近日在使用QT進行sqlite數據庫編程時,出現中文數據顯示亂碼情況,附源碼如下:
//main.cpp
#include <QtGui>
#include <QtCore/QTextCodec>
#include <QSqlTableModel>
#include <QTableView>
#include <QHeaderView>
#include <QSqlRecord>
#include <QtGui/QLabel>
#include <QString>
#include <QVariant>
#include "connection.h"
#include "sql.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));

    //創建數據庫連接
    if (!createConnection())
        return 1;
    //創建學生信息表
    createTables();
    //初始添加數據
   addData();
    enum{
        Student_Id = 0,
        Student_Schnum = 1,
        Student_Name = 2,
        Student_Sex = 3,
        Student_Nation = 4
    };

    QSqlTableModel *model = new QSqlTableModel();
    model->setTable("student");
    model->setSort(Student_Schnum, Qt::AscendingOrder);
    model->setHeaderData(Student_Schnum, Qt::Horizontal, QObject::tr("學號"));
    model->setHeaderData(Student_Name, Qt::Horizontal, QObject::tr("姓名"));
    model->setHeaderData(Student_Sex, Qt::Horizontal, QObject::tr("性別"));
    model->setHeaderData(Student_Nation, Qt::Horizontal, QObject::tr("民族"));
    model->select();

    QTableView *view = new QTableView;
    view->setModel(model);
    view->setSelectionMode(QAbstractItemView::SingleSelection);
    view->setSelectionBehavior(QAbstractItemView::SelectRows);
    view->setColumnHidden(Student_Id, true);
    view->resizeColumnsToContents();
    view->setEditTriggers(QAbstractItemView::NoEditTriggers);

    QHeaderView *header = view->horizontalHeader();
    header->setStretchLastSection(true);
    view->show();
    return a.exec();
}

//connection.h
#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlDriver>


inline bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("sim.dat");
    if (!db.open()) {
        QMessageBox::warning(0, QObject::tr("Database Error"),
                             db.lastError().text());
        return false;
    }
    return true;
}

#endif // CONNECTION_H

//sql.h
#include <QSqlQuery>


#ifndef SQL_H
#define SQL_H
inline void createTables()
{
    QSqlQuery query;
    query.exec("CREATE TABLE student ("
               "id INTEGER PRIMARY KEY, "
               "schnum INTEGER NOT NULL, "
               "name VARCHAR(40) NOT NULL, "
               "sex VARCHAR(4) NOT NULL, "
               "nation VARCHAR(10) NOT NULL)");
}

inline void addData(){
    QSqlQuery query;
    for(int i =0;i<100;i++){
    query.exec("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')");
}

}
#endif // SQL_H

上網查了許多無果,後來在閱讀一篇技術文章中無意發現,原來在插入數據語句若有中文必須先QObject::tr()一番,即進行編碼,
將Sql.h中query.exec("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')");改爲如下query.exec(QObject::tr("INSERT INTO student (schnum, name, sex, nation) VALUES (2614103, '天殘腳,'男', '漢族')"));
結果在顯示中都能得正確顯示。
注意,如果語句 QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));中的編碼改爲utf-8則會顯示亂碼。
3、中文問題:
如果使程序只支持一種編碼,也可以直接把整個應用程序的編碼設置爲GBK編碼, 然後在字符串之前 加tr(QObject::tr), qApp->setDefaultCodec( QTextCodec::codecForName("GBK") );
QLabel *label = new QLabel( tr("中文標籤") );
4、找不到<QtSql >
求助:提示無法打開包含文件QtSql
.Pro文件里加入  QT += sql
4、No rule to make target 'mkspecs/default/qmake.conf', needed by `Makefile'. Stop. 錯誤

mingw32-make: *** No rule to make target`http://www.cnblogs.com/http://www.cnblogs.com/Qt/4.3.3/mkspecs/default/qmake.conf',needed by `makefile'.  Stop.

make[2]: Entering directory `/home/lzy/tps2/rplan/super'
make[2]: *** No rule to make target `/home/lzy/qt/qt-3.3.2/mkspecs/default/qmake.conf', needed by `Makefile'.  Stop.
make[2]: Leaving directory `/home/lzy/tps2/rplan/super'

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/wangya216/archive/2009/06/08/4252689.aspx

5、mingw32\bin\ld.exe: cannot find -lqtmaind錯誤

這個錯誤是缺少某些庫,將mingw重新下載安裝即可。

6、編譯時可能會遇到如下錯誤:previous declaration 'long int InterlockedIncrement(long int*)' here

此爲qt的bug需要修改源代碼 (Qt\4.4.3\src\corelib\arch\qatomic_windows.h),
原文件如下:

Solution: 

(1) Qt\4.4.3\src\corelib\arch\qatomic_windows.h:
#if !(defined Q_CC_BOR) || (__BORLANDC__ < 0x560)
extern "C" {
__declspec(dllimport) long __stdcall InterlockedCompareExchange(long *, long, long);
__declspec(dllimport) long __stdcall InterlockedIncrement(long *);
__declspec(dllimport)long __stdcall InterlockedDecrement(long *); __declspec(dllimport) long__stdcall InterlockedExchange(long *, long);
__declspec(dllimport) long __stdcall InterlockedExchangeAdd(long *, long);
}
#else
extern "C" {
__declspec(dllimport) long __stdcall InterlockedCompareExchange(long volatile*, long, long);
__declspec(dllimport) long __stdcall InterlockedIncrement(long volatile*);
__declspec(dllimport) long __stdcall InterlockedDecrement(long volatile*);
__declspec(dllimport) long __stdcall InterlockedExchange(long volatile*, long);
__declspec(dllimport) long __stdcall InterlockedExchangeAdd(long volatile*, long);
}
#endif


you will see above code in Qt\4.4.3\src\corelib\arch\qatomic_windows.h: file. I modified like  below and it works.


/*#if !(defined Q_CC_BOR) || (__BORLANDC__ < 0x560)
extern "C" {
__declspec(dllimport) long __stdcall InterlockedCompareExchange(long *, long, long);
__declspec(dllimport) long __stdcall InterlockedIncrement(long *);
__declspec(dllimport)long __stdcall InterlockedDecrement(long *); __declspec(dllimport) long__stdcall InterlockedExchange(long *, long);
__declspec(dllimport) long __stdcall InterlockedExchangeAdd(long *, long);
}
#else */
extern "C" {
__declspec(dllimport) long __stdcall InterlockedCompareExchange(long volatile*, long, long);
__declspec(dllimport) long __stdcall InterlockedIncrement(long volatile*);
__declspec(dllimport) long __stdcall InterlockedDecrement(long volatile*);
__declspec(dllimport) long __stdcall InterlockedExchange(long volatile*, long);
__declspec(dllimport) long __stdcall InterlockedExchangeAdd(long volatile*, long);
}
// #endif

7、編譯錯誤,顯示 can not find -lqtmaind

這是qt的debug庫,安裝完成後需要再自己編譯這個庫。在Qt的開始菜單中,你可以找到一個程序 Qt 4.4.0 (Build Debug Libraries),運行這個程序就能編譯Qt的Debug庫了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章