Qt君


  • 首页

  • 关于

  • 归档

  • 搜索

Qt程序提示音接口

发表于 2019-03-29

让系统发出提示音.

1
QApplication::beep();

兼容Qt4/Qt5版本Qml控件TextScroll

发表于 2019-03-27

当文本过长时自动将文字以滚动的形式播放。

TextScroll 备注
导入方法 文件导入
兼容性 QtQuick 1.x与QtQuick 2.x
继承 Item
阅读全文 »

Qt加载字体

发表于 2019-03-26

本文介绍怎么设置系统内置字体和从外部加载字体。

查询系统支持的字体

1
2
3
4
QFontDatabase database;
foreach (const QString &family, database.families()) {
qDebug()<<family;
}

设置全局字体

  • font内容为family查询到的字体名称
    1
    2
    QFont font("family");
    QApplication::setFont(font);

外部加载字体

  • 字体支持ttc或ttf格式加载;
  • 使用addApplicationFont可以为系统路径,也可以资源文件。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    int fontId = QFontDatabase::addApplicationFont("font.ttc");
    QStringList fontIDs = QFontDatabase::applicationFontFamilies(fontId);
    if (! fontIDs.isEmpty()) {
    QFont font(fontIDs.first());
    QApplication::setFont(font);
    }
    else {
    qDebug()<<"Failed to load font.";
    }

Qt绘制同心圆示例

发表于 2019-03-25

利用QPainter与QPainterPath绘制同心圆。

同心圆.png

  • 使用addEllipse绘制外圆与内圆;
  • setRenderHint(QPainter::Antialiasing)抗锯齿渲染。
1
2
3
4
5
6
7
QPainter painter(this);
QPainterPath path;
path.addEllipse(0, 0, 200, 200);
path.addEllipse(50, 50, 100, 100);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush( QBrush(QColor("lightblue")) );
painter.drawPath(path);

C/C++黑魔法-常量字符串连接

发表于 2019-03-24

使用方便的字符串常量连接。

使用宏连接字符串常量

1
2
#define PREFIX_PATH "/root/path"
int fd = open(PREFIX_PATH"/file", O_RDONLY);

常规做法

1
2
char buffer[256];
snprintf(buffer, 256, "%s/file", "/root/path");

对比

  • 更快的执行速度;
  • 更清晰;
  • 使用更少的堆栈空间.

QtCreator-启用/关闭FakeVim模式

发表于 2019-03-23

使用QtCreator编辑代码时,突然不知道按了什么按键,输入方式变了,ctrl+c和ctrl+v都用不了,原因是开启了FakeVim模式。

FakeVim模式

  • 在FakeVim模式下,您可以以类似于Vim编辑器的方式运行主编辑器。

启用/关闭FakeVim模式

  • 按两次alt+v
  • 工具 > 选项 > FakeVim > 使用FakeVim
  • 或Tools > Options > FakeVim > Use FakeVim
    示例.png

C/C++黑魔法-另类switch

发表于 2019-03-23

介绍duff写法与范围判断

duff写法

1
2
3
4
5
6
7
8
9
int n = 1;
switch (n) {
case 0: do { printf("%d ", 0);
case 1: printf("%d ", 1);
case 2: printf("%d ", 2);
case 3: printf("%d ", 3);
case 4: printf("%d ", 4);
} while (n-- > 0);
}
  • 用于循环优化,提高性能。但现代编译器已经支持得很好了。
  • 输出
    1
    1 2 3 4 0 1 2 3 4

范围判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int i = 5;
switch (i) {
case 0 ... 3:
std::cout<<"0-3: "<<i<<std::endl;
break;
case 4 ... 6:
std::cout<<"4-6: "<<i<<std::endl;
break;
case 7 ... 9:
std::cout<<"7-9: "<<i<<std::endl;
break;
case 10:
std::cout<<"10"<<i<<std::endl;
default:
break;
}
  • 注意: gnu编译器,mingw编译器支持
  • 输出
    1
    4-6: 5

解决一个运行时错误的问题

发表于 2019-03-22

解决由于程序删除内存方式写得不恰当导致的运行时错误。

  • 使用调试运行程序时弹出断言错误,以下是调试信息:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 01B20000, 2F6AE020 )
    Debug Assertion Failed!

    File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
    Expression: _CrtIsValidHeapPointer(block)

    For information on how your program can cause an assertion
    failure, see the Visual C++ documentation on asserts.

    (Press Retry to debug the application)
  • 看到关键部分HEAP[xxx.exe]和_CrtIsValidHeapPointer,估计是堆内存出错,程序操作堆内存,一般是使用了malloc,free,new,delete等操作;

  • 考虑到程序使用了FFmpeg删除视频资源弹出的错误,就联想到可能是删除操作不正确;
  • 最后查明原因为av_malloc申请内存却用delete删除,修正为av_free删除即可。

C语言骚操作之没有加法运算符

发表于 2019-03-21

利用数学运算符实现不使用加法运算符实现加法的操作。

1
2
3
4
int add(int x, int y)
{
return y == 0 ? x : add( x ^ y, (x & y) << 1);
}

C++接口隔离示例(设计模式)

发表于 2019-03-20

本文介绍C++设计模式中的接口隔离示例。使用接口隔离独立性好,且只限于它的接口(单一性原则)。

相同的返回却不同的操作

  • Device类继承于AudioDevice与VideoDevice;
  • 在Device中:

    1
    2
    AudioDevice *audioDevice() { return this; } // 接口隔离
    VideoDevice *videoDevice() { return this; } // 接口隔离
  • 由于都返回this指针,但实际只能访问到对应的返回值,这就是接口隔离的核心所在。

使用基本套路

  • 单一原则的继承;
  • 接口的返回。

示例

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
#include <iostream>

using namespace std;

class AudioDevice {
public:
AudioDevice() {}
void open() { cout<<"Open Audio Device."<<endl; }
void close() { cout<<"Close Audio Device."<<endl; }
};

class VideoDevice {
public:
VideoDevice() {}
void open() { cout<<"Open Video Device."<<endl; }
void close() { cout<<"Close Video Device."<<endl; }
};

class Device : public AudioDevice, public VideoDevice {
public:
Audio() {}

AudioDevice *audioDevice() { return this; } // 接口隔离
VideoDevice *videoDevice() { return this; } // 接口隔离
};

int main(int argc, char *argv[])
{
Device device;
device.audioDevice()->open();
device.audioDevice()->close();

device.videoDevice()->open();
device.videoDevice()->close();
return 0;
}

关于更新

  • 文章首发于微信公众号你才小学生(nicaixiaoxuesheng)
  • 后续更新于Qtbig哥(qtbig.com)
1…242526…32
Qt君

Qt君

313 日志
41 标签
© 2019 Qt君
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4
粤ICP备 - 16070052号