我正在使用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。
掌握
#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
最佳答案
“ ui_MasterUI.h”中没有Ui :: MasterUI类,最接近的类是Ui :: masterWidget,因此会出现错误。
我猜想您在Qt Designer中或通过编辑MasterUI.ui将MasterUI重命名为masterWidget。这将导致Qt使用Ui :: masterWidget类而不是Ui :: MasterUI生成“ ui_MasterUI.h”。解决方案是将masterWidget重命名回MasterUI,或将masterui.cpp和masterui.h更改为引用Ui :: masterWidget而不是Ui :: MasterUI。
您确定相同的源文件仍然可以用qmake编译吗?
关于c++ - Qt4/C++/CMake-没有合适的默认构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25101540/