Program doesn't draw anything, why?



  • Hi all,
    I'm programming a a kind of "virtual tennis" game, but when I run it there isn't anything but the grey background. Can you please help understand why?

    objects.cpp

    @#include "objects.h"

    Objects::Objects(QWidget *parent) :
    QWidget(parent)
    {
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    //angoli campo da gioco
    Border[0].setY(-height()/2);
    Border[0].setX(-width()/2);
    Border[1].setY(-height()/2);
    Border[1].setX(+width()/2);
    Border[2].setY(+height()/2);
    Border[2].setX(+width()/2);
    Border[3].setY(+height()/2);
    Border[3].setX(-width()/2);

    //dimensioni oggetti
    WidthPad=13;
    HeightPad=30;
    sizeBall=10;
    
    //coordinate Pad1
    Pad1.setX(-width()+WidthPad);
    Pad1.setY(-height()/2+HeightPad);
    
    //coordinate Pad2
    Pad2.setX(width()-WidthPad);
    Pad2.setY(+height()/2-HeightPad);
    
    //coordinate pallina
    Ball.setX(width()/2);
    Ball.setY(0);
    

    }

    QPolygon Objects::BuildRectangle(QPoint location, int Large, int Tall){
    QPolygon Rectangle;
    QPoint TopRight, BottomRight, BottomLeft;
    TopRight.setX(location.x()+Large);
    TopRight.setY(location.y());
    BottomRight.setX(TopRight.x());
    BottomRight.setY(TopRight.y()-Tall);
    BottomLeft.setX(location.x());
    BottomLeft.setY(BottomRight.y());
    Rectangle << location;
    Rectangle << TopRight;
    Rectangle << BottomRight;
    Rectangle << BottomLeft;
    return Rectangle;
    }

    void Objects::SetAliasing(bool antialiasing){
    this->antialiased = antialiasing;
    update();
    }

    void Objects::PaintObjects(QPaintEvent *){
    QPainter PaintIt(this);
    QPainter PaintThem(this);
    QPolygon BorderLine, Plat1, Plat2;

    //Creazione oggetto grafico per coordinate del confine di giuoco
    BorderLine << Border[0];
    BorderLine << Border[1];
    BorderLine << Border[2];
    BorderLine << Border[3];
    
    //????????????????????????????
    PaintIt.setRenderHint(QPainter::Antialiasing,antialiased);
    PaintThem.setRenderHint(QPainter::Antialiasing,antialiased);
    
    //Creazione oggetti grafici Pad di giuoco
    Plat1 = BuildRectangle(Pad1,WidthPad,HeightPad);
    Plat2 = BuildRectangle(Pad2,WidthPad,HeightPad);
    
    //Setting di disegno
    PaintIt.setPen(Qt::black);
    PaintThem.setPen((Qt::black));
    PaintThem.setBrush(Qt::black);
    
    //Translazione
    PaintIt.translate(width(), height());
    PaintThem.translate(width(), height());
    
    //Muciaccia
    PaintIt.drawPolygon(BorderLine);
    PaintThem.drawPolygon(Plat1);
    PaintThem.drawPolygon(Plat2);
    PaintThem.drawEllipse(Ball,sizeBall,sizeBall);
    

    }
    @

    virtualtennis.cpp

    @#include "virtualtennis.h"

    VirtualTennis::VirtualTennis(){
    //vettori di spostamento della pallina;
    VectorX=5;
    VectorY=3;

    //Oggetti del giuoco
    mambo=new Objects;
    
    //Creazione e settaggio del layout
    QGridLayout *Layout = new QGridLayout;
    Layout->addWidget(mambo);
    setLayout(Layout);
    setWindowTitle(tr("VIRTUAL TENNIS SINGLE PLAYER"));
    
    //Creazione del timer;
    QTimer *Clock = new QTimer(this);
    Clock->start(500);
    connect(Clock, SIGNAL(timeout()),this, SLOT(MoveDaBall(VectorX,VectorY)));
    

    }
    @

    Thanks


  • Lifetime Qt Champion

    Hi,

    You don't call start nor end on your QPainter. These are need to tell Qt that you are painting something and when you did end the painting

    Hope it helps



  • Ehm... I didn't get it... I'm a newbie and I still need to learn a lot.

    When I say:
    @
    mambo=new Objects;
    QGridLayout *Layout = new QGridLayout;
    Layout->addWidget(mambo);
    setLayout(Layout);
    @

    It doesn't mean that I the program draw everything in the layout? If not how do I solve the problem?


  • Lifetime Qt Champion

    It will show the widget, but you must ensure that the painting is done correctly first.

    I've re-read QPainter's documentation, begin/end are not mandatory the way you use QPainter. However, I have not seen implementation using two QPainter on the same device, so it might be a source of problem.

    Since you are completely new, you should go step by step. As silly as it may sound, start by painting a rect, once you have it on screen properly. Add the different pieces gradually.


  • Moderators

    Few things here.

    First - you should paint in
    @void Objects::paintEvent(QPaintEvent *) override@
    Qt doesn't know anything about
    @void Objects::PaintObjects(QPaintEvent *)@
    so unless you're not giving us the whole code this will never be called.

    Second, as SGaist said, you should use only one painter on a paint device (in your case "this" widget). There's just no point in having two there.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.