qt QLineEdit文本框只能输入数字 qt测试代码运行时间 qstring是区分大小写

Qt 限制文本框仅输入数字,正则表达式

QRegExp rx("^(\\d|[1-9]\\d|(1[0-2][0-3]))$");//输入范围为【0-123】
QRegExp rx2("^(-[1-9][0-9][0-9]|-[1-9][0-9]|-[1-9])|(500|\\d|[1-9]\\d|[1-4]\\d\\d)$");//输入范围限
制为【-999-500】
QRegExp rx3("^-?((\\d|[1-9]\\d)(\\.\\d{0,2})?)$");//输入范围是【-99.99-99.99】
QRegExpValidator *regval = new QRegExpValidator(rx);
QRegExpValidator *regval2 = new QRegExpValidator(rx2);
QRegExpValidator *regval3= new QRegExpValidator(rx3);//3 个 lineedit 输入限制
QLineEdit *lineEdit=new QLineEdit(this);
lineEdit->setValidator(regval);
lineEdit->setValidator(regval2);
lineEdit->setValidator(regval3);

QLineEdit 只能输入数字和点号

//请输入新尺寸中限制输入:只能输入数字和点号

QRegExp rx("^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");
EditMicroAdjust->setValidator(new QRegExpValidator(rx));

qstring是区分大小写

QString str1 = "aaa";
QString str2 = "AAA";
// QString 默认状态是区分大小写
// Qt::CaseSensitive 默认状态,区分大小写
// Qt::CaseInsensitive 这个是不区分大小写
qDebug() << str1.compare(str2); // 没填入第二个参数,默认区分大小写
qDebug() << str1.compare(str2, Qt::CaseInsensitive);
if (!str1.compare(str2, Qt::CaseInsensitive))
{
qDebug() << "true";
}
else
{
qDebug() << "false";
}

测试代码运行时间

方法一

QElapsedTimer mstimer;

QElapsedTimer mstimer;
mstimer.start(); 、、、、、你要测试的一段代码
float time=(double)mstimer.nsecsElapsed() / (double)1000000;

方法二

QTime time;

#include <QDebug>
#include <QTime>
#include <sys/time.h>
#include <windows.h>
#include <math.h>
void function(); //测试函数
int main(void)
{
qDebug()<<"-------------------------------";
/*************************************************/
//方法 1 利用 QTime,其精度为 ms 级
QTime time;
time.start();
function();
qDebug()<<time.elapsed()/1000.0<<"s";

//运行结果 0.109s

方法三

利用 gettimeofday(),其精度为 us 级

struct timeval tpstart,tpend;
float timeuse;
gettimeofday(&tpstart,NULL);
function();
gettimeofday(&tpend,NULL);
timeuse=(1000000*(tpend.tv_sec-tpstart.tv_sec) +
tpend.tv_usec-tpstart.tv_usec)/1000000.0;
qDebug()<<timeuse<<"s";

//运行结果:0.109375 s

方法四

利用 clock(),其精度为 ms 级

double time_Start = (double)clock();
function();
double time_End = (double)clock();
qDebug()<<(time_End - time_Start)/1000.0<<"s";

方法五

利用 windows.h(VC)函数,提精度为 us 级

LARGE_INTEGER litmp;
LONGLONG Qpart1,Qpart2,Useingtime;
double dfMinus,dfFreq,dfTime;
//获得 CPU 计时器的时钟频率
QueryPerformanceFrequency(&litmp);//取得高精度运行计数器的频率 f,单位
是每秒多少次(n/s),
dfFreq = (double)litmp.QuadPart;
QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
Qpart1 = litmp.QuadPart; //开始计时
function(); //待测试的计算函数等
QueryPerformanceCounter(&litmp);//取得高精度运行计数器的数值
Qpart2 = litmp.QuadPart; //终止计时
dfMinus = (double)(Qpart2 - Qpart1);//计算计数器值
dfTime = dfMinus / dfFreq;//获得对应时间,单位为秒,可以乘 1000000 精确到
微秒级(us)
Useingtime = dfTime*1000000;
qDebug()<<dfTime<<"s";

QComboBox触发

void (QComboBox::*p)(const QString &) = &QComboBox::currentIndexChanged;
connect(comboBox,p,[=]()
{
qDebug()<<"当前值"<< comboBox ->currentText();
});

// 重置窗口大小
resize(600,400);

// 菜单栏 只能最多有一个
//菜单栏创建
QMenuBar * bar = menuBar();
//将菜单栏放入到窗口中
setMenuBar(bar);
//创建菜单
QMenu * fileMenu = bar->addMenu(“文件”);
QMenu * editMenu = bar->addMenu(“编辑”);
//创建菜单项
QAction * newAction = fileMenu->addAction(“新建”);
//添加分割线

fileMenu->addSeparator();
QAction * openAction = fileMenu->addAction(“打开”);
//工具栏 可以有多个
QToolBar * toolBar = new QToolBar(this);
addToolBar(Qt::LeftToolBarArea,toolBar);
//后期设置 只允许 左右停靠
toolBar->setAllowedAreas( Qt::LeftToolBarArea | Qt::RightToolBarArea );
//设置浮动
toolBar->setFloatable(false);
//设置移动 (总开关)
toolBar->setMovable(false);

//工具栏中可以设置内容
toolBar->addAction(newAction);
//添加分割线
toolBar->addSeparator();
toolBar->addAction(openAction);
//工具栏中添加控件
QPushButton * btn = new QPushButton(“aa” , this);
toolBar->addWidget(btn);
//状态栏 最多有一个
QStatusBar * stBar = statusBar();
//设置到窗口中
setStatusBar(stBar);
//放标签控件
QLabel * label = new QLabel(“提示信息”,this);

stBar->addWidget(label);
QLabel * label2 = new QLabel(“右侧提示信息”,this);
stBar->addPermanentWidget(label2);
//铆接部件 (浮动窗口) 可以有多个
QDockWidget * dockWidget = new QDockWidget(“浮动”,this);
addDockWidget(Qt::BottomDockWidgetArea,dockWidget);
//设置后期停靠区域,只允许上下
dockWidget->setAllowedAreas( Qt::TopDockWidgetArea |
Qt::BottomDockWidgetArea );
//设置中心部件 只能一个
QTextEdit * edit = new QTextEdit(this);
setCentralWidget(edit)

#include <string>
#include <iostream>
#include <iosfwd>
#include <sstream>

std::string OneMath::DoubleToStr(const double& value_, const int num)
{
	     std::stringstream stristream;
	     stristream.setf(std::ios::fixed);
	     stristream.precision(num);
	     stristream << value_; //四舍五入
                     double _double = atof(stristream.str().c_str());
	     stristream.clear();

                     std::stringstream s;
                        s.unsetf(std::ios::showpoint); //unsetf换成setf,则会显示小数点后多余的0
                        s << _double;
                        return s.str();
}

C# 通过CLR调用 C++ 的方法和类 Visual Studio 2017

0准备工作
测试平台:Visual Studio 2017
操作系统:window 10
CLR: Common Language Runtime
CLI: Common Language Infrastructure

1:创建c++项目,需要被调用的c++方法【c++源代码 无需修改】

h代码

#pragma once
class __declspec(dllimport) TestCppClass
{
public:
	//测试参数有返回值
	int  TestCal(int value1,int value2);
};`

.cpp代码

#include "TestCppClass.h"

int TestCppClass::TestCal(int value1, int value2)
{
	return value1 + value2;
}

修改配置

进入输出目录可以看到生成的CppTest.dll 文件

2:实现CLR【封装要调用的接口】

设置库目录

添加lib文件

设置配置

生成dll

3:使用C#调用
添加引用

h

#pragma once
#include "../CppTest/TestCppClass.h"

using namespace System;
using namespace System::Collections::Generic;

public ref class TestCLR
{
private:
	TestCppClass* nativeHandle;
public:
	property TestCppClass* Handle
	{
		TestCppClass* get()
		{
			return nativeHandle;
		}
	}
public:
	TestCLR();
	~TestCLR();
	!TestCLR();
	void InstanceCleanup();
public:
	int TestCalClr(int value1,int value2);
};

Cpp

#include "TestCLR.h"

TestCLR::TestCLR()
{
	nativeHandle = new TestCppClass();
}

TestCLR::~TestCLR()
{
	this->!TestCLR();
}

TestCLR::!TestCLR()
{
	InstanceCleanup();
}

void TestCLR::InstanceCleanup()
{
	//以原子操作的形式,将整数设置为指定的值并返回原始值。
	int tempVal = (int)(void*)nativeHandle;
	int temp = System::Threading::Interlocked::Exchange(tempVal, 0);
	if (temp != 0)
	{
		if (nativeHandle)
		{
			delete nativeHandle;
			nativeHandle = 0;
			System::GC::SuppressFinalize(this);
		}
	}
}

int TestCLR::TestCalClr(int value1, int value2)
{
	return nativeHandle->TestCal(value1, value2);
}

4:修改后重新调用
需要删除生成的cs文件再重新生成
5:新增
如果新增c++结构体作为参数进行传递时如何在c#中使用。
6:参考
https://blog.csdn.net/qq_42067550/article/details/123681475
附:交流邮箱:yzxxty39@163.com

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值