【问题标题】:Cmake link issue: undefined reference to Button::mousePressEvent(QGraphicsSceneMouseEvent*)Cmake 链接问题:未定义对 Button::mousePressEvent(QGraphicsSceneMouseEvent*) 的引用
【发布时间】:2018-10-25 17:49:31
【问题描述】:

我正在尝试使用 cmake 编译一组 Qt 项目。它一直有效,直到我添加了 GUI 部分。 main.cpp:

#include <QtWidgets> 

int start_GUI(int argc, char *argv[]){
    QApplication a(argc, argv); 
    Window * window = new Window();
    SSConnection *ssc = new SSConnection();
    Window window;
    return a;
}

cmake:

cmake_minimum_required(VERSION 3.6)   
project(client)              
set(CMAKE_CXX_STANDARD 11)          
set(SOURCE_FILES  main.cpp)

find_package(Qt5Widgets REQUIRED)
find_package(Qt5WebSockets REQUIRED)
find_package(Qt5Gui REQUIRED)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

add_executable(client_main ${SOURCE_FILES}) 

qt5_use_modules(client_main Widgets Gui WebSockets)

输出:

undefined reference to `Button::mousePressEvent(QGraphicsSceneMouseEvent*)'
undefined reference to `non-virtual thunk to Button::mousePressEvent(QGraphicsSceneMouseEvent*)'

Button 类只是一个 QObject,用于在屏幕上添加一个按钮 按钮:

class Button : public QObject , public QGraphicsRectItem
{
Q_OBJECT
public:
    Button(QString name, QGraphicsItem * parent = NULL);
    void mousePressEvent(QGraphicsSceneMouseEvent * event);

signals:
    void clicked();
private:
    QGraphicsTextItem * text;
    QPushButton * button;
    QGraphicsRectItem * rect;
};

关于如何修复链接错误的任何建议

【问题讨论】:

  • 什么是Button?,请提供minimal reproducible example
  • 你试过用 qmake 运行它吗?你有没有清理过它并重建?
  • 这是一个在 QtCreator 中创建的项目,因此可以使用 qmake 运行。

标签: c++ qt cmake


【解决方案1】:

要解决链接器错误,请实现此方法或将其删除。

void mousePressEvent(QGraphicsSceneMouseEvent * event);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 2010-09-13
    • 1970-01-01
    • 2020-10-08
    • 2016-06-10
    • 2021-10-14
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多