【原创 Qt】合集

配置

.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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值