Solved Migrating Qt.4.8 to Qt.5.8 errors
-
Hi all,
I'm migrating my desktop application from qt.4.8.6 version to qt.5.8.
I have the following code:
(file.h)
....
....
signals:void signal1();
#ifdef WIN32
void signal2();
#endifIn the generated moc file signal2 is not created (signal1 yes)
However, if the file is like:
....
....
signals:void signal1();
#ifndef APPLE
void signal2();
#endifIn the generated moc file signal2 IS CREATED
What is the difference?
-
Moc is not the compiler so it might not get the same preprocessors when it runs. can you try with
#ifdef Q_OS_WIN32
-
To add to @VRonin, for a list of all these macros see Global Qt Declarations.
-
Thanks for your answers.
I had already tried with Q_OS_WIN32, and it does not solve my problem.
Besides, I have defined other constants and i have the same problem. -
This is strange, you can pass -DWIN32 as an argument to moc but it shouldn't be necessary. What build system are you using?
-
I'm using cmake3.7.2, and compiling with visual studio 2015.
-
can you post your CMakeList?
-
Here is the code:
CMAKELIST.TXT:
cmake_minimum_required (VERSION 3.2) project (QT5Example) #----------------------------------------------------------------------------- # List of source files set (QT5EXAMPLE_SRCS QT5Example.cxx QT5ExampleDlg.cxx ) set (QT5EXAMPLE_HDRS QT5ExampleDlg.h ) #----------------------------------------------------------------------------- # List of moc'ables set (QT5EXAMPLE_MOC_HDRS QT5ExampleDlg.h ) #----------------------------------------------------------------------------- # The ui files set (QT5EXAMPLE_UIS QT5ExampleDlg.ui ) #----------------------------------------------------------------------------- # Find and use Qt libraries find_package (Qt5Core) set (QT_LIBS ${QT_LIBS} ${Qt5Core_LIBRARIES}) set (QT_INCLUDES ${QT_INCLUDES} ${Qt5Core_INCLUDE_DIRS}) find_package (Qt5Widgets) set (QT_LIBS ${QT_LIBS} ${Qt5Widgets_LIBRARIES}) set (QT_INCLUDES ${QT_INCLUDES} ${Qt5Widgets_INCLUDE_DIRS}) #----------------------------------------------------------------------------- # Set additional include directories in the project configuration. # Directories in binary tree are needed for generated ui and resource headers. # include_directories ( ${QT_INCLUDE_DIR} ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ) #----------------------------------------------------------------------------- # Generate ui sources for headers in Qt qt5_wrap_ui (QT5EXAMPLE_UI_HDRS "${QT5EXAMPLE_UIS}") #----------------------------------------------------------------------------- # Generate moc sources for headers in Qt qt5_wrap_cpp (QT5EXAMPLE_MOC_SRCS "${QT5EXAMPLE_MOC_HDRS}") add_definitions(-DQT_GUI_LIBS -DQT_CORE_LIB ) #----------------------------------------------------------------------------- # Group the generated files in different source groups so they appear together in the IDE source_group (Designer FILES ${QT5EXAMPLE_UIS} ) source_group (Generated FILES ${QT5EXAMPLE_MOC_SRCS} ${QT5EXAMPLE_UI_HDRS} ) add_executable (QT5Example ${QT5EXAMPLE_SRCS} ${QT5EXAMPLE_HDRS} ${QT5EXAMPLE_MOC_SRCS} ${QT5EXAMPLE_UIS} ${QT5EXAMPLE_UI_HDRS} ${QT5EXAMPLE_QM} ) target_link_libraries (QT5Example ${QT_LIBS} )
QT5EXAMPLE.CXX:
#include <QApplication> #include "QT5ExampleDlg.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QT5ExampleDlg* dlg = new QT5ExampleDlg(); dlg->show(); return app.exec(); }
QT5EXAMPLEDLG.H:
#ifndef __QT5ExampleDlg_h #define __QT5ExampleDlg_h #include "ui_QT5ExampleDlg.h" #include <QDialog> class QT5ExampleDlg : public QDialog { Q_OBJECT public: QT5ExampleDlg(QWidget *parent = 0, Qt::WindowFlags flags = 0); ~QT5ExampleDlg(); signals: void message1(); #ifdef Q_OS_WIN void message2(); #endif public slots: void OnPushButton1(); void OnPushButton2(); void OnMessage1(); void OnMessage2(); private: Ui::QT5ExampleDlgUI ui; }; #endif // __QT5ExampleDlg_h
QT5EXAMPLEDLG.CXX:
#include "QT5ExampleDlg.h" QT5ExampleDlg::QT5ExampleDlg(QWidget *parent, Qt::WindowFlags flags) : QDialog(parent, flags) { ui.setupUi(this); QObject::connect(ui.pushButton1, SIGNAL(clicked()), this, SLOT(OnPushButton1())); QObject::connect(ui.pushButton2, SIGNAL(clicked()), this, SLOT(OnPushButton2())); QObject::connect(this, SIGNAL(message1()), this, SLOT(OnMessage1())); QObject::connect(this, SIGNAL(message2()), this, SLOT(OnMessage2())); } QT5ExampleDlg::~QT5ExampleDlg() { } void QT5ExampleDlg::OnPushButton1() { emit message1(); } void QT5ExampleDlg::OnPushButton2() { #ifdef Q_OS_WIN emit message2(); #endif } void QT5ExampleDlg::OnMessage1() { ui.simpleLineEdit->setText("message1 emitted"); } void QT5ExampleDlg::OnMessage2() { ui.simpleLineEdit->setText("message1 emitted"); }
QT5EXAMPLEDLG.UI:
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>QT5ExampleDlgUI</class> <widget class="QWidget" name="QT5ExampleDlgUI"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>246</width> <height>149</height> </rect> </property> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="maximumSize"> <size> <width>300</width> <height>16777215</height> </size> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QVBoxLayout"> <property name="spacing"> <number>6</number> </property> <property name="margin"> <number>9</number> </property> <item> <widget class="QGroupBox" name="SimpleGroupBox"> <property name="maximumSize"> <size> <width>250</width> <height>200</height> </size> </property> <property name="title"> <string>Simple Groupbox</string> </property> <layout class="QVBoxLayout"> <property name="spacing"> <number>6</number> </property> <property name="margin"> <number>9</number> </property> <item> <layout class="QHBoxLayout"> <property name="spacing"> <number>6</number> </property> <property name="margin"> <number>0</number> </property> <item> <widget class="QLabel" name="SimpleLabel"> <property name="text"> <string>Simple Label</string> </property> </widget> </item> <item> <spacer> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>100</width> <height>30</height> </size> </property> </spacer> </item> <item> <widget class="QLineEdit" name="simpleLineEdit"> <property name="enabled"> <bool>false</bool> </property> <property name="minimumSize"> <size> <width>50</width> <height>0</height> </size> </property> <property name="maximumSize"> <size> <width>150</width> <height>20</height> </size> </property> </widget> </item> <item> <spacer> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> </layout> </item> <item> <layout class="QHBoxLayout"> <property name="spacing"> <number>6</number> </property> <property name="margin"> <number>0</number> </property> <item> <widget class="QPushButton" name="pushButton1"> <property name="minimumSize"> <size> <width>100</width> <height>25</height> </size> </property> <property name="maximumSize"> <size> <width>100</width> <height>16777215</height> </size> </property> <property name="text"> <string>Text1</string> </property> </widget> </item> </layout> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QPushButton" name="pushButton2"> <property name="minimumSize"> <size> <width>100</width> <height>25</height> </size> </property> <property name="maximumSize"> <size> <width>100</width> <height>16777215</height> </size> </property> <property name="text"> <string>Text2</string> </property> </widget> </item> </layout> </item> </layout> </widget> </item> </layout> </widget> <resources/> <connections/> </ui>
-
You are using a recent version of CMake, see http://doc.qt.io/qt-5/cmake-manual.html
cmake_minimum_required (VERSION 3.2) project (QT5Example) set(CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) find_package (Qt5Core) find_package(Qt5Widgets)
-
Thanks, now it's working.