How to change MFC based project to Qt4 project in Visual Studio 2005

vedeveloper
vedeveloper used Ask the Experts™
on
Hi,
I created a solution in VS 2005 using "File->New Project->Qt4 Projects -> Qt Application". During the build process, 6 new files are created.

It looks like there are two from the .ui file (qrc_application.cpp and ui_application.h) and the other 4 are from two of my classes which inherit from QWidget.

Whenever I add a .ui file to the solution, that is also built automatically and everything is fine.

The problem lies with my MFC based solution that I'd like to add a Qt dialog to. If I add the .ui files then they aren't built automatically. Neither are the customised QWidget files. This obviously causes problems as I need to include ui_application.h when initialising the dialog.

I think this comes down to some project settings / preferences, or extra command line options. I found a tutorial here which explains what is going on but obviously this is going to take time if it isn't automated like in my original project:

http://doc.qt.nokia.com/2.3/designer/chap5_4.html

I've tried comparing the two project properties and there are no great differences or references to uic and moc that I could find.

I've looked at the Qt-MFC migration projects and tutorials and they only talk about converting the MFC code to use the Qt-MFC wrapper classes. There isn't anything (I could find) about changing the project settings.

I'm obviously overlooking something, or moc and uic are initialised by something else in the code.

Any help, as always, is greatly appreciated.

JP
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
FYI
In the .vcproj file there are VCCustomBuildTool entries under 'Header Files' and 'Form Files' which point to uic.exe and moc.exe in the Qt installation directory. These refer to the files that are used to build the 6 mentioned above.

I need to know how to get these entries in the .vcproj file without modifying it using notepad.

For example, when I add a .ui file or the custom QWidget's .cpp and .h files in my Qt project, those VCCustomBuildTool entries are added automatically.

I need to work out how to do this with my second project. Due to the number of files / projects we're working on it won't be possible to manually modify the .vcproj file every time.

Thanks
Hi vedeveloper,

I don't have any particular experience using QT and MFC within VS, but from your second post I guess your main problem is the custom build step for some new created files, right?

You can set the custom build step for those files by editing its properties:
- select the file in the 'Solution explorer', open the context menu via right click and select 'Properties'
- in 'Configuration properties -> General -> Tool' select 'Custom build tool'
- press 'Apply' so the tree is updated
- in 'Configuration properties -> Custom Build Step -> General' now you can specify with which tool (Command Line) the file is to be built - the best would be to copy the appropriate settings from one of the files which already work ...

Hope that helps,

ZOPPO

Author

Commented:
Thanks,
Yes, that looks like my problem so we're getting there.

I got the .ui file to build, but when I added another .ui file I have to add those details again. We're going to be adding a lot of .ui files so this isn't really convenient.

So, is there a way to automatically give .ui files those custom build steps? It seems possible since it works for my original project.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Hi vedeveloper,

you can't mix MFC and Qt, since both of them implement its own message loop. Its not enough to copy the Qt-Dialog, you would also need an QtApplication and call its exec()-method, which starts the Qt message-loop and gives the control over to Qt, needless to say that that is what MFC needs too.

Long story short: pick one and stick to it in your program.

ike

Author

Commented:
Thanks, but it is possible to mix Qt and MFC with the Qt-MFC migration framework.

http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Windows/qtwinmigrate/

But this isn't useful to me if I can't get Visual Studio building the files I've mentioned in the first place.

Cheers

JP
Correction: Qt has a class that merges the Mfc & Qt Message Loop called QMfcApp. actually for migration from Mfc to Qt. For details look at:

Article: "MFC to Qt Migration - Walkthrough"
Paragraph: "Replacing the MFC event loop"

  http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-walkthrough.html


But this is supposed to make the migration easier rather than run both in parallel.
I would strongly suggest to pick one of them, that will make your life a lot easier :)

Anyway, good luck
Sry, didnt see your post when writing mine :)

Author

Commented:
It's ok, thanks
To avoid the need to change custom build tool properties for each added file you can create a 'Custom Build Rule'.

To do so right click your project in the solution explorer and select 'Custom Build Rules ...' from the context menu. In the upcoming dialog use 'Add Rule File ...' to create a new rule file. A new dialog appears where you can use 'Add build rule' to open the dialog 'Add Custom Build Rule' - in there you can specify things like command line, name, dependencies, file extentions and more - after adding and activating the rule all new files of this type should automatically use the Custom Build Tool specified ...
>> can't get Visual Studio building the files I've mentioned in the first place.

you can use qmake to re-create the dsp-file with the additional moc build-step for your qt-dialog.

Look at Paragraph: "Replacing the MFC application" here:

  http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-walkthrough.html

You can also add the moc-build-step to the qt-dialog-file per hand, just copy it from your reference-project, but it will be a lot easier with qmake.

Author

Commented:
Thanks guys,

Zoppo
I'm going to look at the custom build rules now.

Ikework
We've got loads of projects and dialogs to change so doing anything manually is going to  be a nightmare.
>> We've got loads of projects and dialogs to change so doing anything manually is going to  be a nightmare.

Sure, that's why I mentioned qmake as first option ;)
Here is a qmake file, that generates all the necessary project-settings for you and creates the visual studio dsp-file.

Just add the .ui-files to the FORMS-section and qmake will do it for you.


  from paragraoh "Using the Qt build system" at:
    http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-walkthrough.html#using-the-qt-build-system</p>

Good luck

TEMPLATE = app
 TARGET   = QtMfc4

 DEFINES -= UNICODE
 DEFINES += _AFXDLL WINVER=0x0500
 QMAKE_LIBS_QT_ENTRY =

 HEADERS = childview.h mainframe.h qtmfc.h stdafx.h
 SOURCES = childview.cpp mainframe.cpp qtmfc.cpp stdafx.cpp
 FORMS   = optionsdialog.ui
 RC_FILE = qtmfc.rc

 include(../../../src/qtwinmigrate.pri)

Open in new window

But again, its all mentioned in a step-by-step walkthrough here, from beginning up to adding dialogs:

  http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-walkthrough.html

Author

Commented:
Zoppo, I've got that working for .ui files now.

How about the moc'ing part for the QWidget files?

Also, slightly off subject. Is there any way to link a Filter in the Solution Explorer to an actual folder.

For example, I'd like a 'Generated Files' filter in the Solution view where all these files are created. But I would also like them to be in a seperate file. I can't just filter the filter by .cpp as I'd get all my source files in there.

Again, this needs to be automatic and seems to be ok in my original project.

Author

Commented:
Another problem now, when I include the .h file that is built from the .ui file I get compile errors in qvector.h and qlist.h:

Error      3      error C2061: syntax error : identifier 'pNew'      c:\qt32\4.6.0\include\qtcore\qvector.h      500
Error      4      error C2061: syntax error : identifier 'pNew'      c:\qt32\4.6.0\include\qtcore\qvector.h      505
Error      1      error C2061: syntax error : identifier 'n'      c:\qt32\4.6.0\include\qtcore\qlist.h      352
Error      2      error C2061: syntax error : identifier 'current'      c:\qt32\4.6.0\include\qtcore\qlist.h      383
That's why the put qmake into Qt, cause that's taking care of setting up your project .. last time said, promised ;)

Author

Commented:
Ikework, I'm not ignoring you're comments by the way. Just didn't think that those compile errors were because I wasn't using qmake.

So how do I get my Visual Studio project to use qmake?

As you can tell I'm not familiar with this project settings stuff (apart from adding headers and linking to libraries). I've just been used to coding and clicking 'build'!
>> So how do I get my Visual Studio project to use qmake?

Its all written down step-by-step in this document:

  http://doc.qt.nokia.com/solutions/4/qtwinmigrate/winmigrate-walkthrough.html

From paragraph "Using the Qt build system"

  "Running qmake -tp vc on the .pro file will generate a new .dsp file that we can use in Visual Studio to compile and link the application."

Author

Commented:
So once I've done qmake from the command line I will never have to do that again?
>> So once I've done qmake from the command line I will never have to do that again?

That depends, if you want to add new ui-files then it might be easier to use qmake. You can make it yourself too by editing the dsp/vcproj files. There is no right or wrong. Depends on what you like better.

Author

Commented:
I've got it building the .ui files automatically bu using Zoppo's instructions so I'm going to leave it like that.

Hopefully, I will only need to run qmake once for each project (we have lots of projects). Doing this every time we compile will be mental so I'd like to avoid that.
No that was a misunderstanding. You dont need to do it each time you compile.

However, if you want to add new Qt-Windows/-Dialogs then it might be easier to use qmake.

Author

Commented:
Cheers. Just building now so I'll see how I get on!

Author

Commented:
Hmm, it's sort of worked only now I've lost all the project preferences we needed to compile everything else in the project.

Can you use qmake to just add the relevant Qt stuff to a project and not rebuild it from scratch which is what it seems it's doing.

No Qt compile errors though, which is great.
You have to add all important stuff from the old projecxt to the .pro file of course, so qmake will put it again into the visual studio project files. Or you copy it by hand from the old project files.

Author

Commented:
Thought so, damn!

Getting there, thanks...
Hey rix, my suggestion using qmake worked for the asker, see the end of the thread :)

ike
Oh forgot, meant to say suggestin points split :)

Author

Commented:
Apologies, I've been really busy so forgot all about this.

The step by step walkthrough wasn't totally perfect, even following exactly it screwed up halfway down and I was left with a project that couldn't compile.

The custom build rules solution did a lot of what I needed too.

Author

Commented:
That solution solved a lot of my problems but the step by step guide referenced too doesn't work fully as you end up with compile errors half way through.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial