QT3与QT4移植中的问题引子

来源:百度文库 编辑:神马文学网 时间:2024/05/06 17:46:24
Here's what I found on my Qt-4.2.2 installation (you may have to poke around to find things on your system):

Start in the qt "include" directory.On my system, is it at /usr/local/Trolltech/Qt-4.2.2/include
I'm guessing that on your system this may be /usr/include/qt4, according to the "-I..." stuff that you are seeing.

On my system there is a Qt sub-directory and there's where i see "qapplication.h". (You can go to /usr/include and run "find . -name qtapplication.h")

When I look for the text "setMainWidget" in that qtapplication.h, I see:

CPP / C++ / C Code:
#ifdef QT3_SUPPORTstatic QT3_SUPPORT QWidget *mainWidget();static QT3_SUPPORT void setMainWidget(QWidget *);#endif

So, it's telling us that something changed between Qt3 and Qt4.

For an immediate fix, you can define QT3_SUPPORT:

CPP / C++ / C Code:
//// Define QT3_SUPPORT before including any qt headers//#define QT3_SUPPORT#include #include int main(int argc, char **argv){


Now, try make again (no need to run qmake unless you want to).

Next step: investigate what the heck they did.

Go to the trolltech web site and look at the documentation for qapplication in qt4.2. http://doc.trolltech.com/4.2/qapplication.html

Here's the scoop:

void QApplication::setMainWidget ( QWidget * mainWidget ) [static]

Sets the application's main widget to mainWidget.

In most respects the main widget is like any other widget, except that if it is closed, the application exits. Note that QApplication does not take ownership of the mainWidget, so if you create your main widget on the heap you must delete it yourself.

You need not have a main widget; connecting lastWindowClosed() to quit() is an alternative.

For X11, this function also resizes and moves the main widget according to the -geometry command-line option, so you should set the default geometry (using QWidget::setGeometry()) before calling setMainWidget().
Huh? It indicates that there is a setMainWidget in QApplication, and no indication that it was removed. (This is the Qt-4.2 documentation, right?)

However --- it does say that "you need not have a main widget".

So, just delete the setMainWidget line in your program, and remove the QT3_SUPPORT definition while you are at it:

CPP / C++ / C Code:
#include #include int main(int argc, char **argv){QApplication a(argc, argv);QPushButton hello("Hello world!", 0);hello.resize(100, 30);hello.show();return a.exec();}

Now start all over and see what you get. For me, it worked. Your Mileage May Vary.

Lesson learned: When you get an example program, even from a reputable source, if it doesn't work for you, then you might discover that later revisions in the tools make earlier examples non-functional. Where was the example file that you got?

Lesson learned: Programming, like life, is an adventure. For some people the trip is more important than the destination. For others, we just want to get the job done. It's still an adventure.

In my installation, in /usr/local/Trolltech/Qt-4.2.2 there is a subdirectory of "examples" that has a lot of subdirectories with source files that work with Qt-4.2.2 (Copy them to some place where you can use them; in their original place you probably don't have write privileges anyhow.)

Regards,

Dave

Footnote:

"It's not a problem; it's an opportunity!"
--- My old pal Jorge Rivera

"I like opportunities as much as the next guy. It's
just that on Saturday afternoons I would rather
define my own adventures rather than working
on other people's opportunities (like the boss's
deadline)."
---davekw7x   #3   07-Mar-2007, 11:37 davis   Posts: n/a

Re: problem in hello world programme in qt


Quote: Originally Posted by u04f061 i am new user of qt and i am running ubuntu dapper linux.i got this hello world programe from trolltech official site. i could not run this programe. i got an error.here it is

#include
#include g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o main.o main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:12: error: ‘class QApplication’ has no member named ‘setMainWidget’
make: *** [main.o] Error 1

The code is pre v4.0 and is using API from v3.x. Go to your examples directory under your /usr/share/qt4 path and try the hello from there.