一、本文目的
记录在学习或工作过程中遇到的,可能会重复查询的代码、问题。
尽可能用一句话或者极短的代码解决。
不定期记录,随时更新。
以下代码均已通过测试有效。
二、功能代码
1.Qt连接按钮Button:
// ABBReadBtn:部件名 ABB_GluingUI:工程类名 ABB_read:所连函数名
connect(ui.ABBReadBtn, &QPushButton::clicked, this, &ABB_GluingUI::ABB_read);
2.Qt文本传递到前端Label:
ui.text1_label->setText("Text");
ui.num1_label->setNum(3.14);
// 显示Float数据的4位有效数字
ui.jointData1->setText(QString::number(j1, 'f', 4));
3.Qt日志打印textBrowser:
#include <QDateTime>
// textBrowser_log:日志文本框名
void slot_outPutLog(string str)
{
QDateTime currtTime = QDateTime::currentDateTime();
QString timeStr = currtTime.toString("yyyy.MM.dd HH:mm:ss: ");
timeStr += QString::fromLocal8Bit(str.c_str());
ui.textBrowser_log->setTextColor(Qt::black);
ui.textBrowser_log->append(timeStr);
}
slot_outPutLog("日志记录成功!");
4.Qt传递参数到后端QLineEdit:
// setUserCoor1:部件名
res1 = ui.setUserCoor1->text().toFloat();
res2 = ui.setUserCoor2->text().toInt();
res3 = ui.setUserCoor3->text().toStdString();
5.设置源文件中文本格式为UTF-8:
#pragma execution_character_set("utf-8")
6.读取txt文件中数字并存为double:
std::string settingPath = ".\\setting\\setting.txt";
std::ifstream inFile(settingPath);
// 检查文件是否成功打开
if (inFile.is_open())
{
// 逐行读取文件内容
std::string line;
while (std::getline(inFile, line))
{
res = std::stod(line);
}
// 关闭文件
inFile.close();
}
7.Qt启动exe可执行文件:
void runExe()
{
QProcess process;
// exe可执行文件所在地址
QString program = "./convert/conv.exe";
bool started = QProcess::startDetached(program);
if (started)
{
std::cout << "Process started successfully." << std::endl;
}
}
8.Qt每3秒触发一次槽函数:
#include <QTimer>
QTimer *timer_calendar;
// 在控件初始化的位置连接计时器
timer_calendar = new QTimer(this);
connect(timer_calendar, &QTimer::timeout, this, &ABB_GluingUI::ABB_read);
// 在需要启动槽函数的位置启动计时器
timer_calendar->start(3000);//每隔三秒调用一次槽函数
// 在需要停止槽函数的位置停止计时器
timer_calendar->stop();
9.string与wstring类型互转:
#include <locale>
#include <codecvt>
string str1 = "text!";
wstring_convert<codecvt_utf8<wchar_t>> converter;
// string转wstring
wstring str2 = converter.from_bytes(str1);
// wstring转string
string str3= converter.to_bytes(text);
10.string与UA_string类型互转:
// string转UA_string
char* p = const_cast<char*>(str.c_str());
UA_String myString = UA_STRING(p);
// UA_string转char
char* res_char = (char*)UA_malloc(sizeof(char)*myString.length + 1);
memcpy(res_char, myString.data, myString.length);
res_char[res.length] = '\0';
// char转string格式
string str = res_char;
11.Qt设置背景图片到Widget:
// 自动拉伸图片
ui.widget->setStyleSheet("border-image: url(image/background.PNG);");
12. Qt设置按键组分支radiobutton:
QButtonGroup *pbuttonGroup;
pbuttonGroup = new QButtonGroup(this);
// 控件名为radioButton_1、radioButton_3、radioButton_3
pbuttonGroup->addButton(ui.radioButton_1, 0);
pbuttonGroup->addButton(ui.radioButton_2, 1);
pbuttonGroup->addButton(ui.radioButton_3, 2);
connect(ui.radioButton_1, &QPushButton::clicked, this, &ABB_GluingUI::onRadioClickSlot);
connect(ui.radioButton_2, &QPushButton::clicked, this, &ABB_GluingUI::onRadioClickSlot);
connect(ui.radioButton_3, &QPushButton::clicked, this, &ABB_GluingUI::onRadioClickSlot);
// 不同控件触发的不同分支
void ABB_GluingUI::onRadioClickSlot()
{
switch (pbuttonGroup->checkedId())
{
case 0:
cout << "选择1成功" << endl;
break;
case 1:
cout << "选择2成功" << endl;
break;
case 2:
cout << "选择3成功" << endl;
break;
}
}
13. Qt设置列表组分支:
void CodeManage::init()
{
// 添加列表内容
ui.DirectoryBox->addItem("ParaFile_1");
ui.DirectoryBox->addItem("ParaFile_2");
// 连接列表
connect(ui.DirectoryBox, QOverload<int>::of(&QComboBox::activated), this, &CodeManage::comboBoxIndexActivated);
}
void CodeManage::comboBoxIndexActivated(int index)
{
QString DirectorySet;
string ParaFilePath;
switch (index)
{
case 0:
// 选择一
DirectorySet = "ParaFile_1";
ui.DirectoryLine->setText(DirectorySet);
cout << "DirectorySet = " << ParaFilePath << endl;
break;
case 1:
// 选择二
DirectorySet = "ParaFile_1";
ui.DirectoryLine->setText(DirectorySet);
cout << "DirectorySet = " << ParaFilePath << endl;
break;
default:
break;
}
}
14. 删除某文件夹内所有文件(保留该文件夹本身):
void QtChart::RemoveAllFiles(wstring wstrDir)
{
if (wstrDir.empty())
{
return;
}
HANDLE hFind;
WIN32_FIND_DATA findData;
wstring wstrTempDir = wstrDir + (L"\\*");;
hFind = FindFirstFile(wstrTempDir.c_str(), &findData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
do
{
// 忽略"."和".."两个结果
if (wcscmp(findData.cFileName, L".") == 0 || wcscmp(findData.cFileName, L"..") == 0)
{
continue;
}
wstring wstrFileName;
wstrFileName.assign(wstrDir);
wstrFileName.append(L"\\");
wstrFileName.append(findData.cFileName);
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)// 是否是目录
{
RemoveAllFiles(wstrFileName.c_str());
}
else
{
DeleteFile(wstrFileName.c_str());
}
} while (FindNextFile(hFind, &findData));
FindClose(hFind);
//若要一并删除本文件夹,则恢复如下代码
//RemoveDirectory(wstrDir.c_str());
}