Qt小笔记

Qt小笔记

VS2022的注册码

  • Pro(专业版): TD244-P4NB7-YQ6XK-Y8MMM-YWV2J
  • Enterprise(企业版):  VHF9H-NXBBB-638P6-6JHCY-88JWH

Qt打包exe

  • 1.在Qt应用程序中找到Qt 5.14.2(MSVC 2017 64-bit)命令窗口快捷图标,点击打开黑色命令窗口。
  • 2.在命令窗口中进入要打包的exe文件夹路径,比如D:\Debug>
  • 3.执行命令D:\Debug>WinDeployQt Main.exe,完成打包

自定义的消息弹框

1
2
3
4
5
6
7
8
9
10
class CustomMessageBox : public QMessageBox {
public:
CustomMessageBox(QWidget* parent = nullptr) : QMessageBox(parent) {
setWindowTitle(QStringLiteral("提示"));
setStyleSheet("QPushButton { background-color: blue; color: white;font-family: 微软雅黑 Light; font-size: 18pt; font-weight: bold; width: 100px; height: 50px; border-radius: 10px; }"
"QPushButton:hover { background-color: darkblue; color: white;font-family: 微软雅黑 Light; font-size: 18pt; font-weight: bold;width: 100px; height: 50px; border-radius: 10px; }"
"QLabel { font-family: 微软雅黑 Light; font-size: 18pt; font-weight: bold; color: red; }"
"QMessageBox { background-color: yellow; }");
}
};

消息弹框提示

1
2
3
4
5
CustomMessageBox msgBox;
msgBox.setText(QStringLiteral("切换了视觉类型请重启软件!"));
msgBox.setFont(QFont("微软雅黑Light", 16)); // 设置字体为微软雅黑Light,大小为16
msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint);
msgBox.exec();

按钮的同步消息

  • 一键上使能
    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
     ui.btnServerOn->setFont(font);
    ui.btnServerOn->setStyleSheet("background-color:#F0F0F0;");
    connect(ui.btnServerOn, &QPushButton::clicked, this, [=]() {
    CustomMessageBox msgBox;
    msgBox.setText(QStringLiteral("一键上使能?"));
    msgBox.addButton(QStringLiteral("上使能"), QMessageBox::YesRole);
    msgBox.addButton(QStringLiteral("取消"), QMessageBox::NoRole);
    msgBox.setFont(QFont(WRYH, 16)); // 设置字体为微软雅黑 Light,大小为16
    msgBox.setWindowFlags(msgBox.windowFlags() | Qt::WindowStaysOnTopHint);
    int iValue = msgBox.exec();
    if (0 == iValue)
    {
    ui.btnServerOn->setStyleSheet("background-color:#FF0000;");
    Sleep(50);
    ui.btnServerOn->repaint();
    Sleep(50);
    ui.btnServerOn->setEnabled(false);
    for (int i = 0; i < s_pCardAdmin->mapCardInfo.size(); i++)
    {
    for (int j = 0; j < s_pCardAdmin->mapCardInfo[i]->cardAxisNum; j++)
    {
    if (MT_SEVER == s_pCardAdmin->mapCardInfo[i]->mapAxisInfo[j]->motoType)
    {
    s_pCardAdmin->AxisSetDO(i, j, 0, 0); //上使能
    //s_pCardAdmin->AxisSetDO(i, j, 0, 1); //断使能
    }
    }
    }
    ui.btnServerOn->setStyleSheet("background-color:#F0F0F0;");
    ui.btnServerOn->setEnabled(true);
    }
    });

遍历界面控件,设置字体

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
QFont globalFont("微软雅黑 Light", 16);
QList<QLineEdit*> allChildren1 = this->findChildren<QLineEdit*>();
QList<QLabel*> allChildren2 = this->findChildren<QLabel*>();
QList<QGroupBox*> allChildren3 = this->findChildren<QGroupBox*>();
QList<QRadioButton*> allChildren4 = this->findChildren<QRadioButton*>();
QList<QCheckBox*> allChildren5 = this->findChildren<QCheckBox*>();
QList<QPushButton*> allChildren6 = this->findChildren<QPushButton*>();
for (QLineEdit* child : allChildren1) {
child->setFont(globalFont);
child->setStyleSheet(QLINEEDIT_BK_COLOR);
}
for (QLabel* child : allChildren2) {
child->setFont(globalFont);
}
for (QGroupBox* child : allChildren3) {
child->setFont(globalFont);
}
for (QRadioButton* child : allChildren4) {
child->setFont(globalFont);
}

for (QCheckBox* child : allChildren5) {
child->setFont(globalFont);
}

for (QPushButton* child : allChildren6) {
child->setFont(globalFont);
}

// 设置背景透明,数字颜色为蓝色

1
2
3
4
5
6
7
8
9
10
11
12
QList<QLCDNumber*> allChildren7 = ui.groupBoxAOI->findChildren<QLCDNumber*>();
for (QLCDNumber* child : allChildren7) {
child->setSegmentStyle(QLCDNumber::Flat);
}
ui.groupBoxAOI->setStyleSheet("QWidget > QLCDNumber { border: 1px solid #0061A8; background: transparent; color: #00A2DE; font-weight: bold; padding: 3px; }");

## 查找特定命名规则的控件
for (int i = 0; i < TABLE_NO_BOTTOM; i++)
{
ui.groupBoxAOI->findChild<QLineEdit*>(QString("lineEditTable%1AoiX").arg(i + 1))->setText(QString::number(m_pPara->m_dAoiX[m_iCameraNo][i][WORK_ONE][iBtnNo - AOI_RADIO1], 'f', 2));
ui.groupBoxAOI->findChild<QLineEdit*>(QString("lineEditTable%1AoiY").arg(i + 1))->setText(QString::number(m_pPara->m_dAoiY[m_iCameraNo][i][WORK_ONE][iBtnNo - AOI_RADIO1], 'f', 2));
}