//mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsView> #include "graphicsscene.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT private: QGraphicsView *graph; QWidget *buttonsWidget; QWidget *statusWidget; CustomScene *scene; public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H //mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); graph = ui->graphicsView; // Initialize pointer for graphics view scene = new CustomScene(); // Π’ΡƒΡ‚ ошибка scene->setSceneRect(ui->graphicsView->rect()); // Set size and positon for scene scene->addLine(0,0,200,200); // Test graph->setScene(scene); // Set scene on graphics view statusWidget = ui->StatusWidget; // Initialize pointer for own widget "Status Widget" } MainWindow::~MainWindow() { delete scene; delete ui; } //graphicsscene.h #ifndef GRAPHICSSCENE_H #define GRAPHICSSCENE_H #include <QObject> #include <QGraphicsScene> #include <QGraphicsSceneMouseEvent> class CustomScene : public QGraphicsScene { Q_OBJECT public: explicit CustomScene(QObject *parent = 0); ~CustomScene(); signals: // Π‘ΠΈΠ³Π½Π°Π» для ΠΏΠ΅Ρ€Π΅Π΄Π°Ρ‡ΠΈ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ полоТСния курсора ΠΌΡ‹ΡˆΠΈ void signalTargetCoordinate(QPointF point); public slots: private: // Ѐункция, Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ производится отслСТиваниС полоТСния ΠΌΡ‹ΡˆΠΈ void mouseMoveEvent(QGraphicsSceneMouseEvent *event); }; #endif // GRAPHICSSCENE_H //graphicsscene.cpp #include "graphicsscene.h" CustomScene::CustomScene(QObject *parent) : QGraphicsScene() { Q_UNUSED(parent); } CustomScene::~CustomScene() { } void CustomScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { emit signalTargetCoordinate(event->scenePos()); } 

Gives an error message:

mainwindow.obj: -1: error: LNK2019: link to unresolved external character "public: __thiscall CustomScene :: CustomScene (class QObject *)" (?? 0CustomScene @@ QAE @ PAVQObject @@@ Z) in the function "public: __thiscall MainWindow :: MainWindow (class QWidget *) "(?? 0MainWindow @@ QAE @ PAVQWidget @@@ Z)`

and if you double click on it then writes Π€Π°ΠΉΠ» Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ Π²:mainwindow.obj .

If I mainwindow.h my CustomScene and defined it in mainwindow.cpp , there was no error, but how to make CustomScene in other files?

  • one
    When creating new QObject heirs, you need to restart the meta-object compilation. Usually, I stupidly delete the directories QMake caches, and that helps. - Tripolsky Peter
  • Clean and reassemble the project - Alexander Chernin
  • Usually, folders are created next to the current project directory with the name of the form build-qtcreator-Desktop_Qt_5_10_0_clang_64bit-Debug. Try to remove this. Or through the build menu in Qt Creator itself - Tripolsky Peter
  • one
    Error in IDE when clicking caused by the fact that it was written by people who do not need an IDE and they scored a bolt. As for the nature of the build error itself - do not forget that the signal / slot mechanism is syntactic sugar and cannot be compiled. It is expanded into C ++ code by the QMake utility, which appends files for you. Look at the folder - there are extraneous files moc_predefs.h, moc_mainwindow.cpp and so on. For the sake of saving time, she strives to generate as little code as possible and does not always succeed in putting it together - we have to let her do it with a new one. - Tripolsky Peter

2 answers 2

When creating new QObject heirs, you need to restart the meta-object compilation. Usually, folders are created next to the current project directory with the name of the form build-qtcreator-Desktop_Qt_5_10_0_clang_64bit-Debug. Try to remove this. Or through the build menu in Qt Creator itself

enter image description here

It seems that this can also be done from Qt Creator itself, but I always delete the folder with the meta-object code generated by the QMake utility.

enter image description here

    You have no definition of the CustomScene(QObject *) constructor CustomScene(QObject *) , more precisely, the declaration is and it is β€œvisible” in the MainWindow , but the definition (that is, the body) is not. Either you did not include the .cpp file in the project where the necessary constructor is defined, or in the existing constructors a different set of arguments.

    • That seems to be CustomScene::CustomScene(QObject *parent) : QGraphicsScene() { Q_UNUSED(parent); } definition In graphicsscene.cpp - Kaznachei
    • I removed QObject * arguments for the CustomScene constructor. Does not help - Kaznachei