QT基本使用

安装方法:

ubuntu12.04下安装QT方法:
http://blog.csdn.net/xsl1990/article/details/8299756

输入以下命令:

sudo apt-get install -y qt4-dev-tools qt4-designer qt4-doc qt4-qtconfig qt4-demos qt4-qmake libqt4-sql-mysql qdevelop qt-creator

QT 源码下载

 ftp://ftp.qt.nokia.com

QT 在线帮助文档

连接地址  ==> http://www.kuqin.com/qtdocument/index.html

http://www.cnblogs.com/emouse/archive/2013/01/28/2880142.html

Linux 下编译、安装、配置 QT

http://download.qt.io/official_releases/qt/5.4/5.4.0/single/qt-everywhere-opensource-src-5.4.0.zip.mirrorlist

Found 3 mirrors which handle this country (CN)

ubuntu系统环境, QT下编译程序的命令行 

对.cpp文件进行qmake -project 得到.pro文件,
   qmake -project

对.pro文件进行qmake 文件名.cpp,得到makefile文件。
    qmake  hello.pro

最后进行make,就是编译和连接,可能会报错的,哪一行出了什么错。如果正确,就可以得到可执行文件了。

基本使用方法:

状态栏显示方法

Qt Creator实现状态栏显示
http://blog.chinaunix.net/uid-23592843-id-1752889.html

[Qt教程] 第8篇 基础(八)设置Qt状态栏 (永久信息)
http://tscsh.blog.163.com/blog/static/2003201032013816102824673/

 tableview 和tablewidget 及 sqlite数据库的使用:

Qt学习笔记 TableWidget使用说明和增删改操作的实现
http://www.cnblogs.com/li-peng/p/3654634.html



QTableWidget与QTableView的区别
http://qimo601.iteye.com/blog/1530245

Qt TableView的简单使用 (对tableview的一些属性设置,如居中显示等)

http://blog.csdn.net/pk124729136/article/details/8108583

QT中使用QSqlQueryModel读取数据库问题

Qt数据库sqlite总结

http://blog.csdn.net/luckywang1103/article/details/8256624

    QString sql;
    QString result;
    string tmp;
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(DB_FILENAME);
    db.open();
    int id = ui->tvw_unpack->model()->index(index.row(),0).data().toString().toInt();

//    sql = QString("select data_start,data_end from avindex where id = %1;").arg(id);
    sql = QString("select * from avindex where id = %1;").arg(id);
    QSqlQuery query(db);
    query.exec(sql);
//id,stream_index,flags,pos,size,pts,dts
    int rowNum = query.at();//获取query所指向的记录在结果集中的编号
    int n_col = query.record().count();//获取每条记录中属性(即列)的个数
    int nindex = query.record().indexOf("flags");//获取”name”属性所在列的编号,列从左向右编号,最左边的编号为0
    query.first();
    int n_id = query.value(0).toInt();//获取id属性的值,并转换为int型
    qDebug() << " rowNum is : " << rowNum //将结果输出
             << " n_col is : " << n_col
             << " nindex is : " << nindex
             << " n_id is : " << n_id;
    int n_pts = query.value(0).toInt();
    int n_stream_index = query.value(1).toInt();
    int n_flags = query.value(2).toInt();
    int n_pos = query.value(3).toInt();
    int n_size = query.value(4).toInt();

    QByteArray data_start = qUncompress(query.value(7).toByteArray());
    QByteArray data_end = qUncompress(query.value(8).toByteArray());

//    QByteArray data_start = qUncompress(query.value(0).toByteArray());
//    QByteArray data_end = qUncompress(query.value(1).toByteArray());
    int i = data_start.length();
    int j = data_end.length();
    hex_dump_to_string(data_start.data(),i,result);
    tmp = result.toStdString();
    ui->textEdit->setText(result);
    data_start.data();
    hex_dump_internal(data_start.data(),i);

    hex_dump_internal("abcdefgh",8);
    int ii = data_start.length();

 单选框和复选框

http://blog.csdn.net/xgbing/article/details/7766529
 单选框和复选框

多线程程序设计:

QT 多线程程序设计
QT通过三种形式提供了对线程的支持。它们分别是,一、平台无关的线程类,二、线程安全的事件投递,三、跨线程的信号-槽连接。这使得开发轻巧的多线程Qt程序更为容易,并能充分利用多处理器机器的优势。多线程编程也是一个有用的模式,它用于解决执行较长时间的操作而不至于用户界面失去响应。在Qt的早期版本中,在构建库时有不选择线程支持的选项,从4.0开始,线程总是有效的。

线程类

Qt 包含下面一些线程相关的类:
QThread 提供了开始一个新线程的方法
QThreadStorage 提供逐线程数据存储
QMutex 提供相互排斥的锁,或互斥量
QMutexLocker 是一个便利类,它可以自动对QMutex加锁与解锁
QReadWriterLock 提供了一个可以同时读操作的锁
QReadLocker与QWriteLocker 是便利类,它自动对QReadWriteLock加锁与解锁
QSemaphore 提供了一个整型信号量,是互斥量的泛化
QWaitCondition 提供了一种方法,使得线程可以在被另外线程唤醒之前一直休眠。


http://www.cnblogs.com/hicjiajia/archive/2011/02/03/1948943.html

相关类型变量的使用

QByteArray使用方法举例:
http://blog.csdn.net/lyc_daniel/article/details/14230169

    QByteArray qByteArray("") ; //定义一个空QByteArray对象,记住括号内要有“”,否则所无效对象。
    qByteArray.append("daniel"); //后面添加字符串
    qDebug()<<"qByteArray = "<<qByteArray.data()<<"
"; //返回数据指针
    qDebug()<<"The size of qByteArray is "<<qByteArray.size()<<"
"; //返回大小,不保护末尾的‘
’
    qDebug()<<"The number of occurrences of 'a' is "<<qByteArray.count('a')<<"
"; //查询重复次数
    qByteArray.fill('a'); //更改填充值
    qDebug()<<"qByteArray = "<<qByteArray.data()<<"
";


 运行结果:

容器类QList和QMap

http://blog.csdn.net/a649518776/article/details/6630985

    QMap<QString,QString> map;    
    map.insert("beijing","111");    
    map.insert("shanghai","021");    
    map.insert("jinan","0531");    
    QMap<QString,QString>::iterator i;    
    for(i=map.begin();i!=map.end();++i)    
    {   
         qDebug()<<i.key()<<" "<<i.value();    
    }    
    i=map.find("beijing");    
    if(i!=map.end())   
    i.value()="010";  

 相关错误处理:

QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
QSqlQuery::exec: database not open

http://www.cppblog.com/seahouse/archive/2010/12/31/137131.html

Qt---在线程中传递类对象,槽函数不响应 

http://zhouzhenren163.blog.163.com/blog/static/65499281201461155415167/
QObject::connect: Cannot queue arguments of type 'TextAndNumber' (Make sure 'TextAndNumber' is registed using qRegisterMetaType().) 

Qt 线程间共享数据(qRegisterMetaType的使用) 

http://xinlongli.blog.163.com/blog/static/7342907620134272564462/


 Qt::ConnectionType 解析

http://blog.csdn.net/s04023083/article/details/4746544

signal/slot在底层会使用三种方式传递消息。参见QObject::connect()方法:
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection )
最后一个参数是就是传递消息的方式了,有四个取值:

Qt::DirectConnection
When emitted, the signal is immediately delivered to the slot.
假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这4个slot按连接的时间顺序调用一遍。显然这种方式不能跨线程(传递消息)。

Qt::QueuedConnection
When emitted, the signal is queued until the event loop is able to deliver it to the slot.
假设当前有4个slot连接到QPushButton::clicked(bool),当按钮被按下时,QT就把这个signal包装成一个 QEvent,放到消息队列里。QApplication::exec()或者线程的QThread::exec()会从消息队列里取消息,然后调用 signal关联的几个slot。这种方式既可以在线程内传递消息,也可以跨线程传递消息。

Qt::BlockingQueuedConnection
Same as QueuedConnection, except that the current thread blocks until the slot has been delivered. This connection type should only be used for receivers in a different thread. Note that misuse of this type can lead to dead locks in your application.
与Qt::QueuedConnection类似,但是会阻塞等到关联的slot都被执行。这里出现了阻塞这个词,说明它是专门用来多线程间传递消息的。

Qt::AutoConnection
If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection.
这种连接类型根据signal和slot是否在同一个线程里自动选择Qt::DirectConnection或Qt::QueuedConnection

这样看来,第一种类型的效率肯定比第二种高,毕竟第二种方式需要将消息存储到队列,而且可能会涉及到大对象的复制(考虑sig_produced(BigObject bo),bo需要复制到队列里)。

 [Qt] postevent emit

http://blog.csdn.net/liuysheng/article/details/6723632

aa

原文地址:https://www.cnblogs.com/jingzhishen/p/4063562.html