天天看点

Qt学习备忘录(1)

整理Qt入门学习中遇到的简短代码,记下来备忘。

刚开始接触,什么都是不懂的,也不成体系,等熟悉以后回过头来再修改。

1. 创建Qt Gui应用,根据向导自动生成源文件、头文件、界面文件等。

2. 在头文件mainwindow.h中添加需要用到的Qt类以及opencv库文件。常用的包括:

<pre name="code" class="cpp">#include <QMainWindow>
#include <QCoreApplication>
#include <QFileInfo>
#include <QTextCodec>
#include <QStringList>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QString>
#include <QFileDialog>
#include <QMessageBox>
#include <QtGui/QMainWindow>
#include <QTime>
#include <QTimer>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
           

在头文件中声明opencv库的命名空间:

using namespace cv;
using namespace std;           
源文件中的全局变量也在.h文件里 class MainWindow : public QMainWindow{}中private:里面声明。           
3. opencv库的包含文件、库文件的设置:           
在项目视图下,双击.pro文件打开,添加如下内容:           
<pre name="code" class="cpp">INCLUDEPATH += /usr/local/include \
               /usr/local/include/opencv \
               /usr/local/include/opencv2

LIBS += /usr/local/lib/libopencv_highgui.so \
        /usr/local/lib/libopencv_core.so \
        /usr/local/lib/libopencv_imgproc.so           

如果需要用控制台来查看结果,添加:

CONFIG += console           

4.主要代码在 mainwindow.cpp 中实现,且已经有了框架:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ……;
}           
MainWindow::~MainWindow()
{
    delete ui;
}           

省略号部分填写自己的代码。

(main.cpp不做修改)。

5. 调试过程中想要查看变量值,首先打开监视窗口,方法:窗口->视图->本地和表达式。 必须在运行的时候才能查看监视窗口。发现监视窗口的变量

是随着代码运行而变化的,比如运行到后面时前面出现的变量就没有了。还有,在变量上右键->添加到监测窗口,就在监视窗口下方的窗口中出现了。

6.在指定文件夹中新建一个.txt文档,要求用一个for循环,每次都将输出数据写入到该txt文档中,数据不覆盖,依次往后写。代码如下:

QFile file("/home/puyn/qt-proj/savefile/file8.txt");
if( ! file.open(QIODevice::WriteOnly | QIODevice::Text)) {
    qDebug()<<file.errorString();
}
else {
    qDebug()<<"open is ok"<<endl;
}
QTextStream streamFile(&file);
streamFile.setCodec("UTF-8");

for( int i=0; i<10; i++)
{
    streamFile.seek(file.size());
    streamFile << "BEGIN"<<"\t"<<endl;
    ……;
    streamFile<<"END"<<endl;
}
file.close();           

在for循环内部,每次定位到txt文档的最后,然后开始写数据。for循环结束后close file.

7. QDir的使用。要求遍历指定文件夹中的所有图片文件,进行处理。代码:

QDir dir(tr("/home/puyn/qt-proj/pic-20160331/"));
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);
QFileInfoList list = dir.entryInfoList();

for(int i=0; i<list.size(); ++i)
{
    QFileInfo Input_Pic_Name = list.at(i);
    ......;
}
           

8.  每一次循环,实现读入图片,再以系统当前时间命名该图片并保存。代码:

QFileInfo Input_Pic_Name = list.at(i);
QString Input_Pic_Path = "/home/puyn/qt-proj/pic-20160331/"+Input_Pic_Name.fileName();
QImage img;
img.load(Input_Pic_Path);           
QString save_name_time;
QDateTime time = QDateTime::currentDateTime();
save_name_time = time.toString("yyyy-MM-dd_hh-mm-ss");
QString path ="/home/puyn/qt-proj/savepic/"+save_name_time;
img.save(path,"PNG");           

9. 延时程序,要求每次for循环中间停顿2秒钟。代码:

QTime dieTime = QTime::currentTime().addSecs(2);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents,100);           

10. Qt中的QString和c语言中的string之间转换。

QString Input_Pic_Path = "/home/puyn/qt-proj/pic-20160331/"+Input_Pic_Name.fileName();
string str;
str = Input_Pic_Path.toStdString();

string cntName = "000";
QString cntName_QStr = QString::fromStdString(cntName);           

11. 判断读入的Mat文件是否为空。

string str;
Mat image = imread(str);
if(!image.data) {
   QMessageBox msgBox;
   msgBox.setText(tr("image data is null"));
   msgBox.exec();
}           

12.  Mat 和 QImage之间的转换。

Mat 转 QImage,二值图的情况:

Mat temp1 = temp.clone();
const uchar *qImageBuffer = (const uchar*)temp1.data;
QImage Q_temp(qImageBuffer, temp1.cols, temp1.rows, temp1.step, QImage::Format_RGB888);
QImage Q_temp1 = Q_temp.rgbSwapped();
QString temp_path ="/home/puyn/qt-proj/savepic/temp/"+save_name_time+save_name_time;
Q_temp1.save(temp_path, "BMP");            

Mat 转 QImage, CV_8UC3的情况:

Mat roi = *it1;
cvtColor(roi,roi,CV_BGR2RGB);
QImage img_big;
img_big = QImage((const unsigned char*)(roi.data),roi.cols,roi.rows,roi.step,QImage::Format_RGB888);
QString path_big ="/home/puyn/qt-proj/savepic/pic_big/"+save_name_time+cntName_QStr;
img_big.save(path_big,"PNG");