zoukankan      html  css  js  c++  java
  • 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库了。

  • 相关阅读:
    std thread
    windows更新包发布地址
    How to set up logging level for Spark application in IntelliJ IDEA?
    spark 错误 How to set heap size in spark within the Eclipse environment?
    hadoop 常用命令
    windows 安装hadoop 3.2.1
    windows JAVA_HOME 路径有空格,执行软连接
    day01MyBatisPlus条件构造器(04)
    day01MyBatisPlus的CRUD 接口(03)
    day01MyBatisPlus入门(02)
  • 原文地址:https://www.cnblogs.com/AriLee/p/1997277.html
Copyright © 2011-2022 走看看