C++/Qt 小知识记录

工作中遇到的一些小问题,总结的小知识记录:C++/Qt,夹杂点GIS。


从QFile拷贝文件到另一处,打开目的文件却因ReadOnly无法操作

解决方案一:
读取源文件Cache内容,新建一个可读可写的目的文件,拷贝Cache到其中;

解决方案二:
目的文件改变其权限:

QFile::setPermissions(Public_canshufile,QFileDevice::ReadOther|QFileDevice::WriteOther);

去除浮点数科学计数法的显示

// Qt处理
		QString strLongitude = QString::number(dLongitude, 'f', 15);
		strLongitude.remove(QRegExp("0+$"));
		strLongitude.remove(QRegExp("\\.$"));
// std处理
		std::stringstream ss;
		ss << std::setfill('0') << std::setprecision(14) << std::fixed << m_dDegree;
		std::string str = ss.str();

Shp格式的限制

属性名最多为10字符;
dbf属性表最多255个字段;
shp文件和dbf文件最大2GB;
每个shp只能是一种几何类型;

数据库整型的设置

Type Bytes Minimum Value Maximum Value
(Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
0 255
SMALLINT 2 -32768 32767
0 65535
MEDIUMINT 3 -8388608 8388607
0 16777215
INT 4 -2147483648 2147483647
0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
0 18446744073709551615

时间戳与日期的转换(Windows)

// 精确到100纳秒级别的时间戳
FILETIME fileTime;
long long now_long;
GetSystemTimeAsFileTime(&fileTime);
now_long = (long long(fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
std::cout << "micro time from 1601.1.1:00:00:00 is: " << (now_long / 10) << std::endl;

// 获取当前时间
char date0[64] = {0};
time_t setTime;
time(&setTime);
tm* ptm = localtime(&setTime);
strftime(date0, 64, "%Y-%m-%d %H:%M:%S",ptm); 
std::string TimeStampToDate( time_t nTimestamp )
{
	if (nTimestamp < 0)
	{
		return "";
	}

	char date0[64] = {0};
	time_t tt = nTimestamp;
	struct tm *ttime; 
	ttime = localtime(&tt);
	strftime(date0, 64, "%Y-%m-%d %H:%M:%S",ttime); 

	return date0;
}

time_t DateToTimeStamp( const std::string& strDate )
{
	if (strDate.empty())
	{
		return -1;
	}

	struct tm tt;
	sscanf(strDate.c_str(), "%d-%d-%d %d:%d:%d", 
		&tt.tm_year, 
		&tt.tm_mon, 
		&tt.tm_mday, 
		&tt.tm_hour, 
		&tt.tm_min, 
		&tt.tm_sec);
	tt.tm_year -= 1900;
	tt.tm_mon -= 1;

// 	std::cout << "tt.tm_year:" << tt.tm_year << std::endl;
// 	std::cout << "tt.tm_mon:" << tt.tm_mon << std::endl;
// 	std::cout << "tt.tm_mday:" << tt.tm_mday << std::endl;
// 	std::cout << "tt.tm_hour:" << tt.tm_hour << std::endl;
// 	std::cout << "tt.tm_min:" << tt.tm_min << std::endl;
// 	std::cout << "tt.tm_sec:" << tt.tm_sec << std::endl;
// 	std::cout << "tt.tm_isdst:" << tt.tm_isdst << std::endl;
// 	std::cout << "tt.tm_wday:" << tt.tm_wday << std::endl;

	return mktime(&tt);
}

QDialog去除问号

setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

Std保留小数点后6位

#include <sstream>
#include <iomanip>
std::stringstream ss;
ss << "经度:" << std::setfill('0') << std::fixed << std::setprecision(6) << vPt.x << "°";

获取磁盘空间

QString strDiver;
LPCSTR lpcwstrDriver = "D:/";
ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalF reeBytes;
if (GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes))
{
	//磁盘总空间
	qDebug() << "liTotalBytes=" << liTotalBytes.QuadPart / 1024 / 1024 / 1024 << "G";
	//磁盘剩余空间
	qDebug() << "liTotalFreeBytes=" << liTotalFreeBytes.QuadPart / 1024 / 1024 / 1024 << "G";
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值