Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. unexpected error in a simple class

unexpected error in a simple class

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KamilGie
    wrote on last edited by
    #1

    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);
    }
    };

    beeckscheB K 2 Replies Last reply
    0
    • K KamilGie

      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);
      }
      };

      beeckscheB Offline
      beeckscheB Offline
      beecksche
      wrote on last edited by beecksche
      #2

      @KamilGie

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

      Try to add the Q_OBJECT makro.

      1 Reply Last reply
      1
      • K Offline
        K Offline
        KamilGie
        wrote on last edited by KamilGie
        #3

        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);
        }

        };
        
        1 Reply Last reply
        0
        • K KamilGie

          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);
          }
          };

          K Offline
          K Offline
          kenchan
          wrote on last edited by kenchan
          #4

          @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 *);
          };
          
          1 Reply Last reply
          1
          • K Offline
            K Offline
            KamilGie
            wrote on last edited by
            #5

            ok I tried it but error still exists

            beeckscheB 1 Reply Last reply
            0
            • K KamilGie

              ok I tried it but error still exists

              beeckscheB Offline
              beeckscheB Offline
              beecksche
              wrote on last edited by beecksche
              #6

              @KamilGie

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

              void paintEvent(QpaintEvent *) override;
              
              K 1 Reply Last reply
              2
              • beeckscheB beecksche

                @KamilGie

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

                void paintEvent(QpaintEvent *) override;
                
                K Offline
                K Offline
                KamilGie
                wrote on last edited by
                #7

                @beecksche thanks for advise

                aha_1980A 1 Reply Last reply
                1
                • K KamilGie

                  @beecksche thanks for advise

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @KamilGie

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

                  Qt has to stay free or it will die.

                  K 1 Reply Last reply
                  0
                  • aha_1980A aha_1980

                    @KamilGie

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

                    K Offline
                    K Offline
                    KamilGie
                    wrote on last edited by
                    #9

                    @aha_1980 error still exists

                    aha_1980A beeckscheB 2 Replies Last reply
                    0
                    • K KamilGie

                      @aha_1980 error still exists

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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.

                      Qt has to stay free or it will die.

                      1 Reply Last reply
                      0
                      • K KamilGie

                        @aha_1980 error still exists

                        beeckscheB Offline
                        beeckscheB Offline
                        beecksche
                        wrote on last edited by beecksche
                        #11

                        @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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved