唉,这是一个令人尴尬的愚蠢问题:
环境:Qt 5.1 Qt Creator 2.8 OSX 1.7.5;
如何通过Qt Creator C ++向导创建QLabel的子类?
具体来说,base class
和type information
是什么?我已经尝试了三种不同的方式,并最终遇到各种编译错误(如下)。 Qt一直运行良好,所以看起来不像是安装问题所以它必须是......用户问题。
答案:基类只有QLabel
,类型信息为QWidget
显然我还有其他一些问题导致编译错误。
如果我将类型信息设置为QWidget
或None
,我会收到错误
未找到架构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.
答案 0 :(得分:2)
我不知道你怎么能从Qt向导那里做到这一点,
但如果您更喜欢代码而不是如何完成代码,
//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);