Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

unexpected error in a simple class



  • In my code I get an error about declaration and definition of a class
    Compilator says that error might be caused by tools Desktop Qt 5.10.1 MSVC2017 64bit however i reinstalled it and the eror still exists.
    .h
    class MyWidget : public QWidget
    {
    public: void paintEvent(QPaintEvent * );
    };
    .cpp
    class MyWidget: public QWidget
    {

    public:
    void paintEvent(QPaintEvent * )
    {
    QPainter painter(this);
    painter.setPen(Qt::black);
    painter.drawRect(100,100,100,100);
    }
    };



  • @KamilGie

    The whole error message, header and cpp-file would be nice.

    Try to add the Q_OBJECT makro.



  • unfortunately error still exists
    error C2011 process ended with code 2
    MIght be that the problem is bad configuration of Desktop Qt 5.10.1 MSVC2017 64bit
    While doing "Make".It is my translation from Polish

    .h file
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    //#ifndef Q_OBJECT
    //#define Q_OBJECT

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    };
    class MyWidget : public QWidget
    {
    public: void paintEvent(QPaintEvent * );

    };

    #endif // MAINWINDOW_H
    //#endif //Q_OBJECT

    .cpp file
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<QRect>
    #include<QPainter>
    #include<QWidget>
    #include<QtGui>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    MyWidget * mywidget = new MyWidget();
    setCentralWidget(mywidget);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    class MyWidget: public QWidget
    {

    public:
    void paintEvent(QPaintEvent * )
    {
    QPainter painter(this);
    painter.setPen(Qt::black);
    painter.drawRect(100,100,100,100);
    }

    };


  • @KamilGie as @beecksche says,
    you need to have a Q_OBJECT macro in your class, something like this.

    class MyWidget : public QWidget
    {
          Q_OBJECT
          public: void paintEvent(QpaintEvent *);
    };
    


  • ok I tried it but error still exists



  • @KamilGie

    If you want to use the paintEvent() function you should override it in your header file:

    void paintEvent(QpaintEvent *) override;
    


  • @beecksche thanks for advise


  • Lifetime Qt Champion

    @KamilGie

    If your problem is solved, so please mark this tread as SOLVED also. Thanks.



  • @aha_1980 error still exists


  • Lifetime Qt Champion

    @KamilGie You can try to close QtCreator, delete your build directory and also the *.pro.user file to get a clean state. Then open Creator again and re-try.



  • @KamilGie

    Additionally to @aha_1980, try to clean the compiled files, run qmake again.

    I think the error message should have some more information about the origin of the error.

    Try the examples, if they're working you can search for differences in the example and your code.


Log in to reply