概述

有时,需要对控件的某一类事件进行处理,而某一类则忽略。比如对话框需要拦截键盘事件不让其他控件接收等。

Qt当中对于事件的分发和处理可以通过继承并实现event函数实现,但随着组件的增多,这个操作就会变得多且繁琐,并且重写event还得注意一大堆问题。

Qt提供了另一种方法去实现:事件过滤器(EventFilter)。

原型

阅读全文 »

Overview

A small tool based on Qt for implementing named camel case conversion.
The project is built based on Qt5.12.10_msvc2017_64 + VS2022

Version

V 0.0.5

Todo: update code notes

阅读全文 »

首先构建

选择最适合当前编译器版本的GTest。

Linux

官方的构建环境(未测试):gcc 5.0+,CMake

Windows

阅读全文 »

概述

在日常文件读取中,读取中文文件很可能出现乱码。

那么对于中文编码的文件编码识别则至关重要,这个问题在 Qt 中可以很好的解决。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//检查文件编码 0=ANSI 1=UTF-16LE 2=UTF-16BE 3=UTF-8 4=UTF-8BOM
int DataCsv::findCode(const QString &fileName)
{
//假定默认编码utf8
int code = 3;
QFile file(fileName);
if (file.open(QIODevice::ReadOnly))
{
//读取3字节用于判断
QByteArray buffer = file.read(3);
quint8 b1 = buffer.at(0);
quint8 b2 = buffer.at(1);
quint8 b3 = buffer.at(2);
if (b1 == 0xFF && b2 == 0xFE)
{
code = 1;
} else if (b1 == 0xFE && b2 == 0xFF)
{
code = 2;
} else if (b1 == 0xEF && b2 == 0xBB && b3 == 0xBF)
{
code = 4;
} else
{
//尝试用utf8转换,如果可用字符数大于0,则表示是ansi编码
QTextCodec::ConverterState state;
QTextCodec *codec = QTextCodec::codecForName("utf-8");
codec->toUnicode(buffer.constData(), buffer.size(), &state);
if (state.invalidChars > 0) {
code = 0;
}
}

file.close();
}

return code;
}

调用方法

使用QLibrary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <QLibrary>
void LoadLibrary()
{
// 函数类型为void时
typedef void (*MyPrototype)(int, int);
// libPath为动态库路径
QLibrary lib("libPath");
// 函数名,C编译与C++编译的不同
MyPrototype func = lib.resolve("funcName");
if(func)
{
func(2,3);
}
}

注意事项

动态库路径

阅读全文 »

实现定时器的多种方法

  • QTimer类
  • QTimerEvent

QTimer类

QTimer可以通过设定超时时间并调用start函数,以固定频率发送timeout信号。

使用

阅读全文 »