1.OpenGLWidget
全屏会导致空间显示问题,使用 setHasBorderInFullScreen
解决
QWindowsWindowFunctions::setHasBorderInFullScreen(this->windowHandle(), true);
详见:https://blog.csdn.net/weixin_44328568/article/details/126130185
2.closeEvent 中 delete this 导致崩溃问题,使用 deletelater 解决
this->deletelater();
3.虚基类析构要有实现
现象:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2001 无法解析的外部符号 "public: virtual __cdecl Base::~Base(void)" (??Base@@UEAA@XZ) test H:\Qt\Projects\test\Base.obj 1
要有实现:
virtual ~Base();
businessBase::~Base()
{
}
4.使用 VS 的 QT VS Tools
编译,QT creater 编辑,新增文件需要 [QT VS Tools]->[Open QT Project File (.pro)] 重新生成 VS 工程后编译
5.一份命名规范
1、类名以大写字母开头:class Customer
2、函数名以小写字母开头。
3、尽管编译器允许,我们还是应该尽量避免使用句号,下划线,破折号以及一些花哨的字符.
4、多个单词组成的名字时,后续单词的首字母应该大写,例如class FileTagger void getStudentInfo();
5、常量应该全部采用大写。
6、每个 类名都应该是一个名词或名词短语,例如: class CPlayer;
7、每个函数名都应该是一个动词或者动词的短语,例如:processBookOrder();
8、每个布尔的变量在该用到if()语句时都应该近似于一个句子,例如 bool isQualified;
9、数据成员名:m_Colr,m_Width(以小写的m开头);
10、static数据成员:sm_Singletom;
11、非布尔型getters: clour(), getColor();
12、布尔型getters: isChecked();
13、Setter:setColor(const Color)
6.多重定义问题
① 移除文件时,QT修改 .pro 文件出错,导致源文件和头文件重复包含了,修改.pro文件,去重即可
7.未定义问题
① 槽函数声明,不定义会编译报错引用未定义。
② 继承了QThread,在自己类中声明了run,却没有实现run。
③ 其他类似,实现对应方法即可。
8.项目应该有 README.md
,其中包含对项目目录文件的说明信息
.
├─business // 业务代码
├─doc // 文档
├─log // 日志
├─network // 网络通信
├─*** // 核心代码
├─resource // 图片等资源文件
├─*** // 推流功能
├─Utils // 工具类
├─*** // **控件
├─*** // **功能
└─*** // **变换
9.打包程序
H:\Qt\Qt5.11.3\5.11.3\msvc2017_64\bin\windeployqt.exe yourAppName.exe
10.重写接口 出现 unknown variable
问题
①可能是没有包含对应的头文件,如重写 鼠标事件 需要包含 QMouseEvent 头文件。
11.windows 开机自启
void SetAutoStart(bool isChecked)
{
QString appName = QApplication::applicationName();//获取应用名称
QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);//创建QSetting, 需要添加QSetting头文件
if(isChecked) {
QString appPath = QApplication::applicationFilePath().replace(".exe", ".lnk");//找到应用的目录
if (!QFile::exists(appPath)) {
QFile::link(QCoreApplication::applicationFilePath(), appName + ".lnk");
}
settings.setValue(appName, QDir::toNativeSeparators(appPath));//写入注册表
} else {
settings.remove(appName);
}
}
12.程序打包,inno setup compile
打包运行需要管理员权限,打包后不能运行,iss文件需要添加如下
- 说明:虽然加了权限,用管理员权限打开inno程序再打包会解决安装包无法启动有管理员权限或者无管理员权限但无法启动的问题;不知道是不是修改了inno的某个加权限的文件生效的;@有管理员权限的程序设置开机自启会无效果,所以应用程序去掉管理员权限,安装器要有管理员权限,首次启动程序是有管理员权限的。
[Setup]
PrivilegesRequired=admin
13.hover时间导致SDL当前渲染效果消失,下次渲染显示后,造成闪烁。
//setAttribute(Qt::WA_Hover, true); // 去掉此语句,即不使用 鼠标的hover事件
14.宽高比例窗口自适应
// container : 需要变换的窗口外部包裹的size
QSize Widget::resizePictureWindow(int widthSrc, int heightSrc, QSize container)
{
if ( widthSrc <= 0 || heightSrc <= 0 || container.width() <= 0 || container.height() <= 0 ) return QSize();
// calc
int destHeight = container.width() / (static_cast<double>(widthSrc) / static_cast<double>(heightSrc));
int destWidth = container.height() / (static_cast<double>(heightSrc) / static_cast<double>(widthSrc) );
if (container.width() < destWidth) { destWidth = container.width(); }
if (container.height() < destHeight) { destHeight = container.height(); }
return QSize(destWidth, destHeight); // result
}
15.QT + VS混合编程,中文编译不过问题
①.简单解决:notepad++打开有 中文的源码文件,“编码”-> “使用UTF-8-BOM编码”,保存关闭,重新编译即可。
②.类构造函数添加如下代码,不一定有效
QTextCodec *codec = QTextCodec::codecForName("UTF-8-BOM");
QTextCodec::setCodecForLocale(codec);
16.主窗体垂直居中
#include <QDesktopWidget>
this->move((QApplication::desktop()->width() - width())/2,(QApplication::desktop()->height() - height())/2);
17.计算文字占用宽度,重新设置label等大小
QFontMetrics fm(ui->label->font());
int width = fm.boundingRect( name ).width() + 4;
ui->label->resize(width, ui->label->height());
18.QT+SDL2在linux下渲染看不见问题,需要SDL_CreateWindowFrom函数后添加SDL_ShowWindow(sdlWindow);
SDL_Window *sdlWindow = NULL;
sdlWindow = SDL_CreateWindowFrom((void *)pInterface->m_windowindex[0].hWnd);
SDL_ShowWindow(sdlWindow);
19.linux下出现如下问题,alsa 需要重新编辑pro文件再 编译 【即重新构建】
lib/libasound.a(pcm_dmix.o):
in function `snd_pcm_dmix_sync_ptr': warning ...
20.QT+MSVC中文乱码问题
1.设置QT:工具->外部->configuration->文本编辑器->行为->文件编码 设置默认编码为UTF-8并设置UTF-8 BOM为如果是UTF8编码则添加.
2.在对应cpp文件顶部添加 #pragma execution_character_set("utf-8")
21.SDLz之main入口问题,试了 #undef main ,无效【以下是QT+SDL2用MSVC编译通过;Linux下需要SDL_main变为main】
#include <QApplication>
/* cause [#define main SDL_main] , into SDL_main.h */
#ifdef __cplusplus
extern "C"
#endif
int SDL_main(int argc, char* argv[])
{
QApplication a(argc, argv);
TestQtOpengl w;
w.show();
return a.exec();
}
22.关闭自重启
QApplication::closeAllWindows();
QProcess::startDetached(QApplication::applicationFilePath(), QStringList());
23.qt之UOS系统👎 error: Unknown module(s) in QT: *
multimedia:sudo apt-get install qtmultimedia5-dev
websockets:sudo apt-get install libqt5websockets5-dev
23.uos安装Qt5环境
https://blog.csdn.net/weixin_44328568/article/details/127615554
24.获取程序目录
qDebug()<<"current applicationDirPath: "<<QCoreApplication::applicationDirPath();
qDebug()<<"current currentPath: "<<QDir::currentPath();
25.QAudioOutput播放音频,QIODevice在write时导致的内存泄漏问题,后续加了setBufferSize,一段时间后会释放一块大内存
[采集和播放可参考:https://github.com/dujingning/PcmCaptureAndPlay ]
QAudioFormat qAudioFormat;
//设置 QAudioFormat 参数
QAudioOutput audioOutput = new QAudioOutput(qAudioFormat);
audioOutput->setBufferSize(512000); // 关键是这个,说是有默认值,start前调用有效; bufferSize()则是stat调用用后有效。
QIODevice ioDevice = audioOutput->start();
if ( ioDevice && !ioDevice->isOpen() )
ioDevice->open( QIODevice::WriteOnly );
//...其他设置
...
//------------随后周期性写入
qint64 bufferPlay = out->periodSize();
if (out->bytesFree() < bufferPlay) {
return;
}
ioDevice->write(buffer, len);
26.QFile写文件
static QFile f("8000_16_1.pcm"); if (!f.isOpen()) { f.open(QIODevice::WriteOnly); } if (f.isOpen())
f.write((const char*)data, length);f.flush();
27.显示隐藏鼠标
①只在某个 widget 中隐藏鼠标指针,在该 widget 的构造函数中调用
QWidget::setCursor(QCursor(Qt::BlankCursor))
②程序内不显示,实例化 Application 后调用
QApplication::setOverrideCursor(Qt::BlankCursor);
③在某控件或页面内显示/隐藏
this->setCursor(Qt::BlankCursor); //隐藏
this->setCursor(Qt::ArrowCursor); //显示
28.设置窗口透明,控件不透明
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags( Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setWindowOpacity(1); // 控件统一透明度
29.重写鼠标事件拖动窗口
QPoint Pos;
void widget::mousePressEvent(QMouseEvent *event)
{
Pos= (event->globalPos()) - (this->pos());
}
void widget::mouseMoveEvent(QMouseEvent *event)
{
if (this->isFullScreen() == false)
{
this->move(event->globalPos() - Pos);
}
}
30.Qt Creator修改qss文件后导致样式无效
1.使用编辑器(如notepad++)修改(编码->转为ANSI编码)编码格式为ANSI。
2.工具>选项>文本编辑器>行为>文件编码,修改UTF-8 BOM 为总是删除。
31.QT VS tools 创建VS工程引用的库路径不全
解决:.pro文件中的 -llibsdk ,其中libsdk应为库的全名
32.MSVC编译32位报错:模块对于 SAFESEH 映像是不安全的
原因:链接了使用较早版本编译器生成的.lib或者.obj文件,或者找不到“safe exception handlers”
解决:项目->项目属性->链接器->命令行,在右侧其他选项中添加 /SAFESEH:NO ,保存并重新编译
33.MSVC编译32位程序无法正常启动
没错缺少那个库,先删除目录下其他dll,重新复制32位动态库到程序目录就可以了
题外话,打包建议添加下的VC动态库,有些电脑上没有会报错:
VC\Redist\MSVC\14.16.27012\x86\Microsoft.VC141.CRT