qt4环境下跑qt3程序的遇到的小问题
参考资料:<<C++ GUI Programming with Qt 3 >>英文版
1 今天跑了书上一个例子,是QT3写的,但我只有QT4的环境,所以出现了一点问题~~一开始没有看QT4的文档
看如何运行QT3的程序,所以直接看API修改源代码,代码如下(QT4环境下运行QT3程序的方法见下面2)
#include <iostream>
//这是<<C++ GUI Programming with Qt 3 >>书上一个例子,因为我机器上的QT版本是4.2.2,结果
//编译起来接口不一致,弄的好烦~~好不容易看了API改的程序能跑起来了~~还是有点小问题,没时间,现不改了
#include <QChar>
#include <QDir>
#include <QString>
#include <QStringList>
using namespace std;
int imageSpace(const QString &path) {
QDir dir(path);
QStringList::Iterator it;
int size = 0;
const QString qs("*.jpg");
const QStringList st(qs);
//const QString qs1(" *.png");
//const QString qs2(" *.jpeg");
//st.join(qs1);
//st.join(qs2);
QStringList files = dir.entryList(st,
QDir::Files);
it = files.begin();
while (it != files.end()) {
size += QFileInfo(path, *it).size();
++it;
}
cout << "size-->" << size << endl;
QStringList dirs = dir.entryList(QDir::Dirs);
it = dirs.begin();
while (it != dirs.end()) {
if (*it != "." && *it != "..")
size += imageSpace(path + "/" + *it);
++it;
}
return size;
}
void printQString(QString s) {
const QChar* c = s.unicode();
int i=0;
while (true) {
if (c[i].toLatin1()!='/0') {
cout << c[i].toLatin1();
} else {
cout << "/n";
break;
}
i++;
}
}
int main(int argc, char *argv[]) {
//QT3中的currentDirPath()--->QT4的currentPath()
QString path = QDir::currentPath();
if (argc > 1)
path = argv[1];
cout << "Space used by images in " ;
//QT4找不到QString的path.ascii()方法,只能自己写了个printQString函数打印QString调试,烦死了!(可能我没找到)
printQString(path);
cout << " and its subdirectories is "
<< (imageSpace(path) / 1024) << " KB" << endl;
return 0;
}
2 看了QT4.2.2的文档,发现可以用QT4来编译QT3的程序的
Add the line QT += qt3support to your .pro file if you use qmake; otherwise, edit your makefile or project file to link against the Qt3Support library and add -DQT3_SUPPORT to your compiler flags. (You might also need to specify other libraries. See What's New in Qt 4 for details.)
就是在qmake -project 生成的XX.pro文件只能够加上一行QT += qt3support
###################################################################### TEMPLATE = app QT += qt3support //就是这行 # Input |
然后qmake ,最后make 就行了(QT3的程序代码如下)
#include <iostream> using namespace std; int imageSpace(const QString &path) QStringList files = dir.entryList("*.png *.jpg *.jpeg", QStringList dirs = dir.entryList(QDir::Dirs);
cerr << "Space used by images in " << endl return 0;
|
但是程序运行后,没有任何显示?(解决方法看3)
3 因为程序编译没有问题,链接也没有问题~~那问题出在哪里呢?
我猜想可能出现链接的参数上面
g++ -mthreads -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runt |
就是这个参数作怪,这个是qmake自动生成~~只要把这行去掉,程序运行就没有问题了!
-Wl表示这个参数是是gcc编译器传给连接器的参数。(这个参数在网上没有查找到意思,希望知道的兄弟回个帖)
其他几个传给连接器的参数,我在网上找了下资料(只找到下面几个)
--disable-stdcall-fixup
If the link finds a symbol that it cannot resolve, it will attempt to do "fuzzy linking" by looking for another defined symbol that differs only in the format of the symbol name (cdecl vs stdcall) and will resolve that symbol by linking to the match. For example, the undefined symbol _foo might be linked to the function _foo@12, or the undefined symbol _bar@16 might be linked to the function _bar. When the linker does this, it prints a warning, since it normally should have failed to link, but sometimes import libraries generated from third-party dlls may need this feature to be usable. If you specify `--enable-stdcall-fixup', this feature is fully enabled and warnings are not printed. If you specify `--disable-stdcall-fixup', this feature is disabled and such mismatches are considered to be errors.
4 最后程序运行正常!
Space used by images in
D:/Qt/__GUI_Programming_with_Qt3/debug
and its subdirectories is109 KB