简体   繁体   中英

Why does Qt Creator generate projects that won't compile?

I'm not new to C++ or programming in general, but this is my first attempt at using Qt. When I create a new project it makes this mainwindow.cpp file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
        delete ui;
    }

Without touching that file and just making some changes in the ui file with the designer, then attempting to build the project for a preview, I get two "invalid use of incomplete type" issues for the lines ui(new Ui::MainWindow) and ui->setupUi(this); .

From what I understand, the problem is that it's trying to reference MainWindow before the class is instantiated. But Creator generated that code itself. Is that normal for a new Qt project? Did I skip a step somewhere? More importantly, what is the best way to fix that problem?

Why does Qt Creator generate projects that won't compile?

It doesn't. You do. You even admitted to as much.

just making some changes in the ui file

You've answered your own question. You broke the .ui file.

But Creator generated that code itself.

Not really. The ui_mainwindow.h that the generated code depends on is derived from the .ui file. From the point of view of the mainwindow.cpp , the contents of the .ui file are just as important.

You cannot arbitrarily choose what you call code and what you don't. All of the pre-generated files, including the .ui file, comprise the code of your project.

You're now complaining that somehow Qt Creator generates useless code when you've just admitted that you changed said code! .

Most likely you've changed the name of the class within the .ui file, so that there's no declaration of the Ui::MainWindow in ui_mainwindow.h . Recall that the ui_mainwindow.h file is generated from the .ui file, and by changing the .ui file you can make the content of the other files (eg the .cpp file) incorrect: they must all refer to consistent class name.

You can easily verify that by clicking on the "ui_mainwindow.h" string and pressing F2 in Qt Creator. It will open the header file, that you can now inspect to determine what classes (if any) it declares. Most likely, that file, directly derived from your now changed .ui file, fails to declare the class that the "unchanged" .cpp file depends on.

It is entirely on you to maintain the dependent files to keep the project coherent. It is normal and fully expected for changes to one file (eg the .ui file) to break compilation of other files if you don't propagate the necessary changes (eg class and member names from .ui to other files).

Before you do anything else, though, please delete the build folder and build the project again. If the error persists, investigate what you broke in the .ui file.

In the future, always check-in any auto-generated project immediately into version control, so that you'll be more aware of what changes you've made to the code if things break. Initializing a git repository is a trivial operation, and there's excellent tooling and know-how available to make it an easy experience. If you're not using version control from the get-go, you're doing it wrong.

In addition to the great explanation by Kuba Ober :

I just want to add that the name of the class generated by the User Interface Compiler in the header ui_*.h (in your case it is ui_mainwindow.h ) can be controlled from the designer by changing the objectName property of the main window:

Qt Designer屏幕截图

Most likely, You have changed that into something else. This will cause inconsistency between your code (written in the files mainwindow.h , mainwindow.cpp ) and the auto-generated code from the UIC (in the file ui_mainwindow.h ). Where the former continue to refer to the class as MainWindow while the latter had that name changed.

And since Ui::MainWindow is forward declared in mainwindow.h , it becomes an incomplete type (and hence the error you get). because its definition (generated in ui_mainwindow.h ) is done to different name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM