首页
统计
关于
Search
1
C语言:获取程序运行消耗的时间(gettimeofday)
564 阅读
2
QT-利用Qcamera查看USB摄像头参数(数据帧格式+分辨率)
388 阅读
3
嵌入式linux组播接收发送失败解决
371 阅读
4
CMAKE报告:COULD NOT FIND PKGCONFIG (MISSING: PKG_CONFIG_EXECUTABLE)
355 阅读
5
Linux 查看硬盘通电时间
298 阅读
编程语言
C/C++
PHP
Go
分享
随笔
Linux
OpenHarmony
登录
Search
标签搜索
C++
QT
Linux
Git
Go
C
程序执行时间
函数执行时间
GDAL
zeromq
github
Centos
代理
goKit
gitea
247.1
累计撰写
34
篇文章
累计收到
0
条评论
首页
栏目
编程语言
C/C++
PHP
Go
分享
随笔
Linux
OpenHarmony
页面
统计
关于
搜索到
10
篇与
的结果
2025-07-17
银河麒麟上QT应用自动创建桌面快捷方式及增加自启动
银河麒麟使用的桌面为MATE桌面。#include <QCoreApplication> #include <QFile> #include <QTextStream> #include <QDir> #include <QMessageBox> #include <QStandardPaths> #include <QFileInfo> #include <QProcess> bool shortcutExists(const QString& appName) { // 获取桌面路径 QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); if (desktopPath.isEmpty()) { qDebug() << "无法获取桌面路径"; return false; } // 构建.desktop文件路径 QString desktopFileName = appName + ".desktop"; QString desktopFilePath = desktopPath + QDir::separator() + desktopFileName; // 检查文件是否存在 return QFile::exists(desktopFilePath); } bool isAutostartEnabled(const QString& appName) { // 获取自动启动目录 QString autostartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart/"; QString autostartFilePath = autostartDir + appName + ".desktop"; // 检查自动启动文件是否存在 return QFile::exists(autostartFilePath); } QString findApplicationIcon(const QString& appName, const QString& execPath) { // 获取可执行文件所在目录 QFileInfo execFileInfo(execPath); QString execDir = execFileInfo.absolutePath(); // 检查目录下是否存在logo.png QString logoPath = execDir + QDir::separator() + "logo.png"; if (QFile::exists(logoPath)) { qDebug() << "找到应用图标:" << logoPath; return logoPath; } // 尝试查找其他可能的图标文件 QStringList possibleIcons = { "icon.png", appName.toLower() + ".png", appName.toLower() + ".svg", "application-x-executable" // 默认应用图标 }; for (const QString& icon : possibleIcons) { QString iconPath = execDir + QDir::separator() + icon; if (QFile::exists(iconPath)) { qDebug() << "找到替代图标:" << iconPath; return iconPath; } } // 如果没有找到自定义图标,使用系统默认图标 qDebug() << "未找到自定义图标,使用默认图标"; return "application-x-executable"; // 系统默认应用图标 } bool createDesktopShortcut(const QString& appName, const QString& execPath, const QString& iconPath = "", const QString& comment = "", bool terminal = false, bool mateSpecific = true) { // 获取桌面路径 QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); if (desktopPath.isEmpty()) { qDebug() << "无法获取桌面路径"; return false; } // 构建.desktop文件路径 QString desktopFileName = appName + ".desktop"; QString desktopFilePath = desktopPath + QDir::separator() + desktopFileName; // 创建文件 QFile file(desktopFilePath); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "无法创建文件:" << desktopFilePath; return false; } // 写入文件内容 QTextStream out(&file); out << "[Desktop Entry]\n"; out << "Version=1.0\n"; out << "Type=Application\n"; out << "Name=" << appName << "\n"; if (!comment.isEmpty()) out << "Comment=" << comment << "\n"; out << "Exec=" << execPath << "\n"; out << "Terminal=" << (terminal ? "true" : "false") << "\n"; // 使用提供的图标路径或默认图标 QString effectiveIcon = !iconPath.isEmpty() ? iconPath : "application-x-executable"; out << "Icon=" << effectiveIcon << "\n"; // MATE桌面特定配置 if (mateSpecific) { out << "X-MATE-Autostart-enabled=true\n"; out << "X-MATE-Autostart-Delay=0\n"; out << "X-MATE-Provides=panelapplet\n"; out << "Categories=GNOME;GTK;Utility;\n"; } else { out << "Categories=Application;\n"; } file.close(); // 设置文件权限为可执行 QFileInfo fileInfo(desktopFilePath); QFile::setPermissions( desktopFilePath, fileInfo.permissions() | QFileDevice::ExeUser | QFileDevice::ExeGroup | QFileDevice::ExeOther ); qDebug() << "桌面快捷方式已创建:" << desktopFilePath; // 刷新MATE桌面(可选) QProcess::startDetached("mate-panel --replace &"); return true; } bool enableAutostart(const QString& appName, const QString& execPath, const QString& iconPath = "", const QString& comment = "", bool terminal = false, bool mateSpecific = true) { // 获取自动启动目录 QString autostartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart/"; QString autostartFilePath = autostartDir + appName + ".desktop"; // 创建自动启动目录(如果不存在) QDir().mkpath(autostartDir); // 创建文件 QFile file(autostartFilePath); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "无法创建自动启动文件:" << autostartFilePath; return false; } // 写入文件内容 QTextStream out(&file); out << "[Desktop Entry]\n"; out << "Version=1.0\n"; out << "Type=Application\n"; out << "Name=" << appName << "\n"; if (!comment.isEmpty()) out << "Comment=" << comment << "\n"; out << "Exec=" << execPath << "\n"; out << "Terminal=" << (terminal ? "true" : "false") << "\n"; // 使用提供的图标路径或默认图标 QString effectiveIcon = !iconPath.isEmpty() ? iconPath : "application-x-executable"; out << "Icon=" << effectiveIcon << "\n"; // 自动启动特定配置 out << "NoDisplay=true\n"; out << "Hidden=false\n"; out << "X-GNOME-Autostart-enabled=true\n"; out << "X-MATE-Autostart-enabled=true\n"; // MATE桌面特定配置 if (mateSpecific) { out << "X-MATE-Autostart-Delay=0\n"; out << "Categories=GNOME;GTK;Utility;\n"; } else { out << "Categories=Application;\n"; } file.close(); // 设置文件权限 QFileInfo fileInfo(autostartFilePath); QFile::setPermissions( autostartFilePath, fileInfo.permissions() | QFileDevice::ExeUser | QFileDevice::ExeGroup | QFileDevice::ExeOther ); qDebug() << "已启用开机自启动:" << autostartFilePath; return true; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // 获取应用信息 QString appName = QCoreApplication::applicationName(); QString execPath = QCoreApplication::applicationFilePath(); QString comment = "My Qt Application for MATE Desktop"; // 查找应用图标 QString iconPath = findApplicationIcon(appName, execPath); // 检查并创建桌面快捷方式 if (!shortcutExists(appName)) { if (createDesktopShortcut(appName, execPath, iconPath, comment, false, true)) { qDebug() << "MATE桌面快捷方式创建成功!"; } else { qDebug() << "MATE桌面快捷方式创建失败!"; return 1; } } else { qDebug() << "桌面快捷方式已存在,跳过创建"; } // 检查并启用开机自启动 if (!isAutostartEnabled(appName)) { if (enableAutostart(appName, execPath, iconPath, comment, false, true)) { qDebug() << "已成功设置开机自启动!"; } else { qDebug() << "开机自启动设置失败!"; return 1; } } else { qDebug() << "开机自启动已启用,跳过设置"; } return 0; }
2025年07月17日
30 阅读
0 评论
0 点赞
2024-09-17
error while loading shared libraries: libmpfr.so.4: cannot open shared object file
在Ubuntu22上编译QT出现这个错误error while loading shared libraries: libmpfr.so.4: cannot open shared object file加载共享库时出错:libmpfr.so.4:无法打开共享对象文件:没有这样的文件或目录解决办法:sudo ln -s /usr/lib/x86_64-linux-gnu/libmpfr.so.6 /usr/lib/x86_64-linux-gnu/libmpfr.so.4
2024年09月17日
93 阅读
0 评论
0 点赞
2024-09-11
Linux通用串口数据接收代码
#include <iostream> #include <thread> #include <mutex> #include <termios.h> #include <string.h> #include <poll.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <string.h> #include <signal.h> #include <pthread.h> using namespace std; int fd; void serial_read() { int nread; int BUFSIZE = 1024; unsigned char buff[BUFSIZE]; unsigned char *pbuff = NULL; struct timeval tv; fd_set rfds; tv.tv_sec = 2; tv.tv_usec = 0; while (true) { FD_ZERO(&rfds); FD_SET(fd, &rfds); if (select(1 + fd, &rfds, NULL, NULL, &tv) > 0) { if (FD_ISSET(fd, &rfds)) { pbuff = buff; nread = read(fd, buff, BUFSIZE); printf("Recv Len = %d\n", nread); for (int i = 0; i < nread; i++) { printf("0x%x ", buff[i]); } printf("\n"); memset(buff, 0, sizeof(buff)); } } } } int main() { if ((fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0) { printf("err: can't open serial port!\n"); return -1; } struct termios options; tcgetattr(fd, &options); bzero(&options, sizeof(options)); options.c_cflag |= B115200 | CLOCAL | CREAD; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~PARENB; tcflush(fd, TCIOFLUSH); if (tcsetattr(fd, TCSANOW, &options) != 0) return -1; thread th(serial_read); th.join(); return 0; }
2024年09月11日
64 阅读
0 评论
0 点赞
2023-05-19
QT-利用Qcamera查看USB摄像头参数(数据帧格式+分辨率)
运行环境检查:首先检查自己的QT版本及系统环境是否支持Qcamera:Qcamera是multimedia模块中的接口函数,multimedia模块在QT5.3之后是自带的。在windows环境下可直接使用,在LINUX 环境下需要手动安装multimedia模块。代码片段:pro文件包含:QT += multimedia multimediawidgets示例#include "mainwindow.h" #include "ui_mainwindow.h" //相机相关的头文件 #include <QCamera> #include <QCameraInfo> #include <QVideoProbe> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); m_camera = new QCamera(this);//初始化摄像头设备 QVideoProbe *m_pProbe = new QVideoProbe; if(m_pProbe != nullptr) { m_pProbe->setSource(m_camera); // Returns true, hopefully. connect(m_pProbe, SIGNAL(videoFrameProbed(QVideoFrame)),this, SLOT(slotOnProbeFrame(QVideoFrame)),Qt::QueuedConnection); }//其中SIGNAL(videoFrameProbed(QVideoFrame)为VideoProbe模块自带的信号 m_camera->setCaptureMode(QCamera::CaptureViewfinder);//设置捕捉模式为视频 m_camera->setViewfinder(ui->centralWidget);//设置 摄像头画面的显示位置 m_camera->start();//开启摄像头 //查询摄像头支持的分辨率 QList<QSize> sizes = m_camera->supportedViewfinderResolutions(); qDebug() << "viewfinderResolutions sizes.len = " << sizes.length(); foreach (QSize size, sizes) { qDebug() << "Resolutions size = " << size; } //摄像头参数初始化,根据需求用户自定义摄像头分辨率,也可以用来设置摄像头数据类型 QCameraViewfinderSettings set; set.setResolution(VIDEO_WIDTH, VIDEO_HEIGHT);//设置分辨率 //set.setPixelFormat(QVideoFrame::Format_YUYV); //设置像素格式 Android上只支持NV21格式 m_camera->setViewfinderSettings(set); //包含头文件QCameraInfo QList<QCameraInfo> cameras = QCameraInfo::availableCameras();//获取可用摄像头设备列表 foreach (const QCameraInfo &cameraInfo, cameras) { qDebug()<<"检测到设备:"<<cameraInfo.deviceName();//摄像头的设备名称 } } //图像触发的槽函数 void MainWindow::slotOnProbeFrame(const QVideoFrame &frame) { QVideoFrame cloneFrame(frame);//类拷贝操作,fram -> clonframe cloneFrame.map(QAbstractVideoBuffer::ReadOnly); //视频缓冲区数据映射到系统内存 //unsigned char rgb_buffer[VIDEO_WIDTH*VIDEO_HEIGHT*3]; qDebug()<<"设备数据格式:"<< cloneFrame.pixelFormat()<<endl; cloneFrame.unmap(); } MainWindow::~MainWindow() { delete ui; }
2023年05月19日
388 阅读
0 评论
0 点赞
2023-05-08
C语言:获取程序运行消耗的时间(gettimeofday)
对于普通的C程序:#include<stdio.h> #include<sys/time.h> //注意引用这个头文件 #include<unistd.h> int delay(int time) { //这里用来表示你自己要运行的程序 } int main() { //定义两个结构体,来记录开始和结束时间 struct timeval start; struct timeval end; //记录两个时间差 unsigned long diff; //第一次获取时间,即表示开始记时 gettimeofday(&start,NULL); //运行自己的程序 delay(10); //第二次获取时间,即表示结束记时 gettimeofday(&end,NULL); //计算时间差,并打印 diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec; printf(“thedifference is %ld\n”,diff); return 0; }对于内核模块的C程序#include<stdio.h> //注意引用这个头文件 #include<linux/time.h> #include<unistd.h> int delay(int time) { //这里用来表示你自己要运行的程序 } int main() { //定义两个结构体,来记录开始和结束时间 struct timeval start; struct timeval end; //记录两个时间差 unsigned long diff; //第一次获取时间,即表示开始记时 do_gettimeofday(&start,NULL); //运行自己的程序 delay(10); //第二次获取时间,即表示结束记时 do_gettimeofday(&end,NULL); //计算时间差,并打印 diff = 1000000 * (end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec; printf(“thedifference is %ld\n”,diff); return 0; }
2023年05月08日
564 阅读
0 评论
0 点赞
1
2