简体   繁体   English

如何用 QT 6,0 制作 window?

[英]How make a window with QT 6,0?

Hi to everyone Im beguinner with QT 6.0 Im started to practice with but I get this error message大家好,我开始使用 QT 6.0 我开始练习,但我收到此错误消息

C:\Users\blabla\Documents\Programming\VentanaPrincipal\main.cpp:6: error: undefined reference to `__imp__ZN7QWidgetC1EPS_6QFlagsIN2Qt10WindowTypeEE'
CMakeFiles/VentanaPrincipal.dir/main.cpp.obj: In function `main':
C:/Users/blabla/Documents/Programming/VentanaPrincipal/main.cpp:6: undefined reference to `__imp__ZN7QWidgetC1EPS_6QFlagsIN2Qt10WindowTypeEE'

literally this is the code what I made and As well I'm saw at https://doc.qt.io/qt-5/qtwidgets-tutorials-widgets-toplevel-example.html从字面上看,这是我所做的代码,我也在https://doc.qt.io/qt-5/qtwidgets-tutorials-widgets-toplevel-example.ZFC335EZ883A2

#include <QCoreApplication>
#include <QtWidgets/QtWidgets>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QWidget window;

    window.resize(320,240);
    window.show();

    return a.exec();
}

Based on comments to your question, your CMakeLists.txt file has this for the library requirements:根据对您问题的评论,您的CMakeLists.txt文件具有以下库要求:

 target_link_libraries(VentanaPrincipal Qt${QT_VERSION_MAJOR}::Core) 

While core brings in a large chunk of Qt , it does not bring in everything.虽然core带来了大量的Qt ,但它并没有带来一切。 In particular, if you want to use widgets, you'll need Qt*::Widgets as well.特别是,如果您想使用小部件,您还需要Qt*::Widgets


A good way to ensure you have everything you need is to look at the documentation for the classes you wish to use.确保您拥有所需的一切的好方法是查看您希望使用的类的文档。 For example, the QWidget documentation page (for Qt5, which is what I use) has this in the table at the top:例如, QWidget 文档页面(对于 Qt5,这是我使用的)在顶部的表格中有这个:

+-----------+--------------------+
| Header:   | #include <QWidget> |
+-----------+--------------------+
| qmake:    | QT += widgets      |
+-----------+--------------------+

That means you need to include the QWidget header file and link with the widgets library.这意味着您需要包含QWidget header 文件widgets库链接。 Were you using qmake , that would simply involve putting that exact line QT += widgets into your project file.如果您使用qmake ,只需将确切的行QT += widgets放入您的项目文件中。

The equivalent in cmake would result in the following changes to your CMakeLists.txt file: cmake中的等效项将导致您的CMakeLists.txt文件发生以下更改:

target_link_libraries(VentanaPrincipal
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Widgets
)

The equivalent Qt6 documentation page is even better since it now shows the cmake stuff as well, meaning you don't need to think about translating the qmake variant:等效的 Qt6 文档页面更好,因为它现在也显示cmake内容,这意味着您无需考虑翻译qmake变体:

+-----------+------------------------------------------------------+
| Header:   | #include <QWidget>                                   |
+-----------+------------------------------------------------------+
| CMake:    | find_package(Qt6 COMPONENTS Widgets REQUIRED)        |
|           | target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
+-----------+------------------------------------------------------+
| qmake:    | QT += widgets                                        |
+-----------+------------------------------------------------------+

How about using QApplicacion instead of QCoreApplication?使用 QApplicacion 代替 QCoreApplication 怎么样?

QApplication a(argc, argv);

You also don't need cmake.您也不需要 cmake。 You should create simple project, and use qmake to generate makefile.您应该创建一个简单的项目,并使用 qmake 生成 makefile。

Chcek this thread: Simple Qt program builds but doesn't show output .检查这个线程: 简单的 Qt 程序构建但不显示 output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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