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. Program doesn't draw anything, why?
Forum Updated to NodeBB v4.3 + New Features

Program doesn't draw anything, why?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 1.3k Views 1 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.
  • G Offline
    G Offline
    guaranito93
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        guaranito93
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

            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