WIN的异常情况SetUnhandledExceptionFilter

1
2
3
4
5
6
7
8
#include <windows.h>
LONG WINAPI callback_system(EXCEPTION_POINTERS* ExceptionInfo) {
std::cerr << "Unhandled exception occurred!" << std::endl;
std::cerr << "Exception code: " << std::hex << ExceptionInfo->ExceptionRecord->ExceptionCode << std::endl;

return EXCEPTION_EXECUTE_HANDLER;
}
SetUnhandledExceptionFilter(callback_system);

qt中的qlistView列表

1
实现整个列表的数据

qt中的qtableView列表

1
2
3
4
5
   tblvModel->setColumnCount(5);
tblvModel->setHeaderData(0, Qt::Horizontal, tr("serial number"));
tblvModel->setHeaderData(1, Qt::Horizontal, tr("program id"));
tblvModel->setHeaderData(2, Qt::Horizontal, tr("program name"));
ui->tblvProgram->setModel(tblvModel);

QColumnView

1

QTableWidget列表

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
39
 // 将按钮控件放入表格的第三列
tableWidget->setCellWidget(row, 2, button);


// 设置表格显示模式为扁平模式(不可编辑),隐藏行头,让每行合并
tableWidget->setSelectionMode(QAbstractItemView::NoSelection);
tableWidget->setShowGrid(false);
tableWidget->verticalHeader()->hide();
//写入Qstring
QTableWidgetItem *item = new QTableWidgetItem(QString("Row %1, Column %2").arg(row + 1).arg(col + 1));
// 在每个单元格中添加数据并设置为不可编辑
for (int row = 0; row < tableWidget->rowCount(); ++row) {
for (int col = 0; col < tableWidget->columnCount(); ++col) {
QTableWidgetItem *item = new QTableWidgetItem(QString("Item %1-%2").arg(row + 1).arg(col + 1));
item->setFlags(item->flags() & ~Qt::ItemIsEditable); // 去除可编辑标志
tableWidget->setItem(row, col, item);
}
}
//表格变为禁止编辑
setEditTriggers(QAbstractItemView::NoEditTriggers);
//设置表格为整行选择
将表格视图的选择模式设置为单选模式 (SingleSelection):
这样设置后,用户只能选择表格视图中的一行,点击其他行时将取消之前选中的行。
ui->tblwProgram->setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows)
//设置单元格背景颜色
columnHeaderItem0->setBackgroundColor(QColor(0,60,10));
//
创建一个QTableWidget:
创建一个QTableWidget对象,设置行数和列数。
使用setHorizontalHeaderLabels方法设置表头标签。
隐藏网格线:
使用setShowGrid(false)方法隐藏表格的网格线。
设置表头可见:
使用horizontalHeader()->setVisible(true)和verticalHeader()->setVisible(true)方法确保表头可见(默认情况下表头是可见的,所以这步实际上是可选的)。
//清空所有行
使用 clearContents() 方法清空所有行内容,但保留表头。
或者使用 setRowCount(0) 方法清空所有行,包括内容。有行数

QtabelWidget

1
2
3
4
5
6
7
8
/*QWidget *tab1 = new QWidget();
QVBoxLayout *layout1 = new QVBoxLayout();
QLabel *label1 = new QLabel("This is the content of Tab 1");
layout1->addWidget(label1);
tab1->setLayout(layout1);

// 创建第二个选项卡
QWidget *tab2 = new QWidg

QMenuBar设置

1
2
3
4
5
6
7
8
位置大小设置:menuBar->setFixedSize(800, 50);  // 设置固定大小
QMenuBar *menuBar = new QMenuBar(this);
menuBar->setFixedSize(52, 100);
QMenu *startMenu = menuBar->addMenu(QObject::tr("start interface"));
QAction *settingAction = new QAction(QObject::tr("parameter settings"));
QAction *quitAction = new QAction(QObject::tr("program quit"));
startMenu->addAction(settingAction);
startMenu->addAction(quitAction);

QLineEdit

1
lineEdit->setText("Hello, world!");

QFileDialog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//获取所有文件
getOpenFileName(this, tr("Open File"), "", tr("All Files (*);;Text Files (*.txt)"));
QString filePath = QFileDialog::getOpenFileName(
this, // 父窗口
tr("Open File"), // 对话框标题
"", // 初始目录
tr("All Files (*);;Text Files (*.txt)"), // 文件过滤器
nullptr, // 选定的过滤器
QFileDialog::DontUseNativeDialog // 对话框选项
);
tr("Open File") 是对话框的标题,可以翻译。
tr("All Files (*);;Text Files (*.txt)") 是文件过滤器,可以翻译。这个过滤器有两个部分:
All Files (*):显示所有文件。
Text Files (*.txt):仅显示扩展名为 .txt 的文件。

//获取文件名称
if (!filePath.isEmpty()) {
QFileInfo fileInfo(filePath);
QString fileName = fileInfo.fileName();
qDebug() << "Selected file name:" << fileName;
}

QDialog

1
2
3
退出:  reject();accept();
accept():当用户点击“OK”按钮时,调用此方法以关闭对话框并返回 QDialog::Accepted。
reject():当用户点击“Cancel”按钮时,调用此方法以关闭对话框并返回 QDialog::Rejected。

QVector使用

1

QPushButton

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
// 只设置按钮的图标
QIcon icon(":/path/to/your/image.png");
button.setIcon(icon);
button.setText("");
// 设置按钮的样式表为图片的形状
button.setStyleSheet("QPushButton { "
" border-image: url(:/path/to/your/image.png); " // 设置按钮的背景图像
" background-color: transparent; " // 设置背景颜色为透明
" border: none; " // 设置按钮的边框为无
"}");
//
button.setStyleSheet("QPushButton { "
" background-color: red; " // 设置按钮的背景颜色为红色
" border: none; " // 设置按钮的边框为无
"}"
"QPushButton::indicator { "
" width: 20px; " // 设置指示器的宽度
" height: 20px; " // 设置指示器的高度
" border: 2px solid white; " // 设置指示器的边框为白色
" border-radius: 10px; " // 设置指示器的边框半径为10px,使其呈圆形
"}"
"QPushButton::indicator:checked { "
" background-color: white; " // 设置指示器被选中时的背景颜色为白色
" image: url(); " // 清除指示器的图像
" border: none; " // 设置指示器的边框为无
" border-radius: 0px; " // 设置指示器的边框半径为0px
"}");
//
int row = button->property("row").toInt();

QString

1

QMessgBox

1
2
3
4
5
//选择yes,删除
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Confirmation", "Are you sure you want to delete this row?",
QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {

QcheckBox

1
2
//设置拒绝点击
setEnabled(false);

Qcombox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 初始化数据
data["Fruit"] = QStringList() << "Apple" << "Banana" << "Orange";
data["Vegetable"] = QStringList() << "Carrot" << "Broccoli" << "Spinach";
data["Animal"] = QStringList() << "Dog" << "Cat" << "Elephant";

// 创建和配置第一个组合框(一级类别)
label1 = new QLabel("Select Category:", this);
layout->addWidget(label1);

combobox1 = new QComboBox(this);
combobox1->addItems(data.keys());
layout->addWidget(combobox1);
connect(combobox1, SIGNAL(currentIndexChanged(int)), this, SLOT(on_combobox1_changed(int)));

// 创建和配置第二个组合框(二级类别)
label2 = new QLabel("Select Subcategory:", this);
layout->addWidget(label2);

combobox2 = new QComboBox(this);
layout->addWidget(combobox2);

setCentralWidget(centralWidget);
//QOverload 是一个模板函数,用于解决重载函数的问题。在 Qt 中,一些信号可能会有多个重载版本,这时候如果直接使用 &QComboBox::currentIndexChanged,编译器无法确定具体使用哪个版本,因为它们具有相同的函数名但不同的参数列表。
QOverload<int>::of(&QComboBox::currentIndexChanged)

QProcess

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
函数 isFileOpen():

构建 lsof 命令,并将文件路径作为参数传递。
使用 QProcess 执行命令,并等待命令完成。
读取命令的标准输出,判断输出中是否包含文件路径来确定文件是否正在被使用。

// 构建 lsof 命令
QString command = "lsof " + filePath;

// 创建 QProcess 对象并执行命令
QProcess process;
process.start(command);
process.waitForFinished();

// 读取命令输出
QString output = QString(process.readAllStandardOutput());

// 判断输出中是否包含文件路径,表示文件正在被使用
if (output.contains(filePath))
{
qDebug() << "File" << filePath << "is open by some process.";
return true;
}
else
{
qDeMoubug() << "File" << filePath << "is not open by any process.";
return false;
}
//启动和删除进程
MainWindow::~MainWindow()
{
if (process->state() != QProcess::NotRunning) {
process->kill();
}
}

void MainWindow::startProcess()
{
// 启动一个示例进程,例如 gedit
在 startProcess 槽函数中,使用 process->start("gedit") 启动 gedit 文本编辑器进程。
使用 waitForStarted() 方法等待进程启动,并检查是否启动成功。
打印进程的PID。
process->start("gedit");
if (!process->waitForStarted()) {
qDebug() << "Failed to start process.";
} else {
qDebug() << "Process started with PID:" << process->processId();
}
}

void MainWindow::endProcess()
{
if (process->state() != QProcess::NotRunning) {
qint64 pid = process->processId();
// 使用 kill 命令结束进程
QProcess::execute("kill", QStringList() << QString::number(pid));
process->waitForFinished();
qDebug() << "Process ended.";
} else {
qDebug() << "No running process to end.";
}
}

Modbus/TCPIP协议

1