配置
.pro
QT += core gui
QT += concurrent
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
include
//qt
#include <QApplication>
#include <QMainWindow>
#include <QImage>
#include <QCloseEvent>
#include <QTimer>
#include <QDebug>
#include <QTimer>
#include <QTime>
#include <QDir>
#include <QString>
//opencv
//#include <opencv2/opencv.hpp>
//#include <opencv2/core/core.hpp>
//#include "opencv2/imgproc/imgproc.hpp"
//#include "opencv2/highgui/highgui.hpp"
//#include <opencv2/features2d/features2d.hpp>
//using namespace cv;
//c++
#include<iostream>
#include<thread>
#include<vector>
using namespace std;
//自己的头文件
//自己的头文件
库
opencv
INCLUDEPATH+= D:\software\opencv\cmake_415_all\GPU\build\install\include
LIBS += -LD:\software\opencv\cmake_415_all\GPU\build\install\x64\vc16\lib -lopencv_world451d
halcon
#########QT_HALOCN
macx {
QMAKE_CXXFLAGS += -F/Library/Frameworks
QMAKE_LFLAGS += -F/Library/Frameworks
LIBS += -framework HALCONCpp
}
else {
#defines
win32:DEFINES += WIN32
#includes
INCLUDEPATH += "$$(HALCONROOT)/include"
INCLUDEPATH += "$$(HALCONROOT)/include/halconcpp"
#libs
QMAKE_LIBDIR += "$$(HALCONROOT)/lib/$$(HALCONARCH)"
unix:LIBS += -lhalconcpp -lhalcon -lXext -lX11 -ldl -lpthread
win32:LIBS += "$$(HALCONROOT)/lib/$$(HALCONARCH)/halconcpp.lib" \
"$$(HALCONROOT)/lib/$$(HALCONARCH)/halcon.lib"
}
QMainwindow
实例化
窗口属性设置
my_calib=new calib_ui(this);
my_calib->setWindowModality(Qt::WindowModal);
my_calib->setAttribute(Qt::WA_DeleteOnClose,true); //卡死
my_calib->show();
窗口指针
//cpp包含要指的窗口类
#include<start_ui.h>
start_ui *pmainwin=(start_ui *) parentWidget(); //多次->parent() 就可以多级显示
frame=pmainwin->pcamera->frame.clone();
qframe=pmainwin->pcamera->qframe.copy();
窗口实例化
QT控件
QTimer
void cnm()
{
QTimer* timer=new QTimer(this);
timer->start(200);//ms
connect(timer,&QTimer::timeout,this,&class::my_timer_fun);
}
QTime
模板
年月日时分秒
std::string get_time_date_now()
{
QTime *time=new QTime();
QDate *date=new QDate();
*date=QDate::currentDate();
int sec =time->currentTime().second();
int min=time->currentTime().minute();
int hour=time->currentTime().hour();
int day=date->day();
int month =date->month();
int year =date->year();
string std_year=std::to_string(year);
string std_month=std::to_string(month);
string std_day=std::to_string(day);
string std_hour=std::to_string(hour);
string std_min=std::to_string(min);
string std_sec=std::to_string(sec);
string time_now=std_year+"_"+std_month+"_"+std_day+"_"+std_hour
+"_"+std_min+"_"+std_sec;
return time_now;
}
QLabel
自适应大小
QPixmap pixmap = QPixmap::fromImage(pcamera->qframe);
int width=ui->label_oneget->width();
int height=ui->label_oneget->height();
QPixmap fitpixmap = pixmap.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 饱满填充
ui->label_oneget->setPixmap(fitpixmap);
QDir
选择格式的文件
路径
#include <QString>
#include <QDir>
#include <QDebug>
void get_pic_in_folder()
{
QString path="C:\\Users\\Administrator\\Desktop";
QDir dir(path);
QStringList name_filters;
name_filters<<"*.jpg"<<"*.png";
QStringList files=dir.entryList(name_filters,QDir::Files|QDir::Readable,QDir::Name);
for(int i=0;i<files.size();++i)
{
QString path_tmp=path;
path_tmp.append("\\");
QString one_file=files.at(i);
QString one_file_path=path_tmp.append(one_file);
qDebug()<<one_file_path;
}
}
其它
中文乱码
哪里要中文,哪里加
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
宏定义
#define cuda 1
# if cuda
//加cuda
net.setPreferableBackend(dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(dnn::DNN_TARGET_CUDA);
#endif
时间测试
#include<time.h>
clock_t start_time=clock();
#代码块1
clock_t end_time1=clock();
cout<< "Running time1 is: "<<static_cast<double>(end_time1-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
#代码块2
clock_t end_time2=clock();
cout<< "Running time2 is: "<<static_cast<double>(end_time2-end_time1)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
#代码块3
clock_t end_time3=clock();
cout<< "Running time3 is: "<<static_cast<double>(end_time3-end_time2)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间
中文字体
改cpp中添加
#pragma execution_character_set("utf-8")
或者main函数中
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif