Qt程序,总会遇到各种使人凌乱的问题,于是决定开始记录...
一、程序移植问题
1、用vs2010交叉编译的Qt4.7.4程序,在移植到x86的裸机上时,需要添加..\Microsoft Visual Studio 10.0\VC\redist\x86\Microsoft.VC100.CRT\目录下的msvcp100.dll和msvcr100.dll。
2、使用phonon编写程序发布时,除了phonon4.dll外,还要将..\plugins\目录下的phonon_backend文件夹拷贝到项目中,如果目标机为x86, 则需要phonon_backend\phonon_ds94.dll即可。
否则会出现以下警告,程序没有声音:
WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded WARNING: Phonon::createPath: Cannot connect Phonon::MediaObject ( no objectName ) to Phonon::AudioOutput ( no objectName ). WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded WARNING: bool __thiscall Phonon::FactoryPrivate::createBackend(void) phonon back end plugin could not be loaded
3、设置应用程序图标
首先准备个ICO图标。例如:myappico.ico,用记事本新建个文件里面就写一行:
IDI_ICON1 ICON DISCARDABLE "myappico.ico"
保存改名为 myapp.rc并把它和你的图标myappico.ico一起复制到你的QT工程项目的目录。 用记事本打开你的QT工程文件(如 "myapp.pro" ),在里面最后新添一行
RC_FILE = myapp.rc
保存,编译你的工程,OK.
4、Qt debug 和release 版本导入不同的库(opencv2.4.6)
INCLUDEPATH += C:/OpenCV2.4.6/opencv/build/include CONFIG(debug, debug|release) { #debug LIBS += -LC:/OpenCV2.4.6/opencv/build/x86/vc10/lib/ \ -lopencv_core246d \ -lopencv_contrib246d \ -lopencv_calib3d246d \ -lopencv_flann246d \ -lopencv_features2d246d \ -lopencv_highgui246d \ -lopencv_imgproc246d \ -lopencv_nonfree246d } else { #release LIBS += -LC:/OpenCV2.4.6/opencv/build/x86/vc10/lib/ \ -lopencv_core246 \ -lopencv_contrib246 \ -lopencv_calib3d246 \ -lopencv_flann246 \ -lopencv_features2d246 \ -lopencv_highgui246 \ -lopencv_imgproc246 \ -lopencv_nonfree246 }
5、Qt 5.1.0 msvc2010_opengl 移植到其它电脑上打不开,将plugins/下的platforms文件夹拷贝到项目中,可删除platform下*d.dll。截图:
6、Ubuntu 下安装Qt后出现Permission denied,使用如下命令获取root权限:
sudo chown -R zdd:zdd ~/.config/
7、将Qt写的含有boost1.55.0和opencv2.4.8的项目移植到mac os x 10.9时,出现
symbol(s) not found for architecture x86_64 linker command failed with exit code 1(use -v to see invocation)
1、如果项目是从win32移植过来,首先使用dos2unix 命令转字符格式.
2、如果从ubuntu上移植过来,注意boost线程的库为boost_thread-mt而不是boost_thread.
3、如果还有问题,更改../Qt5.2.1/clang_64/mkspecs/macx-clang/qmake.conf中的
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.6
QMAKE_MACOSX_DEPLOYMENT_TARGET = 10.9
如果运气足够好,你的Debug版本程序能顺利执行,Release版本依然不能执行。这是MAC_SDK使用的还是10.8.所以,在配置文件中再添加:
macx: QMAKE_MAC_SDK = macosx10.9
就行了。
8、Mac Qt release
this->setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint);
2、Qt自带系统托盘例子,右键单击托盘但是不选中,托盘菜单不消失。
将
trayIconMenu = new QMenu(this);
改为:
trayIconMenu = new QMenu((QWidget*)QApplication::desktop());
3、
this->setMouseTracking(true);//调用该函数,开启鼠标跟踪事件响应,否则,得拖动时mouseMoveEvent才响应。
4、使用Win_QextSerialPort类编写串口程序"\\\\.\\"保证在windows下COM10以后的串口可用
relayCom = new Win_QextSerialPort("\\\\.\\" + comName, QextSerialBase::EventDriven);
5、平滑直线
QPainter painter(&image); painter.setRenderHint(QPainter::Antialiasing);//平滑直线 painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.drawLine(lastPoint, endPoint);
6、opencv 2.4.3版本通用特征及描述符创建错误,需调用initModule_nonfree()函数
cv::initModule_nonfree(); featureDetector = cv::FeatureDetector::create(getDetectorType(detector)); descriptorExtractor = cv::DescriptorExtractor::create(getDescriptorType(descriptor)); descriptorMatcher = cv::DescriptorMatcher::create(getMatcherType(matcher));
7、mouseMoveEvent 按键判断
void SomeView::mousePressEvent(QMouseEvent *e) { if(e->button() == Qt::LeftButton) { //todo... } else if(e->button() == Qt::RightButton) { //todo... } else { //todo... } } void SomeView::mouseMoveEvent(QMouseEvent *e) { //该函数中,e->button()总是返回Qt::NoButton.使用如下 if(e->buttons() & Qt::LeftButton) { //... } //... }
8、Init CriticalSection spin count异常
新建Qt Qui项目,运行出现如下异常:
Init CriticalSection spin count CDLPShareMemConfig::Close()CDLPShareMemConfig::Close()TSWebDownLoadProtect.dll is Load failedTSWebMon.dat is Load failedDelete CriticalSection spin count
经过@南京短暂的春天的建议,果断卸载腾讯安全管家,该异常消失,初步判断为软件冲突引起。Thank you @南京短暂的春天.
9、error C2243: 'type cast' : conversion from 'const Object*' to 'const QObject *' exists, but is inaccessible
请确保const Object 的父类是public.
10、error C2664: 'QMetaObject::Connection cannot convert parameter 2 from 'void (__thiscall ToolWidget::* )(void)' to 'const char *'
使用Qt5.1 新的connect写法给qApp传递信号时出现上述错误:
connect(toolWidget, &ToolWidget::closeClicked, qApp, &QApplication::quit);//错误使用原来的写法正确:
connect(toolWidget, SIGNAL(closeClicked()), qApp, SLOT(quit()));//正确新的语法对类型检测很严,当出现同名函数后,混淆,例如:以下写法:
void (QComboBox:: *indexChangedSignal)(QString) = &QComboBox::currentIndexChanged; connect(comboBox, indexChangedSignal, this, &MyClass::func1) //正确照着试试QApplication只有一个quit信号:
connect(toolWidget, &ToolWidget::closeClicked, qApp, static_cast<void (QApplication::*)()>(&QApplication::quit));//错误 connect(toolWidget, &ToolWidget::closeClicked, qApp, static_cast<void (QCoreApplication::*)()>(&QCoreApplication::quit));//错误
connect(toolWidget, &ToolWidget::closeClicked, &QApplication::quit);//正确
推荐一个用Qt写界面的非常棒的博客@續寫丶那過厾的专栏
推荐的另一个Qt博客逛奔的蜗牛
赞助:如果您觉得此渣文对您有用,欢迎对我进行无负担小额赞助,一毛两毛的都行。不筹钱娶媳妇的程序员不是好程序员!