How does the Qgraphic widget coordinate system work?



  • I cannot figure out how this extremely vague coordinate system is working.
    alt text
    On the right of the app you see the 3 squares and the yellow dot. These items are on a QGraphicview widget which is as high as the black display next to it.

    These squares + dot lie perfectly in the middle op the graphic view but I cannot move them up or down. The coordinates of all the items are linked to eachother which is for me nothing less than an annoying 'bug'.

    This is the code.

    scene = new QGraphicsScene(this);
      ui->graphicsView_1->setScene(scene);
    
      QBrush greenBrush(Qt::green);
      QBrush yellowBrush(Qt::yellow);
      QBrush redBrush(Qt::red);
      QBrush greyBrush(Qt::gray);
      QPen outlinePen(Qt::black);
      outlinePen.setWidth(0);
    
      rectangle = scene->addRect(0, 100, 140, 140, outlinePen, redBrush);
      rectangle = scene->addRect(20, 120, 100, 100, outlinePen, greyBrush);
      rectangle = scene->addRect(40, 140, 60, 60, outlinePen, greenBrush);
      ellipse = scene->addEllipse(65, 165, 10, 10, outlinePen, yellowBrush);
    

    It really does not matter which numbers I put in the first 2 arguements as long as the difference between them is the same. For instance, for the X coordinates I put in 0, 30, 40, 65. But if I would do 100, 130, 140, 165 there is no visual difference. This is a payne because I want to move these items down.

    That dot is suppose to dance arround in the rectangles but now it is dancing arround outside the rectangles. But because I understand nothing of this coordinate system I cannot fix this.

    If I change the Y-coordinate of the top rectangle, that rectangle still stays on the same place. But all the other items get pushed up or down.

    Can somebody tell me the logic behind this system so I can figure out how to move the rectangles and dot down.


  • Qt Champions 2016

    Hi
    Please read
    http://doc.qt.io/qt-5/graphicsview.html
    and section
    The Graphics View Coordinate System



  • @mrjj said in How does the Qgraphic widget coordinate system work?:

    Hi
    Please read
    http://doc.qt.io/qt-5/graphicsview.html
    and section
    The Graphics View Coordinate System

    Why do you think I come on this forum? I have read that page 3x now but every website with the letters Qt in it, is always a mediocre explaining website at best.

    It starts with ".. This makes it very easy to implement custom items..." Nothing in Qt is ever easy.

    Later in the text I come across: "The top left corner of QGraphicsView's viewport is always (0, 0), " This is what I thought. But if I try to paint a rectangle at 0,0

    rectangle = scene->addRect(0, 0, 140, 140, outlinePen, redBrush);
    

    Than why is the rectangle not painted in the top left corner? I mean how complicated do people want software to be?
    Why do all rectangles move if I give just 1 an other location?

    I came here to get an explanation to why my items not drawn on the given coordinates and why they all move when I want to move only 1.



  • Not a good idea to get snotty with people who kindly reply to you on this forum, if you expect to get any help help...


  • Moderators

    @bask185 Can you share your code?



  • @kenchan said in How does the Qgraphic widget coordinate system work?:

    Not a good idea to get snotty with people who kindly reply to you on this forum, if you expect to get any help help...

    I was not getting snotty with him but with Qt's 'wonderful features' and Qt's 'well explaining' websites like I always do... If that was not obvious, I apologize.

    @jsulm said in How does the Qgraphic widget coordinate system work?:

    @bask185 Can you share your code?

    But ofc. it is alot of code, but if you Ctrl F "Jsulm" you will be pointed at the functions in question. I have the dot now moving correctly inside the rectangles but I still have absolute no control in moving the rectangles at all.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QPixmap>
    #include <QDebug>
    #include <QPalette>
    #include <QtSerialPort>
    #include "keyboard.h"
    #include "numpad.h"
    #include <QTableWidget>
    #include <QHeaderView>
    #include <QColor>
    #include <setup.h>
    #include <QKeyEvent>
    #include <QGraphicsEllipseItem>  
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        qDebug().noquote() << "hello raspberry pi";
        serial = new QSerialPort();
    
        QPixmap logo(":/resources/img/logo.jpg");
        int w = ui->label->width();
        int h = ui->label->height();
        ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
    
       connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
       Kboard = new Keyboard();
       numpad = new Numpad();
       settings = new Setup();
       connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
       connect(numpad, &Numpad::KeyPressed, this, &MainWindow::send);
    
       connect(settings, &Setup::openSerialPort, this, &MainWindow::openSerialPort);
       connect(settings, &Setup::closeSerialPort, this, &MainWindow::closeSerialPort);
       connect(settings, &Setup::changeInterval, this, &MainWindow::changeInterval);
    
       connect(settings, &Setup::changeIntervalKeyboard, Kboard, &Keyboard::changeIntervalKeyboard);
    
       monitors[0] = ui->tableWidget_1;
       monitors[1] = ui->tableWidget_2;
       monitors[2] = ui->tableWidget_3;
       monitors[3] = ui->tableWidget_4;
       monitors[4] = ui->tableWidget_5;
    
    
       for(QTableWidget* monitor : monitors){
           monitor->setRowCount(16);
           monitor->setColumnCount(40);
           monitor->setShowGrid(false);
           monitor->horizontalHeader()->hide(); // requires #include <QHeaderView>
           monitor->verticalHeader()->hide();
           monitor->rowHeight(5);
           monitor->columnWidth(5);
           monitor->horizontalHeader()->setDefaultSectionSize(24);
           monitor->verticalHeader()->setDefaultSectionSize(24);
           monitor->horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
           monitor->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
       }
    
      for(int j = 0; j < 16; j++) {
           for(int k= 0; k < 40; k++) {
               column[index1] = k;
               row[index1] = j;
                monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]),QBrush(Qt::green),Qt::ForegroundRole);
                monitors[index1]->item(row[index1],column[index1])->setTextAlignment(Qt::AlignRight);
          }
       }
      column[index1] = 0;
      row[index1] = 0;
    
      // QGraphic widget                                <<-- Jsulm
    
      scene = new QGraphicsScene(this);
      ui->graphicsView_1->setScene(scene);
    
      //scene->mapToScene(0, 0);
    
      QBrush greenBrush(Qt::green);
      QBrush yellowBrush(Qt::yellow);
      QBrush redBrush(Qt::red);
      QBrush greyBrush(Qt::gray);
      QPen outlinePen(Qt::black);
      outlinePen.setWidth(0);
    
      rectangle = scene->addRect(-70, -70, 140, 140, outlinePen, redBrush);
      rectangle = scene->addRect(-50, -50, 100, 100, outlinePen, greyBrush);
      rectangle = scene->addRect(-30, -30, 60, 60, outlinePen, greenBrush);
      ellipse = scene->addEllipse(-5, -5, 10, 10, outlinePen, yellowBrush);
      //horizontalLine = scene->addLine(QlineF(-50,0,50,0),outlinePen);
      //verticalLine = scene->addLine(QlineF(0,-50,0,50),outlinePen);
    
    }
    
    MainWindow::~MainWindow()
    {
       delete ui;
    }
    
    void MainWindow::serialReceived()
    {
        QDataStream serialStream(serial);
        unsigned char b;
        //qDebug() << "entering the for-loop of death";
        for(;;){
         //QCoreApplication::processEvents();
         serialStream.startTransaction();
         serialStream >> b;
         if(serialStream.commitTransaction()) {
        //qDebug() << "Incoming byte!";
    
    
            QString bAsHex = QString("%1").arg(b, 0, 16);
            qDebug() << "<< " << bAsHex;
    
            if (b == 0x80 && COMMAND == false) {EXCEPTION = true;qDebug()<< "0x80 received";}
            if(EXCEPTION == false) {
                if(b > 0x1F && b < 0x80 && COMMAND == false) {                                         // if received bytes are > 31 and < 127 AND if the COMMAND flag is false, the byte is treated as a to be printed character
                    monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]),QBrush(color[colorIndex[2]]),Qt::ForegroundRole); // sets the color of the lettre
                    monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]++),QChar(b));  // stuffs the character in the text array with the active color.
                    //MainWindow::drawCursor();
                    qDebug() << (char)b <<"printed on" << column[index1] << row[index1]<< "in color" << colorIndex[0] << "On page"<< index1 << "Shown Page =" << index2;
                  } else {
                    COMMAND = true;                                                                   // Makes sure that any following byte(s) with a value < 31 or > 127 is treated as a command instruction
                    switch(command) {                                                                  // this switch case is the menu for commands
                    case 1:
                      switch(b) {                                       // this will be the first case to be run and this switch case links the received command to an appropiate action
    
                        case 0x01: MainWindow::clearAll(); break;                                                                      // CLEAR ALPHA PAGE AND HOME CURSOR
                        case 0x02: MainWindow::clearToEndOfLine(); break;                                                              // CLEAR TO END OF LINE (CLREOL2 WORDT VERZONDEN)
                        case 0x03: blink[index1] = true; COMMAND = false;MainWindow::drawCursor(); command = 1; qDebug() << "cursor on";  break;     // CURSOR ON BLINK
                        case 0x04:  MainWindow::clearToEndOfScreen(); break;                                                           // CLEAR TO END OF SCREEN
                        case 0x05: blink[index1] = false; MainWindow::drawCursor(); COMMAND = false; command = 1; qDebug() << "cursor off";  break;  // CURSOR OFF
                        case 0x06: command = 2; break;                                                                                 // SET CURSOR X-Y (ALPHA)  <-- cursor is the blinking rectangle
                        case 0x08: command = 11;   break;                                                                              // CURSOR SHIFT ONE POS. TO LEFT
                        case 0x09: command = 12;  break;                                                                               // CURSOR SHIFT ONE POS. TO RIGHT
                        case 0x0E: command = 7; break;                                                                                 // SETDRAW ALPHA PAGE
                        case 0x10: command = 8; break;                                                                                 // SETSHOW ALPHA PAGE
                        case 0x18: command = 6; break;                                                                                 // COLOUR C <--   1-16
                        case 0x1A: command = 4; break;                                                                                 // SET ALPHANUMERIC POSITION X Y <-- actual true position of the cursor
                        //case 0x1C: command = 9; break;  /* OBSOLETE */                                                               // SET BIG FIGURE ON GRAPHIC PAGE
                        case 0x1C: send((QString)0x96); qDebug() << "hello machine"; COMMAND = false;  break;                          // RESPOND TO HANDSHAKING CALL
    
                        case 0x0D: command = 13; break;                                                                                  // SET DRAW GRAPHIC PAGE
                        case 0x19: COMMAND = false; command = 1; qDebug() << "clear graphic page command, no action taken";break;        // CLEAR GRAPHIC PAGE
                        case 0x0F: command = 15; break;                                                                                  // SET SHOW GRAPHIC PAGE
    
                        case 0x85: COMMAND = false;command = 1;qDebug() << "translate command, no action taken";break; // translate command
                        case 0x86: COMMAND = false;command = 1;qDebug() << "end-translate command, no action taken";break; // translate command
    
                      }
                      //MainWindow::drawCursor();
                      break;
    
                    case 2:                                  // cases 2 and 3 handle the setCursor function
                      cursorColumnPrevious[index1] = cursorColumn[index1];
                      cursorRowPrevious[index1] = cursorRow[index1];
    
                      if(b == 41) b = 40;
                      if(b == 0) b = 1;
                      cursorColumn[index1] = b - 1;
                      command = 3;
                      break;
                      case 3:                  
                      if(b == 17) b = 16;
                      if(b == 0) b = 1;
                      cursorRow[index1] = b - 1;
                      COMMAND = false;
                      command = 1;
                      MainWindow::drawCursor();
                      break;
    
                    case 4:                                  // cases 4 and 5 handle the setPosition function
                      if(b == 41) b = 40;
                      column[index1] = b - 1;
                      command = 5;
                      break;
                      case 5:
                      if(b == 17) b = 16;
                      row[index1] = b - 1;
                      COMMAND = false;
                      command = 1;
                      //MainWindow::drawCursor();
                      qDebug() << "position set at "<< column[index1]<<row[index1];
                      break;
    
                    case 6:                                  // case 6 handles the colour
                      colorIndex[0] = b;
                      COMMAND = false;
                      command = 1;
                      //MainWindow::drawCursor();
                      qDebug() << "color index =" << colorIndex[0];
                      break;
    
                    case 7:                                   // case 7 does SETDRAW ALPHA PAGE
                      index1 = b - 1;
                      COMMAND = false;
                      command = 1;
                      qDebug() << "alpha page" << index1 + 1 << "selected";
                      MainWindow::drawCursor();
                      break;
    
                    case 8:                                   // case 8 does SETSHOW ALPHA PAGE
                      index2 = b - 1;
                      COMMAND = false;
                      monitors[index2]->raise();
                      //graphicsView_1->raise();
                      command = 1;
                      MainWindow::drawCursor();
                      qDebug() << "alpha page" << index2 << "is shown";
                      break;
    
                    case 9:                                   // case 9 draws the BIG FIGURE
                      //display[index1].setFigure(b);          // OBSOLETE
                      COMMAND = false;
                      command = 1;
                      break;                                         
    
                    case 11:
                        cursorColumn[index1]--;    // CURSOR SHIFT ONE POS. TO LEFT
                        COMMAND = false;
                        command = 1;
                        MainWindow::drawCursor();
                        qDebug() << "cursor shift left";
                        break;
    
                    case 12:                       // CURSOR SHIFT ONE POS. TO RIGHT
                        cursorColumn[index1]++;
                        COMMAND = false;
                        command = 1;
                        MainWindow::drawCursor();
                        qDebug() << "cursor shift right";
                        break;
    
                    case 13:                      // SET DRAW GRAPHIC PAGE
                        COMMAND = false;
                        command = 1;
                        //MainWindow::drawCursor();
                        qDebug() << "set draw graphic page command, no action taken";
                        break;
    
                    case 15:                      // CLEAR GRAPHIC PAGE
                        COMMAND = false;
                        command = 1;
                        //MainWindow::drawCursor();
                        qDebug() << "Set show graphic page command, no action taken";
                        break;
                     }
                 }
           } else if (EXCEPTION == true) {
                switch(special) {
                      case 0:
                      special = 7;
                      break;
    
                      case 7:
                      _0x80 = b;
                      special = 1;
                      qDebug() << "0x80 command" << "case"<< _0x80;
                      break;
    
                      case 1:
                      switch(_0x80) {
                        case 1: dummy = 1; MainWindow::readDummy(); break;
                        case 2: dummy = 1; MainWindow::readDummy(); break;
                        case 3: // cursorpositie. (0 t/m 80)                    << this function Jsulm
                          {
                              qreal X = (b % 9) * 15 - 60;
                              qreal Y = (b / 9) * 15 - 60;
    
                              ellipse->setPos(X,Y);
                              qDebug()<< "Bullseye set at" << X / 15 + 60 << Y / 15 + 60;
                              EXCEPTION = false;
                              special = 0;
                          }
                          break;
    
                        case 4: dummy = 1; MainWindow::readDummy(); break; //set big figure
                        case 5: dummy = 1; MainWindow::readDummy(); break;
                        case 6: special = 2;             break;  // special
                        case 14: dummy = 14; MainWindow::readDummy(); break;
                        case 15: dummy = 2; MainWindow::readDummy(); break;
                        case 16: dummy = 4; MainWindow::readDummy(); break;
                        case 17: dummy = 11; MainWindow::readDummy(); break;
                        case 18: dummy = 1; MainWindow::readDummy(); break;
                        case 19: dummy = 1; MainWindow::readDummy(); break;
                        case 20: dummy = 4; MainWindow::readDummy(); break;
                        case 21: dummy = 13; MainWindow::readDummy(); break;
                        case 22: dummy = 3; MainWindow::readDummy(); break;
                        case 23: dummy = 1; MainWindow::readDummy(); break;
                        case 24: dummy = 1; MainWindow::readDummy(); break;
                        case 25: dummy = 2; MainWindow::readDummy(); break;
                        case 26: dummy = 2; MainWindow::readDummy(); break;
                        case 27: dummy = 5; MainWindow::readDummy(); break;
                        case 28: dummy = 2; MainWindow::readDummy(); break;
                        case 30: dummy = 3; MainWindow::readDummy(); break;
                        case 31: dummy = 6; MainWindow::readDummy(); break;
                        case 32: dummy = 1; MainWindow::readDummy(); break;
                        case 34: dummy = 25; MainWindow::readDummy(); break;
                        case 35: dummy = 3; MainWindow::readDummy(); break;
                        case 37: dummy = 15; MainWindow::readDummy(); break;
                        case 38: special = 3;dummy = b;             break; // special
                        case 40: special = 4;                       break; // special
                        case 41: dummy = 3; MainWindow::readDummy(); break;
                        case 42: special = 5;dummy = b;               break;// special
                        case 50: dummy = 10; MainWindow::readDummy(); break;
                        case 51: dummy = 4; MainWindow::readDummy(); break;
                        case 52: dummy = 12; MainWindow::readDummy(); break;
                        case 53: dummy = 4; MainWindow::readDummy(); break;
                        case 55: dummy = 2; MainWindow::readDummy(); break;
                        case 56: dummy = 1; MainWindow::readDummy(); break;
                        case 57: dummy = 1; MainWindow::readDummy(); break;
                        case 58: special = 6; dummy = b;             break; // special
                        case 59: dummy = 2; MainWindow::readDummy(); break; // special
                        case 73: dummy = 2; MainWindow::readDummy(); break;
                        case 74: dummy = 11; MainWindow::readDummy(); break;
                        case 75: special = 8;            break; // special
                        case 80: dummy = 2; MainWindow::readDummy(); break;
                        case 100: dummy = 9; MainWindow::readDummy(); break;
                      }
                      break;
    
                      case 2:
                      dummy++;
                      if(dummy >= 33) {
                          dummy=0;
                          EXCEPTION = false;
                          qDebug() << "false command averted";
                      }
                      break;
    
                      case 3:
                      if (PIGS_CAN_FLY == false){
                          qDebug() << "dummy ="<< dummy;
                          PIGS_CAN_FLY = true;
                      }
                      else if (PIGS_CAN_FLY == true) {
                          dummy2++;
                          if(dummy2 == dummy){
                              EXCEPTION = false;
                              PIGS_CAN_FLY = false;
                              dummy  = 0;
                              dummy2 = 0;
                              special= 0;
                              qDebug() << "false command averted";
                           }
                      }
                      break;
    
                      case 4:
                        if(PIGS_CAN_FLY == false) {    // will be true in the beginning
                            dummy++;                   // keep increasing dummy until...
                            if(dummy == 5) {          //.... dummy == 5,   // NB one byte is dependend of boolean, can be 4
                                dummy2 = b;           // then read b and stuff it in dummy 2
                                if(dummy2 > 64) dummy2 -=64; // if dummy2 > 64 substract 64
                                if (dummy2 == 0) {          // if dummy 2 == 0...
                                    PIGS_CAN_FLY = true;    // ...set boolean true
                                    dummy = 0;              // ...and reset dummy
                                } else {
                                    PIGS_CAN_FLY = false;      // reset all used variables
                                    EXCEPTION = false;
                                    dummy = 0;
                                    dummy2 = 0;
                                    special = 0;
                                    qDebug() << "dummy2 is 0";
                                    qDebug() << "false command averted";
                                    break;
                                }                                           // if dummy 2 != 0 break
                            }
                        } else if(PIGS_CAN_FLY == true) {  // if the boolean has been set ... ignore the code above
                            dummy++;                       // keep increasing dummy until...
                            qDebug() << "dummy2 is not 0";
                            if(dummy == 5) {               // .. dummy = 5  than
                                PIGS_CAN_FLY = false;      // reset all used variables
                                EXCEPTION = false;
                                dummy = 0;
                                dummy2 = 0;
                                special = 0;
                                qDebug() << "false command averted";
                            }
                        }
                      break;
    
                      case 5:
                        if (PIGS_CAN_FLY == false) PIGS_CAN_FLY = true;
    
                        else if (PIGS_CAN_FLY == true) {
                            dummy2++;
                            if(dummy2 == dummy){
                                EXCEPTION = false;
                                PIGS_CAN_FLY = false;
                                dummy  = 0;
                                dummy2 = 0;
                                special = 0;
                                qDebug() << "false command averted";
                             }
                        }
                      break;
    
                      case 6:
                        if (PIGS_CAN_FLY == false) PIGS_CAN_FLY = true;
    
                        else if (PIGS_CAN_FLY == true) {
                            dummy2++;
                            if(dummy2 == dummy + 1){
                                EXCEPTION = false;
                                PIGS_CAN_FLY = false;
                                dummy  = 0;
                                dummy2 = 0;
                                special = 0;
                                qDebug() << "false command averted";
                             }
                        }
                      break;
    
                      case 8:
                       if (PIGS_CAN_FLY == true) {
                           if(dummy2 == dummy + 1){
                               PIGS_CAN_FLY = false;
                               dummy = 0;
                               dummy2 = 0;
                               EXCEPTION = false;
                               special = 0;
                               qDebug() << "false command averted";
                               break;
                           }
    
                       } else {PIGS_CAN_FLY = true; dummy = b;}
    
                       dummy2++;
                       break;
                    }
            }
         }else break;
      }
    }
    
    
    void MainWindow::readDummy() {
      dummy2++;
      if(dummy2 == dummy) {
        dummy2=0;
        dummy=0;
        EXCEPTION = false;
        special = 0;
        qDebug() << "dummy bytes read!";
      }
    }
    
    void MainWindow::send(QString c)
    {
      serial->write(c.toLatin1());
      QString cAsHex = QString("%1").arg(c, 0, 16);
      qDebug()  << cAsHex << " >>";
    }
    
    void MainWindow::on_F1_clicked()
    {
        MainWindow::send((QString)0x8a);
    }
    void MainWindow::on_F2_clicked()
    {
         MainWindow::send((QString)0x85);
    }
    void MainWindow::on_F3_clicked()
    {
       MainWindow::send((QString)0x80);
    }
    void MainWindow::on_F4_clicked()
    {
       MainWindow::send((QString)0x81);
    }
    void MainWindow::on_F5_clicked()
    {
       MainWindow::send((QString)0x82);
    }
    void MainWindow::on_F6_clicked()
    {
       MainWindow::send((QString)0x83);
    }
    void MainWindow::on_F7_clicked()
    {
       MainWindow::send((QString)0x84);
    }
    void MainWindow::on_F8_clicked()
    {
       MainWindow::send((QString)0x95);
    }
    void MainWindow::on_F9_clicked()
    {
      MainWindow::send((QString)0x97);
    }
    void MainWindow::on_F10_clicked()
    {
       MainWindow::send((QString)0x98);
    }
    void MainWindow::on_F11_clicked()
    {
       MainWindow::send((QString)0x99);
    }
    void MainWindow::on_F12_clicked()
    {
       MainWindow::send((QString)0x9a);
    }
    void MainWindow::on_F13_clicked()
    {
       MainWindow::send((QString)0x9b);
    }
    void MainWindow::on_F14_clicked()
    {
       MainWindow::send((QString)0x9c);
    }
    void MainWindow::on_F15_clicked()
    {
       MainWindow::send((QString)0x9d);
    }
    void MainWindow::on_F16_clicked()
    {
       MainWindow::send((QString)0x9e);
    }
    void MainWindow::on_START_clicked()
    {
       MainWindow::send((QString)0x86);
    }
    void MainWindow::on_STOP_clicked()
    {
       MainWindow::send((QString)0x87);
    }
    void MainWindow::on_STEP_clicked()
    {
       MainWindow::send((QString)0x88);
    }
    void MainWindow::on_UP_clicked()
    {
       MainWindow::send((QString)0x8c);
       //ellipse->setPos(yolo, --swag);qDebug()<<yolo<<swag;
    }
    void MainWindow::on_DOWN_clicked()
    {
       MainWindow::send((QString)0x8f);
       //ellipse->setPos(yolo, ++swag);qDebug()<<yolo<<swag;
    }
    void MainWindow::on_LEFT_clicked()
    {
       MainWindow::send((QString)0x8d);
       //ellipse->setPos(--yolo, swag);qDebug()<<yolo<<swag;
    }
    void MainWindow::on_RIGHT_clicked()
    {
       MainWindow::send((QString)0x8e);
       //ellipse->setPos(++yolo, swag);qDebug()<<yolo<<swag;
    }
    void MainWindow::on_PAGEUP_clicked()
    {
       MainWindow::send((QString)0x94);
    }
    void MainWindow::on_PAGEDOWN_clicked()
    {
       MainWindow::send((QString)0x93);
    }
    void MainWindow::on_MENU_clicked()
    {
      MainWindow::send((QString)0x89);
    }
    
    void MainWindow::on_NUMPAD_clicked()
    {
        numpad->setModal(true);
        numpad->setGeometry(437,548,150,200);
        numpad->exec();
    }
    void MainWindow::on_KEYBOARD_clicked()
    {
        Kboard->setModal(true);
        Kboard->setGeometry(157,548,720,200);
        Kboard->exec();
    }
    
    void MainWindow::on_Y_clicked()
    {
       MainWindow::send((QString)"Y");
    }
    
    void MainWindow::on_N_clicked()
    {
        MainWindow::send((QString)"N");
    }
    
    void MainWindow::on_ENTER_clicked()
    {
        MainWindow::send((QString)0x8b);  
    }
    
    void MainWindow::drawCursor()
    {
        monitors[index1]->model()->setData(monitors[index1]->model()->index(cursorRowPrevious[index1],cursorColumnPrevious[index1]),QBrush(color[0]),Qt::BackgroundRole);// makes the previous cursor location black
    
        if(blink[index1] == true)
            monitors[index1]->model()->setData(monitors[index1]->model()->index(cursorRow[index1],cursorColumn[index1]),QBrush(color[2]),Qt::BackgroundRole); // green
        //else
            //monitors[index1]->model()->setData(monitors[index1]->model()->index(cursorRow[index1],cursorColumn[index1]),QBrush(color[0]),Qt::BackgroundRole); // black
    
        //cursorRowPrevious[index1] = row[index1];
        //cursorColumnPrevious[index1] = column[index1];
        qDebug() << "Old cursor position was at "<< cursorColumnPrevious[index1]<<cursorRowPrevious[index1];
        qDebug() << "cursor set at "<< cursorColumn[index1]<<cursorRow[index1];
    }
    
    void MainWindow::on_Settings_clicked()
    {
        settings->setModal(true);
        settings->setGeometry(399,248,230,500);
        settings->exec();
    }
    
    void MainWindow::openSerialPort()
    {
        Setup::Settings p = settings->settings();
        serial->setPortName(p.name);
        serial->setBaudRate(p.baudRate);
        serial->setDataBits(p.dataBits);
        serial->setParity(p.parity);
        serial->setStopBits(p.stopBits);
        serial->setPortName(p.name);
        serial->open(QIODevice::ReadWrite);
    
             /*if (serial->open(QIODevice::ReadWrite)) {
            console->setEnabled(true);
            console->setLocalEchoEnabled(p.localEchoEnabled);
            ui->actionConnect->setEnabled(false);
            ui->actionDisconnect->setEnabled(true);
            ui->actionConfigure->setEnabled(false);
            showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
                              .arg(p.name).arg(p.stringBaudRate).arg(p.stringDataBits)
                              .arg(p.stringParity).arg(p.stringStopBits).arg(p.stringFlowControl));
        } else {
            QMessageBox::critical(this, tr("Error"), serial->errorString());
    
            showStatusMessage(tr("Open error"));
        }*/
    }
    
    void MainWindow::closeSerialPort()
    {
        serial->close();
    }
    
    void MainWindow::changeInterval(int value)
    {
       ui->DOWN->setAutoRepeatInterval(value);
       ui->UP->setAutoRepeatInterval(value);
       ui->LEFT->setAutoRepeatInterval(value);
       ui->RIGHT->setAutoRepeatInterval(value);
       //qDebug() << "i'm @ Mainwindow";
    }
    
    void MainWindow::keyPressEvent(QKeyEvent* event)
    {
        if(event->key() == Qt::Key_Escape)
        {
           qDebug() << "Escape is pressed, quitting application...";
           QApplication::quit();
        }
    }
    
    void MainWindow::clearToEndOfScreen()
    {
        for(int i = column[index1]; i < 40; i++) {
            store = column[index1];
            column[index1] = i;
            monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]++),QChar(' '));    // clears top line
          }
          store2 = row[index1];
          for(int j = row[index1] + 1; j < 16; j++) {
            for(column[index1] = 0; column[index1] < 40; column[index1]++){ // clears everything else
                row[index1] = j;
                //column[index1] = i;
                monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]),QChar(' '));
            }
          }
          row[index1] = store2;
          column[index1] = store;
    
          COMMAND = false;
          command = 1;
          MainWindow::drawCursor();
          qDebug() << "screen cleared to end";
    }
    
    void MainWindow::clearToEndOfLine()
    {
        qDebug() << "clearToEndOfLine" << column[index1] << row[index1];
        store = column[index1];
        for(int i = column[index1]; i < 40; i++) {
          column[index1] = i;
          monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]),QChar(' '));
        }
        column[index1] = store;
    
       // cursorColumn[index1] = column[index1];
       // cursorColumnPrevious[index1] =  cursorColumn[index1];
    
        column[index1] = 0;
        row[index1]++;
    
        //MainWindow::drawCursor();
        COMMAND = false;
    }
    
    void MainWindow::clearAll()
    {
        //cursorRowPrevious[index1] = row[index1];
        //cursorColumnPrevious[index1] = column[index1];
        //MainWindow::drawCursor();
    
        cursorColumn[index1]=0; // homes cursor
        cursorRow[index1]=0;
    
        column[index1]=0;       // homes alpha position
        row[index1]=0;
        for(int i = 0; i < 40; i++) {
            for(int j = 0; j < 16; j++) {
    
              column[index1] = i;
              row[index1] = j;
    
              monitors[index1]->model()->setData(monitors[index1]->model()->index(row[index1],column[index1]),QChar(' '));
            }
        }
    
        COMMAND = false;
        command = 1;
        MainWindow::drawCursor();
        qDebug() << "screen cleared and cursor homed";
    }
    
    
    // GRAPHIC OBJECTS //
    [HEADER]
    
    private:
        QGraphicsScene *scene;
        QGraphicsRectItem *rectangle;
        QGraphicsEllipseItem *ellipse;
        QGraphicsLineItem *horizontalLine, *verticalLine; // not used
        QGraphicsView *graphicsView_1;
    

  • Moderators

    @bask185 said in How does the Qgraphic widget coordinate system work?:

    rectangle = scene->addRect(-70, -70, 140, 140, outlinePen, redBrush);
    rectangle = scene->addRect(-50, -50, 100, 100, outlinePen, greyBrush);
    rectangle = scene->addRect(-30, -30, 60, 60, outlinePen, greenBrush);
    ellipse = scene->addEllipse(-5, -5, 10, 10, outlinePen, yellowBrush);

    is there a reason why you use negative numbers for x and y coordinates?



  • What are the settings for the QGraphicsView which I guess you have added with QDesigner?
    Things like the Transformation Anchor point and alignment can have an influence. Also what is the sceneRect set to? this will have an influence on your coordinate system. Or to be more precise, what you actually see.



  • @jsulm said in How does the Qgraphic widget coordinate system work?:

    @bask185 said in How does the Qgraphic widget coordinate system work?:

    rectangle = scene->addRect(-70, -70, 140, 140, outlinePen, redBrush);
    rectangle = scene->addRect(-50, -50, 100, 100, outlinePen, greyBrush);
    rectangle = scene->addRect(-30, -30, 60, 60, outlinePen, greenBrush);
    ellipse = scene->addEllipse(-5, -5, 10, 10, outlinePen, yellowBrush);

    is there a reason why you use negative numbers for x and y coordinates?

    On the Qt webpage of the first reply: "Graphics View is based on the Cartesian coordinate system;" My superior here said that he thought the 0 ,0 would be in the middle rather than the top-left corner and I did not check whether he was right or wrong. But it does not matter really, I have the exact same layout when I use positive numbers, as long as the interval between them is consistent

    @kenchan said in How does the Qgraphic widget coordinate system work?:

    What are the settings for the QGraphicsView which I guess you have added with QDesigner?
    Things like the Transformation Anchor point and alignment can have an influence. Also what is the sceneRect set to? this will have an influence on your coordinate system.

    That what is not in my code, is that what I don't know about.
    I don't know what Qdesigner is.
    I know nothing about sceneRect
    And I don't know how I can use a Transformation Anchor Point.

    These things are also not mentioned on the Qt doc page of graphicsview I believe. I looked into sceneRect() and it appears to be the solution.
    By adding.

    scene->setSceneRect(0,0,165,400);
    

    I can finally manipulate the locations of my items.

    So thnx @kenchan ;)



  • Glad to be of help.
    There are a number of settings for the QGraphicsView, which is used to actually see the QGraphicsScene and need to be taken into account. It is all in the documentation but I agree it is not always clearly explained and sometimes it can take trial and error to understand how it works.

    Good luck with your project.


Log in to reply