独钓寒江雪

用C++的优雅,驯服Windows的狂野

默认构造函数

什么是默认构造函数?

我们一般会认为默认构造函数就是编译器自动生成的那个构造函数,其实这种理解不全面。
**准确的说,默认构造函数就是在调用时不需要显示地传入实参的构造函数。**根据这个原则,下面 2 种构造函数都是默认构造函数:

1
2
3
4
5
6
7
class Sample {
public:
// 默认构造函数。
Sample() {
// do something
}
};
1
2
3
4
5
6
7
class Sample {
public:
// 默认构造函数。虽然有形参,但有默认值,调用时可以不显式地传入实参。
Sample(int m = 10) {
// do something
}
};
阅读全文 »

条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符。常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成的平行线图案。

阅读全文 »

本文围绕 3 个问题来理解 C++的默认构造函数:

  1. 什么是默认构造函数?
  2. 默认构造函数什么时候被调用?
  3. 编译器在什么情况下会生成默认构造函数?
阅读全文 »

本文从实际需求出发,介绍了内存池的实现原理,并且提供了具体的实现方案。

为什么需要使用内存池

在 C/C++ 中我们通常使用 mallocfreenewdelete 来动态分配内存。
一方面,因为这些函数涉及到了系统调用,所以频繁的调用必然会导致程序性能的损耗;

另一方面,频繁的分配和释放小块内存会导致大量的内存碎片的产生,当碎片积累到一定的量之后,将无法分配到连续的内存空间,系统不得不进行碎片整理来满足分配到连续的空间,这样不仅会导致系统性能损耗,而且会导致程序对内存的利用率低下。

当然,如果我们的程序不需要频繁的分配和释放小块内存,那就没有使用内存池的必要,直接使用malloc,freenew,delete函数即可。

阅读全文 »

路径相关

  • 当前程序可执行文件的据对路径
    QCoreApplication::applicationFilePath()
  • 当前程序可执行文件所在的目录
    QCoreApplication::applicationDirPath()
  • 当前进程的工作目录
    QDir::currentPath(),在调用 CreateProcess 函数时通过 lpCurrentDirectory 参数指定进程工作目录
  • 通过文件路径获取文件相关信息
    1
    2
    3
    4
    QFileInfo fi("D:/1/2/3 4.txt");
    qDebug() << fi.dir().path(); // D:/1/2
    qDebug() << fi.fileName(); // 3 4.txt
    qDebug() << fi.suffix(); // txt

<原文出自: jiangxueqiao.com,请尊重原创>

QEnum 转字符串

1
2
3
4
template <typename QEnum>
inline const char* QEnumToStr(const QEnum value) {
return QMetaEnum::fromType<QEnum>().valueToKey(value);
}

移除 Qt 控件虚线框

方式一:使用 StyleSheet

1
2
3
QWidget:focus {
outline: none; /* 去掉得到焦点时的虚线框 */
}

方式二:继承 QProxyStyle

继承 QProxyStyle,PrimitiveElement 为 QStyle::PE_FrameFocusRect 时不绘制虚线框,然后在 main() 函数里调用 QApplication::setStyle() 使用新的样式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 文件名: NoFocusRectStyle.h
#ifndef NOFOCUSRECTSTYLE_H
#define NOFOCUSRECTSTYLE_H
#include <QProxyStyle>
class NoFocusRectStyle : public QProxyStyle
{
public:
NoFocusRectStyle(QStyle *baseStyle) : QProxyStyle(baseStyle) {}
void drawPrimitive(PrimitiveElement element,
const QStyleOption *option,
QPainter *painter,
const QWidget *widget = 0) const
{
if (element == QStyle::PE_FrameFocusRect)
{
return;
}
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
};
#endif // NOFOCUSRECTSTYLE_H
1
2
3
4
5
6
7
8
9
10
11
12
13
// 文件名: main.cpp
#include "Widget.h"
#include "NoFocusRectStyle.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
NoFocusRectStyle *style = new NoFocusRectStyle(app.style());
app.setStyle(style); // Ownership of the style object is transferred to QApplication
Widget w;
w.show();
return app.exec();
}

无法拖入文件到 QListWidget

一般而言,只需要做如下操作,QWidget 即可支持拖入文件:

1
2
3
4
5
6
7
8
setDragDropMode(QAbstractItemView::DropOnly); // 仅支持拖入文件


// 重写dragEnterEvent,当拖入文件进入时被调用,可以在该函数中取消拖入操作
void dragEnterEvent(QDragEnterEvent* e) override;

// 重写dropEvent,当拖入文件并释放鼠标时被调用
void dropEvent(QDropEvent* e) override;

但在执行上述操作后,我们拖入文件到 QListWidget 时,却只能收到 dragEnterEvent 事件,却无法收到 dropEvent 事件。

阅读全文 »

Windbg 是 Microsoft 公司推出的免费的、带 GUI 的调试器,支持 Source 和 Assembly 两种模式的调试。

Windbg 不仅可以调试应用程序,还可以进行系统内核调试,Windbg 支持的平台包括 X86、IA64、AMD64。

阅读全文 »
0%