简体   繁体   中英

How to compile Qt 5 projects without QtCreator?

I tried to modify KDevelop Qt 4 CMake project to be compile it with Qt 5 and changed the project, but I failed. I'm still learning Qt and the project's simple. Commented lines are replaced to make it a Qt 5 project.

main.cpp:

//#include <QtGui/QApplication>
#include <qt5/QtWidgets/QApplication>
#include "Qt4_Training.h"

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    Qt4_Training qt4_training;
    qt4_training.show();
    return app.exec();
}

Qt4_Training.cpp:

#include "Qt4_Training.h"

/*#include <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QtGui/QPushButton>
#include <QTime>*/
#include <qt5/QtWidgets/QLabel>
#include <qt5/QtWidgets/QMenu>
#include <qt5/QtWidgets/QMenuBar>
#include <qt5/QtWidgets/QAction>
#include <qt5/QtWidgets/QPushButton>
#include <qt5/QtCore/QTime>

Qt4_Training::Qt4_Training()
{
    QLabel *label = new QLabel;
    vbox = new QVBoxLayout;

    label.setText("Hi.");
    vbox->addWidget(label);
    QWidget *a = new QWidget;
    a->setLayout(vbox);
    setCentralWidget(a);

    QAction* action = new QAction(this);
    action->setText( "Quit");
    connect(action, SIGNAL(triggered()), SLOT(close()));
    menuBar()->addMenu( "File" )->addAction(action);
}

Qt4_Training::~Qt4_Training() {}

//#include "Qt4_Training.moc"

Qt4_Training.h:

#ifndef Qt4_Training_H
#define Qt4_Training_H

/*#include <QtGui/QMainWindow>
#include <QtGui/QVBoxLayout>*/
#include <qt5/QtWidgets/QMainWindow>
#include <qt5/QtWidgets/QVBoxLayout>

class Qt4_Training : public QMainWindow
{
    //Q_OBJECT

public:
    QVBoxLayout *vbox;
    Qt4_Training();
    virtual ~Qt4_Training();
};

#endif // Qt4_Training_H

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)
project(qt4_training)
#find_package(Qt4 REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)

#include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
include_directories(${Qt5Widgets_INCLUDE_DIRS} ${Qt5Widgets_LIBRARIES} ${CMAKE_CURRENT_BINARY_DIR} ${Qt5Core_INCLUDE_DIRS} ${Qt5Core_LIBRARIES})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")
set(Qt4_Training_SRCS Qt4_Training.cpp main.cpp)

#qt4_automoc(${Qt4_Training_SRCS})
add_executable(qt4_training ${Qt4_Training_SRCS})
target_link_libraries(qt4_training ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})

install(TARGETS qt4_training RUNTIME DESTINATION bin)

I also tried to use Eclipse and also used '#include ', but all objects names must end with 'Private' like 'QLabelPrivate ...;'.

KDevelop errors:

...
CMakeFiles/qt4_training.dir/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x37): undefined reference to `QApplication::QApplication(int&, char**, int)'
main.cpp:(.text.startup+0x47): undefined reference to `QWidget::show()'
main.cpp:(.text.startup+0x4c): undefined reference to `QApplication::exec()'
main.cpp:(.text.startup+0x5f): undefined reference to `QApplication::~QApplication()'
main.cpp:(.text.startup+0x96): undefined reference to `QApplication::~QApplication()'
collect2: error: ld returned 1 exit status
CMakeFiles/qt4_training.dir/build.make:110: recipe for target 'qt4_training' failed
make[2]: *** [qt4_training] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/qt4_training.dir/all' failed
make[1]: *** [CMakeFiles/qt4_training.dir/all] Error 2
Makefile:117: recipe for target 'all' failed
make: *** [all] Error 2
*** Failure: Exit code 2 ***

Eclipse errors:

In file included from ../src/Qt 5 Training.cpp:1:0:
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qapplication.h:37:37: fatal error: QtCore/qcoreapplication.h: No such file or directory
 #include <QtCore/qcoreapplication.h>
                                     ^
compilation terminated.
src/subdir.mk:21: recipe for target 'src/Qt 5 Training.o' failed
make: *** [src/Qt 5 Training.o] Error 1

I installed libqt5widgets5, qtdeclarative5-dev, libqt5gui5, qtbase5-dev,... on Kubuntu. So do I have Qt 5?

Check out the documentation for cmake-qt at http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html

According to that target_link_libraries(qt4_training Qt5::Widgets)

might fix your linker errors.

Your include directories also look wrong, you should not add LIBRARIES variables here

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