关于Qt中常遇到的问题总结:
1.数据库问题
如果报错连接不上数据库:查询数据库==位数(32位,86位)==当前环境数据库位数需要与Qt编译环境位数相同,否则无法连接。
QWeb模块问题,需要先load才可以调用load完成信号使用。
新版Qt 5.14
需要自己手动编译数据库:
首先打开路径:D:\Qt5.14\5.14.2\Src\qtbase\src\plugins\sqldrivers\mysql
将第七行QMAKE_USE += mysql
注释掉
mysql.pro里添加完后,找到qsqldriverbase\qsqldriverbase.pri 这个文件,注释include($$shadowed($$PWD)/qtsqldrivers-config.pri)
添加include($$shadowed($$PWD)/configure.pri)
添加数据库sdk的include目录及库文件,保存并关闭
如:
TARGET = qsqlmysql
HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp
INCLUDEPATH+="C:/Program Files/MySQL/MySQL Server 5.7/include"
LIBS += "C:/Program Files/MySQL/MySQL Server 5.7/lib/libmysql.lib"
DESTDIR +="../mysql/lib/"
OTHER_FILES += mysql.json
PLUGIN_CLASS_NAME = QMYSQLDriverPlugin
include(../qsqldriverbase.pri)
在D:\Qt5.14\5.14.2\msvc2015_64\plugins\sqldrivers路径下找到编译好的动态库
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nBESq97F-1640914555522)(C:\Users\ASUS\AppData\Roaming\Typora\typora-user-images\image-20201110171147027.png)]
添加D:\Qt5.14\5.14.2\msvc2017_64\plugins\sqldrivers;
将dll添加到bin下,重新打开Qt运行即可
关于Qt在运行.exe文件时连接不上数据库的问题
使用Qt在运行程序是连接MySQL数据库失败,但是在Qt中可以直接运行,所以原因可能是在Windows环境下没有找到该数据库的驱动,解决办法是:
将\MySQL\lib文件夹中"libmysql.dll"复制到C:\Windows下
2.资源文件问题
如果添加资源文件报堆空间不足,需要在pro文件中加入CONFIG+=resources_big
3.Qt中WebGL问题
首先查看QWebEngineView是否启用了WebGL
m_pEngineView->load(QUrl(QStringLiteral("chrome://gpu/")));
然后查看reset notification strategy
如果是0x8252则支持WebGL,如果是0x0000则不支持WebGL。
我的显示是0x0000,下一步则开启WebGL。
在main函数QApplication或QCoreApplication创建之前添加Qt::AA_ShareOpenGLContexts属性
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QApplication a(argc, argv);
MainWindow w;
然后reset notification strategy变为0x8261,说明已经支持WebGL了,再次加载Cesium即可。
4.QLineEdit设置错误提示信息的方法
设置错误提示信息用的是QToolTip
类中的showText()
接口函数来实现的,此类可以为任何窗口提供一个小的提示tips窗口。在QLineEdit中一般是用信息textEdited()
信号。例如,设定输入框的输入值范围为0-3000,若不在范围内,会进行提示。具体的槽函数实现如下:
void onLineEdit_textEdited(const QString &text)
{
ui->lineEdit->setValidator(new QDoubleValidator(0, 3000, 2)); //设置验证器
if(text.toDouble()<0 || text.toDouble()>3000)
{
QToolTip::showText(ui->lineEdit->mapToGlobal(QPoint(100, 0)), "范围应该是0-3000");
ui->lineEdit->setStyleSheet("QLineEdit{border:1px solid red}");
}
else
{
ui->lineEdit->setStyleSheet("QLineEdit{border:1px solid gray border-radius: 1px }");
}
}
5.阴影窗口的实现方法
阴影窗口的实现方法一般有三种,方法一是:在窗口的构造函数中设置窗口的属性为:
setAttribute(Qt::WA_TranslucentBackground);
从而保证窗口不被绘制的部分透明化。接着重写绘制函数paintEvent()
,其代码示例如下:
void QT_Test::paintEvent(QPaintEvent *event)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10, 10, this->width()-20, this->height()-20);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillPath(path, QBrush(Qt::white));
QColor color(0, 0, 0, 50);
for(int i=0; i<10; i++)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2);
color.setAlpha(150 - qSqrt(i)*50);
painter.setPen(color);
painter.drawPath(path);
}
}
方法二是用一个阴影窗口来覆盖在原窗口上,或者用一张阴影的背景图片来重画窗口。这里不再具体说明。
方法三是通过QFrame + QGraphicsDropShadowEffect
的方式实现。其代码示例如下:
QFrame *frame = new QFrame(this);
frame->setStyleSheet("QFrame{background-color: rgb(255, 255, 255);border-radius:10px}"); //设置圆角与背景透明
frame->setGeometry(5, 5, this->width() - 5, this->height() - 5);//设置有效范围框
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
shadow_effect->setOffset(0, 0);
shadow_effect->setColor(Qt::black);
shadow_effect->setBlurRadius(10);
frame->setGraphicsEffect(shadow_effect);
this->setAttribute(Qt::WA_TranslucentBackground);
//若没有效果,可能是设置了setContentsMargins,调整这个函数的参数即可
6.Qt程序启动画面
#include <QSplashScreen>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//创建启动动画类实例,使用资源文件splash.jpg作为展示图片
QSplashScreen splash(QPixmap(":/splash.jpg"));
splash.show();
QWidget w;
w.show();
splash.finish(&w);
return a.exec();
}
7.避免界面冻结
QCoreApplication::processEvents();
8.关于找不到ui_问题
将所处项目移除重新添加编译
9.VS中生成动态库问题配置
查看正常
OutPut Directory $(SolutionDir)bin\$(Configuration)\名称\
SolutionDir只文件目录
Configuration指 Debug 或 Relase
Target Extension .dll
Configuration Type .dll
Linker General $(OutDir)\$(ProjectName.dll)
10.关于Qt主窗口置顶问题
this->setWindowFlags(this->windowFlags() |Qt::Widget);
this->setWindowModality(Qt::ApplicationModal);
11.Qt自定义注释
工具->选项->文本编辑器->片段
/**
* @projectName %{CurrentProject:Name}
* @brief 摘要
* @author Wanglipeng
* @date %{CurrentDate:yyyy-MM-dd}
*/
12.Qt窗口居中显示
QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以
move((desktop->width() - this->width())/2, (desktop->height() - this->height())/2);
13.Qt设置路径
如果存在路径中有空格符号,路径前后加上=="\“"==加转义字符