我试图通过功能按setLayout设置小部件的布局,我收到错误消息:

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl LayoutManager::LayoutManager(void)" (??0LayoutManager@@QEAA@XZ) referenced in function main

main.obj:-1: error: LNK2019: unresolved external symbol "public: class QVBoxLayout * __cdecl LayoutManager::setHelloLayout(void)" (?setHelloLayout@LayoutManager@@QEAAPEAVQVBoxLayout@@XZ) referenced in function main

main.cpp
int main(int argc, char *argv[]){
    QApplication app(argc, argv);
    MainWindow mWin; //main widget
    LayoutManager *LayMan = new LayoutManager();
    mWin.setLayout(LayMan->setHelloLayout());

    mWin.show();

    return app.exec();
}

layoutmanager.h
class LayoutManager : public MainWindow
{
    Q_OBJECT

    void (*set_Lo_Pt[LAST_LAYOUT])(MainWindow&);

public:
    LayoutManager();
    ~LayoutManager();

    QVBoxLayout* setHelloLayout();

};

这是setHelloLayout()函数
QVBoxLayout* LayoutManager::setHelloLayout(){


    QVBoxLayout *menuOptions = new QVBoxLayout();

    QPushButton *but_HouseManager = new QPushButton("HouseManager");
    QPushButton *but_Help = new QPushButton("Help");
    QPushButton *but_Quit = new QPushButton("Quit");


    menuOptions->addWidget(but_HouseManager);
    menuOptions->addWidget(but_Help);
    menuOptions->addWidget(but_Quit);

    return menuOptions;

}

类MainWindow派生自QWidget

功能本体有问题还是我应该更改整个布局更改系统?

这是编译输出
21:18:33: Running steps for project HM...
21:18:33: Configuration unchanged, skipping qmake step.
21:18:33: Starting: "D:\Qt\Tools\QtCreator\bin\jom.exe"
    D:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
    link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST:embed /OUT:debug\HM.exe @C:\Users\MICHA~1\AppData\Local\Temp\HM.exe.4644.15.jom
main.obj : error LNK2019: unresolved external symbol "public: __cdecl LayoutManager::LayoutManager(void)" (??0LayoutManager@@QEAA@XZ) referenced in function main
main.obj : error LNK2019: unresolved external symbol "public: class QVBoxLayout * __cdecl LayoutManager::setHelloLayout(void)" (?setHelloLayout@LayoutManager@@QEAAPEAVQVBoxLayout@@XZ) referenced in function main
debug\HM.exe : fatal error LNK1120: 2 unresolved externals
jom: D:\Qt\build-HM-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug\Makefile.Debug [debug\HM.exe] Error 1120
jom: D:\Qt\build-HM-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug\Makefile [debug] Error 2
21:18:33: The process "D:\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project HM (kit: Desktop Qt 5.5.1 MSVC2013 64bit)
When executing step "Make"
21:18:33: Elapsed time: 00:00.

这是.pro文件
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = HM
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    layoutmanager.cpp

HEADERS  += mainwindow.h \
    layoutmanager.h

最佳答案

我在.pro文件中添加了一些模块,例如多媒体,快速,小部件,然后对其进行了构建并且可以正常工作。我不知道它是如何工作的,因为在那之后我删除了一个接一个的模块,并且当我再次构建没有这些模块的模块时,它仍然可以正常工作。对我来说,这似乎是个错误(我正在QTCreator中做所有事情),或者我只是不了解编译器的工作原理。

关于c++ - QT 5.5 setLayout()通过返回值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35901905/

10-12 21:33