Important: Please read the Qt Code of Conduct -

[Solved] Moc file that does not get created

  • This a bit baffling to a new comer such as myself.

    We have this fairly large application that relies on Qt for interfacing. The idea is to convert it to static for distribution (it seems some dll's conflic with those of other tools for a shared distribution).
    Compiling Qt (version 4.5.2 used) as static went OK, but now, when trying to build the complete tool itself (using MS Visual Studio 2005, on Windows XT) we have one of the file, internally captioned as having been generated by the User Interface Compiler (the header of the file in question PrintersDialog.cpp reads:

    ** Form implementation generated from reading ui file 'printersdialog.ui'
    ** Created: Tue Mar 4 14:37:34 2008
    ** by: The User Interface Compiler ($Id: qt/main.cpp 3.3.0b1 edited Nov 24 13:47 $)
    ** WARNING! All changes made in this file will be lost!

    as can be seen, this file is already 4 year old, and there is little hope I can locate the person who triggered its generation)

    failing to generate, the seemingly required moc_PrintersDialog.cpp is missing.

    If one starts by building a shared version of the program, a rather tedious process since then a shared version of Qt needs to be brought on line, the moc file is created along with the other 12 that pop up; and if that moc file is then added to the source code for the static build -- which does generate those other 12 moc files -- then comppilation proceeds properly and we do get our executable.

    So, the question is:

    • what is preventing the moc_PrintersDialog.cpp file form being created, and what can we do to make sure it is generated when needed in a static build?

    Best regards


  • That file is generated by moc, the meta object compiler, a tool bundled with Qt.

    You can call it on the command line manually:

    moc -o moc_file.cpp file.h

    Or put that somewhere into your VS project.

  • I will sure give this a try, as soon as I figure where to add the instruction, so thanks.

    Still, that is a work around, and I wonder why "moc" does it for the other files and not that one when in static. Does this mean there must be some of those "moc" commands that are part of the VS project, one of which is controlled and inhibited by a variable that is related to the build being static?



  • Moderators

    Does the PrintersDialog.h header file have a Q_OBJECT macro in it?

  • That is the whole file here:


    #include <QObject>
    #include <qdialog.h>

    class Q3VBoxLayout;
    class Q3HBoxLayout;
    class QGridLayout;
    class QSpacerItem;
    class QPushButton;
    class Q3ListBox;
    class Q3ListBoxItem;
    class QPrinter;
    class QLineEdit;

    class CPrintersDialog : public QDialog

    CPrintersDialog( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, Qt::WFlags fl = 0 );
    void print();

    public slots:
    bool getSelectedPrinterName(QString& name) {name = selectedPrinter; return printerSelected;}

    Q3HBoxLayout* layout5;
    QSpacerItem* spacer5;
    QPushButton* okButton;
    QPushButton* cancelButton;
    Q3ListBox* lstPrinters;
    QString selectedPrinter;
    bool printerSelected;

    protected slots:
    virtual void languageChange();
    virtual void printerClicked(Q3ListBoxItem* item);

    #endif // PRINTERSDIALOG_H@


  • If you want visual studio to handle it, right click on the header file in solution explorer and select properties. In the properties dialog select under Item Type, custom build tool. Then fill in the fields as required. In the command line part, you want something like what volker suggested.

    @"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles$(ConfigurationName)\moc_%(Filename).cpp "-I$(QTDIR)\include\QtGui"" @

    Or something like that but specific to your project.

  • Ah ha!

    Thanks to you people clues and pointers, I was able to track down something that was different in the way the build step was defined for the PrintersDialog.h file. Instead of having


    for output as all the other header files associated with 'moc', it was missing the "$(InputDir) part when compiling a shared solution.
    Moreover, when attempting to compile a static one, it was entirely missing its custom build step. Filling those fields allowed for the generation of the required moc file. To coin a really bad pun, making it part of the solution made it no longer part of the problem.

    I only hope one day to be as knoweldgable as you folks so that I could eventually pay it forward. In the mean time, the fine people on this forum have my gratitude.

    Problem solved.

  • Moderators

    Glad you got your problem taken care of! Please be sure and edit the first post and change the thread title to add [Solved]. Thanks!

Log in to reply