Qt4 / C ++ / CMake - 没有合适的默认构造函数可用

时间:2014-08-03 04:07:12

标签: c++ qt qt4 cmake qt-creator

我使用的是Qt Creator 3.1.2 / Qt4.8.6和CMake3.0.0。请注意,如果您关注我的CMakeLists.txt,我会使用SceniX API,但这并不会影响这一点。但是,在编译Qt Creator生成的UI时,我收到以下错误:

D:\Projects\C Projects\MyProjectName\ui\masterui.cpp:6: error: C2512: 'Ui::MasterUI' : no appropriate default constructor available

但是,默认构造函数IS可用。这些是Qt Creator生成的默认文件,用于CMake。在试验时它们可能会略微修改,但它们从未起作用。

我将按顺序分享我的masterui.h / masterui.cpp / CMakeLists.txt。

masterui.h

#ifndef MASTERUI_H
#define MASTERUI_H

#include <QWidget>

namespace Ui {
class MasterUI;
}

class MasterUI : public QWidget
{
    Q_OBJECT

public:
    explicit MasterUI(QWidget *parent = 0);
    ~MasterUI();

private:
    Ui::MasterUI *ui;
};

#endif // MASTERUI_H

masterui.cpp

#include "masterui.h"
#include "ui_masterui.h"

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

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

的CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

project(MyProjectName)

file(TO_CMAKE_PATH $ENV{SCENIXHOME} SCENIX_HOME)
set(CMAKE_MODULE_PATH "${SCENIX_HOME}/CMake")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../exe/MyProjectName_exe")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "../exe/MyProjectName_exe")

file(GLOB sources
        "src/*.cpp")

file(GLOB headers
        "inc/*.h")

find_package(OpenGL REQUIRED)
find_package(SceniX REQUIRED)
find_package(NVQt4 QUIET)
find_package(Qt4 REQUIRED)

set( QT_USE_QTOPENGL TRUE )
include( ${QT_USE_FILE} )

SCENIX_SET_BINARY_FOLDERS()

QT4_ADD_RESOURCES(QTResources)

QT4_WRAP_UI(UIsrc "ui/masterui.ui")
QT4_WRAP_CPP(MOCsrc "ui/masterui.h")
ADD_EXECUTABLE(UIexe "src/main.cpp" "ui/masterui.cpp" ${MOCsrc} ${UIsrc})
file( GLOB UICsources
        "${CMAKE_CURRENT_BINARY_DIR}/*.h")
set( UIC_SOURCES "${UICsources}")

set( SCENIX_COMMON_BASE "${SCENIX_HOME}/samples/common" )

file( GLOB uisources
      "${SCENIX_COMMON_BASE}/src/ui/*.cpp" )

set( UI_SOURCES "${uisources}" )

set( MyProjectName_SOURCES
  ${sources}
  ${SCENIX_COMMON_BASE}/src/SimpleScene.cpp
  ${SCENIX_COMMON_BASE}/src/MeshGenerator.cpp
  ${SCENIX_COMMON_BASE}/src/SceneFunctions.cpp
  ${SCENIX_COMMON_BASE}/src/SceniXWidget.cpp
  ${SCENIX_COMMON_BASE}/Qt4/src/SceniXQGLContext.cpp
  ${SCENIX_COMMON_BASE}/Qt4/src/SceniXQGLWidget.cpp
  ${SCENIX_COMMON_BASE}/Qt4/src/SceniXQGLSceneRendererWidget.cpp
  ${UI_SOURCES}
  ${UIC_SOURCES}
  ${QTResources}
  ${UICsources}
)

set ( MyProjectName_HEADERS
  ${headers}
  ${SCENIX_COMMON_BASE}/inc/SimpleScene.h
  ${SCENIX_COMMON_BASE}/inc/MeshGenerator.h
  ${SCENIX_COMMON_BASE}/inc/SceneFunctions.h
  ${SCENIX_COMMON_BASE}/inc/SceniXWidget.h
  ${SCENIX_COMMON_BASE}/Qt4/inc/SceniXQGLContext.h
  ${SCENIX_COMMON_BASE}/Qt4/inc/SceniXQGLWidget.h
  ${SCENIX_COMMON_BASE}/Qt4/inc/SceniXQGLSceneRendererWidget.h
)

include_directories( ${SCENIX_INCLUDES} )
include_directories( ${SCENIXRT_INCLUDES} )
include_directories( "." )
include_directories( "inc" )
include_directories( "${SCENIX_COMMON_BASE}/inc" )
include_directories( "${SCENIX_COMMON_BASE}/Qt4/inc" )
include_directories( "${CMAKE_CURRENT_BINARY_DIR}" )

add_executable( MyProjectName
  ${MyProjectName_SOURCES}
  ${MyProjectName_HEADERS}
)

target_link_libraries( MyProjectName
  ${QT_LIBRARIES}
  ${SCENIX_LIBS}
  ${SCENIXRT_LIBS}
  ${OPENGL_gl_LIBRARY}
  ${QT_QTMAIN_LIBRARY}
)


if(TARGET SceniX)
  add_dependencies( MyProjectName SceniX SceniXRT )
  add_perforce_to_target( MyProjectName )
endif()

set_target_properties( MyProjectName PROPERTIES FOLDER "Samples")

ui_masterui.h

/********************************************************************************
** Form generated from reading UI file 'masterui.ui'
**
** Created by: Qt User Interface Compiler version 4.8.6
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_MASTERUI_H
#define UI_MASTERUI_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_masterWidget
{
public:

    void setupUi(QWidget *masterWidget)
    {
        if (masterWidget->objectName().isEmpty())
            masterWidget->setObjectName(QString::fromUtf8("masterWidget"));
        masterWidget->resize(640, 360);
        masterWidget->setStyleSheet(QString::fromUtf8("background-color: rgb(71, 71, 71);"));

        retranslateUi(masterWidget);

        QMetaObject::connectSlotsByName(masterWidget);
    } // setupUi

    void retranslateUi(QWidget *masterWidget)
    {
        masterWidget->setWindowTitle(QApplication::translate("masterWidget", "Form", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

};

namespace Ui {
    class masterWidget: public Ui_masterWidget {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_MASTERUI_H

1 个答案:

答案 0 :(得分:4)

&#34; ui_MasterUI.h&#34;中没有Ui :: MasterUI类,最接近的是Ui :: masterWidget,因此你得到错误。

我猜你在Qt Designer中将MasterUI重命名为masterWidget,或者编辑MasterUI.ui。这将导致Qt生成&#34; ui_MasterUI.h&#34;使用Ui :: masterWidget类,而不是Ui :: MasterUI。解决方案是将masterWidget重命名为MasterUI,或者更改masterui.cpp和masterui.h以引用Ui :: masterWidget而不是Ui :: MasterUI。

您确定相同的源文件仍可与qmake编译吗?