1

唉,这是一个令人尴尬的愚蠢问题:

环境:Qt 5.1 Qt Creator 2.8 OSX 1.7.5;

如何通过 Qt Creator C++ 向导创建 QLabel 的子类?

具体来说,什么是base classand type information?我已经尝试了三种不同的方法,最终出现了各种编译错误(如下)。Qt 一直运行良好,所以看起来不像是安装问题,所以它一定是……用户问题。

回答:基类是 just QLabel,类型信息是QWidget 显然我遇到了其他一些导致编译错误的问题。


如果我将类型信息设置为QWidgetNone我收到错误

未找到体系结构 x86_64 的符号

如果我将类型信息设置为QObject我收到错误:

从“QObject*”到“QWidget*”的无效转换

更新

01:25:29: Running steps for project LabelImageTest...
01:25:29: Configuration unchanged, skipping qmake step.
01:25:29: Starting: "/usr/bin/make" 
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.6 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib -o mainwindow.o /Users/rise/QT_workspace/LabelImageTest/mainwindow.cpp
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.6 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib -o customlabel.o /Users/rise/QT_workspace/LabelImageTest/customlabel.cpp
/Users/rise/Qt/5.1.0/clang_64/bin/rcc -name images /Users/rise/QT_workspace/LabelImageTest/images.qrc -o qrc_images.cpp
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.6 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib -o qrc_images.o qrc_images.cpp
/Users/rise/Qt//5.1.0/clang_64/bin/moc -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -D__APPLE__ -D__GNUC__=4 -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib /Users/rise/QT_workspace/LabelImageTest/mainwindow.h -o moc_mainwindow.cpp
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.6 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib -o moc_mainwindow.o moc_mainwindow.cpp
/Users/rise/Qt//5.1.0/clang_64/bin/moc -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -D__APPLE__ -D__GNUC__=4 -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib /Users/rise/QT_workspace/LabelImageTest/customlabel.h -o moc_customlabel.cpp
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -c -pipe -g -gdwarf-2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.6 -Wall -W -fPIE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/Users/rise/Qt/5.1.0/clang_64/mkspecs/macx-g++ -I/Users/rise/QT_workspace/LabelImageTest -I/Users/rise/Qt/5.1.0/clang_64/include -I/Users/rise/Qt/5.1.0/clang_64/include/QtWidgets -I/Users/rise/Qt/5.1.0/clang_64/lib/QtWidgets.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtGui -I/Users/rise/Qt/5.1.0/clang_64/lib/QtGui.framework/Headers -I/Users/rise/Qt/5.1.0/clang_64/include/QtCore -I/Users/rise/Qt/5.1.0/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/AGL.framework/Headers -I. -I. -F/Users/rise/Qt//5.1.0/clang_64/lib -o moc_customlabel.o moc_customlabel.cpp
/Applications/Xcode.app/Contents/Developer/usr/bin/g++ -headerpad_max_install_names -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -mmacosx-version-min=10.6 -o LabelImageTest.app/Contents/MacOS/LabelImageTest main.o mainwindow.o customlabel.o qrc_images.o moc_mainwindow.o moc_customlabel.o   -F/Users/rise/Qt//5.1.0/clang_64/lib -framework QtWidgets -F/Users/rise/Qt//5.1.0/clang_64/qtbase/lib -framework QtGui -framework QtCore -framework OpenGL -framework AGL 
ld: warning: directory not found for option '-F/Users/rise/Qt//5.1.0/clang_64/qtbase/lib'
Undefined symbols for architecture x86_64:
  "MainWindow::on_label_linkActivated(QString const&)", referenced from:
      MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)in moc_mainwindow.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [LabelImageTest.app/Contents/MacOS/LabelImageTest] Error 1
01:25:34: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project LabelImageTest (kit: QT 5.1)
When executing step 'Make'
01:25:34: Elapsed time: 00:05.

在此处输入图像描述

4

1 回答 1

2

我不确定你如何从 Qt Wizard 中做到这一点,

但是,如果您更喜欢代码而不是遵循它的完成方式,

//in header file
#include <QLabel>
class MyLabel: public QLabel{
   Q_OBJECT
public:
   MyLabel( QWidget* parent=0);
   //other methods
};

//in cpp file
MyLabel::MyLabel( QWidget* parent) 
    : QLabel(parent)
{
}

您可以通过以下代码创建其实例

MyLabel* label = new MyLabel();
//layout->addWidget(label);
于 2013-08-27T04:00:15.010 回答