Qt - Windodw识别设备插拔事件

Winodws系统消息结构#include <qt_windows.h>

1
2
3
4
5
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_DEVICECHANGE
WPARAM wParam, // device-change event
LPARAM lParam ); // event-specific data

设备插入/拔出:#include <Dbt.h> msg->wParam

Value Meaning
DBT_DEVICEARRIVAL A device or piece of media has been inserted and is now available.
DBT_DEVICEREMOVECOMPLETE A device or piece of media has been removed.

设备接入类型:msg->lParam 强制转换

Value Meaning
DBT_DEVTYP_DEVICEINTERFACE
0x00000005
Class of devices. This structure is a DEV_BROADCAST_DEVICEINTERFACE structure.
DBT_DEVTYP_HANDLE
0x00000006
File system handle. This structure is a DEV_BROADCAST_HANDLE structure.
DBT_DEVTYP_OEM
0x00000000
OEM- or IHV-defined device type. This structure is a DEV_BROADCAST_OEM structure.
DBT_DEVTYP_PORT
0x00000003
Port device (serial or parallel). This structure is a DEV_BROADCAST_PORT structure.
DBT_DEVTYP_VOLUME
0x00000002
Logical volume. This structure is a DEV_BROADCAST_VOLUME structure.

CyAPI:官方lib使用MinGW编译,无法使用MSVC编译调用。

QString

https://doc.qt.io/qt-5/qstring.html

QString hex to unsigned char

Hexadecimal QString representation to Unsigned char array

1
2
3
4
5
unsigned char myBuffer[2];
Qstring hexStr = "0x1234";
bool ok = false;
myBuffer[0] = (unsigned char)hexstr.midRef(2,2).toUShort(&ok, 16)
myBuffer[1] = (unsigned char)hexstr.midRef(4,2).toUShort(&ok, 16)

QString 基本操作

在Qt5中,right()/left()/mid()是深拷贝,返回一个字符串中的n个字符的字符串。rightRef()/leftRef()/midRef()则是返回对其的引用。

  • n > str.length : return null string / reference
  • n < str.length : starting at the given position
  • n = -1(default): return all string / reference
1
2
3
4
5
6
QString str = "hello-world!";
int idx = str.lastIndexOf("-");
str.replace("h", "H");
str.right(idx-1);
str.left(idx);
str.mid(idx, 5); //mid(star_pos, size)

QCharts

改变画笔颜色/宽度

How to change QLineSeries width?

  • 单独指定画笔颜色+宽度
1
2
3
4
QPen pen = series->pen();
pen.setWidth(1);
pen.setBrush(QBrush("red")); // or just pen.setColor("red");
series->setPen(pen);
  • 继承系统默认颜色,在chart->addSeries(series);之后设置画笔
    1
    2
    3
    4
    5
    6
    QChart *chart = new QChart();
    chart->addSeries(series); // addSeries must be called first

    QPen pen = series->pen();
    pen.setWidth(1);
    series->setPen(pen);

QSpinBox

按位数补齐0:重载QSpinBox - textFromValue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PADDINGZEROSPINBOX_H
#define PADDINGZEROSPINBOX_H
#include <QSpinBox>

class Padding4ZeroSpinBox: public QSpinBox
{
Q_OBJECT
public:
Padding4ZeroSpinBox();
explicit Padding4ZeroSpinBox(QWidget *parent = nullptr){};

protected:
virtual QString textFromValue(int val) const{
//set padding str '0' and size 4
QString val_str = QStringLiteral("%1").arg(val, 4, 16, QChar('0'));
return val_str.left(4);
}
};
#endif // PADDINGZEROSPINBOX_H

设置前缀/后缀/当前值

1
2
ui->my_spinbox->setPrefix("0x");
ui->my_spinbox->setValue("0xFF");

QTabWidget

改变背景色

How to set only QTabWidget background color stylesheet

QtDesigner -> TabWidget ->styleSheet属性中设置:在样式表中用’#’指定对象,子对象将不会继承样式表

1
#objectName {background-color: rgb(240,240,240);}

Qt打包程序

打包程序的一些已知问题:https://cloud.tencent.com/developer/article/1516768

无法定位程序输入点?xxx@Qxxx@@xxxx@@@xxxxx于动态链接库xx.exe上

调整系统环境变量该编译环境的环境变量顺序靠前即可。

eg.Qt有MinGW+MSVC2015的两个环境编辑:如果用MSVC2015编译打包的环境,则将MSVC2015的环境变量的顺序置于MinGW之前(必要的时候,越靠前越好)

Qt进程与线程

Error

error C2001: newline in constant(Qt中文编码问题)

待补充

Warnings

narrowing conversion from unsigned int to int