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. Pointer to class can't find function
Forum Updated to NodeBB v4.3 + New Features

Pointer to class can't find function

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.7k 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.
  • N Offline
    N Offline
    nanoandrew4
    wrote on last edited by
    #1

    So I have a class called displayData, with a constructor. The constructor has as an argument QPainter *painter, but when the game class tries to add it to the scene, it returns "no matching function for call to 'displayData::displayData()' data = new displayData();
    ^
    I have posted the relevant code here: http://pastebin.com/EsdXC32d

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You are trying to use displayData without parameter.
      As you are pointing out you have constructor with parameter.

      This will work.

      #ifndef DISPLAYDATA
      #define DISPLAYDATA
      
      #include <QGraphicsTextItem>
      
      class displayData : public QGraphicsTextItem{
      public:
          displayData(QPainter *painter = 0);
      private:
      };
      
      displayData::displayData(QPainter *painter)
      {
          if ( painter )
          {
              painter->setFont(QFont("times", 13));
              painter->drawText(5,15, QString("Speed: " + QString::number(speed)));
              painter->drawText(5, 30, QString("Rotation: " + QString::number(planeRotation)));
          }
      }
      
      

      Vote the answer(s) that helped you to solve your issue(s)

      N 1 Reply Last reply
      0
      • K koahnig

        You are trying to use displayData without parameter.
        As you are pointing out you have constructor with parameter.

        This will work.

        #ifndef DISPLAYDATA
        #define DISPLAYDATA
        
        #include <QGraphicsTextItem>
        
        class displayData : public QGraphicsTextItem{
        public:
            displayData(QPainter *painter = 0);
        private:
        };
        
        displayData::displayData(QPainter *painter)
        {
            if ( painter )
            {
                painter->setFont(QFont("times", 13));
                painter->drawText(5,15, QString("Speed: " + QString::number(speed)));
                painter->drawText(5, 30, QString("Rotation: " + QString::number(planeRotation)));
            }
        }
        
        
        N Offline
        N Offline
        nanoandrew4
        wrote on last edited by nanoandrew4
        #3

        @koahnig I did what you suggested, but now there is no text on the screen. When I build and run it says, "Note, no relevant classes found. No output generated." I have been able to display it on screen, but the reason I bring this up is so I can update the data displayed when the user hits one of the arrow keys. I might be using the wrong approach, but it seems to me that if I just make the constructor paint it it will be easier to access from outside classes. The example I am basing this off uses the paint() function and overrides it, but I tried to instead use the QPainter in the constructor to take up less space, and make it more accessible from other classes in other files.

        Thank you for your help!

        K 1 Reply Last reply
        0
        • N nanoandrew4

          @koahnig I did what you suggested, but now there is no text on the screen. When I build and run it says, "Note, no relevant classes found. No output generated." I have been able to display it on screen, but the reason I bring this up is so I can update the data displayed when the user hits one of the arrow keys. I might be using the wrong approach, but it seems to me that if I just make the constructor paint it it will be easier to access from outside classes. The example I am basing this off uses the paint() function and overrides it, but I tried to instead use the QPainter in the constructor to take up less space, and make it more accessible from other classes in other files.

          Thank you for your help!

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

          @nanoandrew4

          It depends what you are trying to do.
          I have simply updated the code which gave an error message. If those ae the only lines executed, there will be no text for sure.

              data = new displayData();
              scene->addItem(data);
          

          The first statement is creating an object with the default value handed to the constructor. The default value is a zero pointer. In the constructor the pointer is tested and since the pointer is zero the three statements are not carried out.
          If you are removing the if-statement, yopu will face a crash.

          Anyhow, there is still a conceptual problem in your program. However, it is not posssible to give you further help with the little information available. The only hint is that you need to use a pointer to QPainter when you do the initialization of displayData or you have to supply later.

          Vote the answer(s) that helped you to solve your issue(s)

          N 1 Reply Last reply
          0
          • K koahnig

            @nanoandrew4

            It depends what you are trying to do.
            I have simply updated the code which gave an error message. If those ae the only lines executed, there will be no text for sure.

                data = new displayData();
                scene->addItem(data);
            

            The first statement is creating an object with the default value handed to the constructor. The default value is a zero pointer. In the constructor the pointer is tested and since the pointer is zero the three statements are not carried out.
            If you are removing the if-statement, yopu will face a crash.

            Anyhow, there is still a conceptual problem in your program. However, it is not posssible to give you further help with the little information available. The only hint is that you need to use a pointer to QPainter when you do the initialization of displayData or you have to supply later.

            N Offline
            N Offline
            nanoandrew4
            wrote on last edited by
            #5

            @koahnig Ok so this might not be the right way to do it. Before I did this I used the paint() function, and used that to paint the text, but could not figure out how to update it once painted. The code I used was this http://pastebin.com/5pDbV0uw , but I can't run the function from outside the class, at least I don't know how to do it. What I want is to update the speed/rotation to match the actual values when a key is hit from the plane class using the keyPressEvent. Any hints on how to do that?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              Well, there is still some code missing to make this run.

              Please post code directly here in your replies. This makes it easier for others to read and respond.

              I am suggesting to you to go through some of the tutorials for Qt. Possibly you shall also revisit some tutorials for C++.

              Vote the answer(s) that helped you to solve your issue(s)

              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