Nominate our 2022 Qt Champions!

Pointer to class can't find function

  • 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:

  • 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{
        displayData(QPainter *painter = 0);
    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)));

  • @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!

  • @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();

    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.

  • @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 , 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?

  • 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++.

Log in to reply