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. Problem With QRect and QPainter
Forum Updated to NodeBB v4.3 + New Features

Problem With QRect and QPainter

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 1.5k 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.
  • jsulmJ jsulm

    @Oumayma To add to @Pl45m4 : you can also use https://doc.qt.io/qt-5/animation-overview.html

    O Offline
    O Offline
    Oumayma
    wrote on last edited by
    #4

    @jsulm
    How can I use a QPropertyAnimation for a Qmenubar and a Qmenu I only find Qbutton examples, thanks.

    Pl45m4P 1 Reply Last reply
    0
    • Pl45m4P Pl45m4

      @Oumayma

      You need to define steps. move(pos) moves the widget directly to pos.
      Use a QTimer and move your rect by x every y msecs.

      O Offline
      O Offline
      Oumayma
      wrote on last edited by
      #5

      @Pl45m4
      I'm researching the QTimer but I don't really know how to use it.
      The following would be a Qtimer of one second, but I don't know how to use this to make my QRect move after one second has passed:

      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(update()));
      timer->start(1000);

      Thanks.

      1 Reply Last reply
      0
      • O Oumayma

        @jsulm
        How can I use a QPropertyAnimation for a Qmenubar and a Qmenu I only find Qbutton examples, thanks.

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #6

        @Oumayma

        For your property animation you can use the example from the documentation

        https://doc.qt.io/qt-5/qpropertyanimation.html#details

        Animate / Change the geometry of your QRect over time.

        To use the timer approach you'll need a slot, where you manually increase your size, position or whatever you want to change. Just update() won't work.

        Something like:

        void MainWindow::AnimateSlot {
        
        QPoint currPos = yourRect.topLeft(); // In parent coordinates
        int moveBy = 1; // Your rect's step size
        QPoint newPos = QPoint (currPos.x() + moveBy, currPos.y());
        yourRect.moveTo(newPos);
        }
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        O 1 Reply Last reply
        2
        • Pl45m4P Pl45m4

          @Oumayma

          For your property animation you can use the example from the documentation

          https://doc.qt.io/qt-5/qpropertyanimation.html#details

          Animate / Change the geometry of your QRect over time.

          To use the timer approach you'll need a slot, where you manually increase your size, position or whatever you want to change. Just update() won't work.

          Something like:

          void MainWindow::AnimateSlot {
          
          QPoint currPos = yourRect.topLeft(); // In parent coordinates
          int moveBy = 1; // Your rect's step size
          QPoint newPos = QPoint (currPos.x() + moveBy, currPos.y());
          yourRect.moveTo(newPos);
          }
          
          O Offline
          O Offline
          Oumayma
          wrote on last edited by
          #7

          @Pl45m4 I don't really know how Slots work, but this is the code that I have tried. It gives me the following error:
          QObject::connect: No such slot MainWindow::AnimateSlot(rec)
          QObject::connect: (receiver name: 'MainWindow')

          I will continue to inform myself, but if you see what I'm doing wrong, I would appreciate your help.

          void MainWindow::paintEvent(QPaintEvent *e){
          Q_UNUSED(e);
          QPainter MyPainter(this);
          relleno(&MyPainter);
          }

          void MainWindow::relleno(QPainter *paint){
          QRect rec = QRect(0, 0, 95, 35);
          paint->fillRect(rec,QBrush("#c56c00"));
          QTimer timer = new QTimer(this);
          connect(timer, SIGNAL(timeout()), this, SLOT(AnimateSlot(rec)));
          timer->start(1000);
          }
          void MainWindow::AnimateSlot(QRect rec){
          QPoint currPos = rec.topLeft(); // In parent coord
          /
          int moveBy = 180; // Your rect's step size
          QPoint newPos = QPoint (currPos.x() + moveBy, currPos.y());
          rec.moveTo(newPos);
          }

          Thanks a lot.

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

            Hi,

            You can't use parameters to slots in the connection call like that one.

            You can consider using lambdas.

            In any case, you're doing something nasty here, each time paintEvent (and that can happen quite a lot) is called you are creating a new QTimer object.

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

            O 1 Reply Last reply
            2
            • SGaistS SGaist

              Hi,

              You can't use parameters to slots in the connection call like that one.

              You can consider using lambdas.

              In any case, you're doing something nasty here, each time paintEvent (and that can happen quite a lot) is called you are creating a new QTimer object.

              O Offline
              O Offline
              Oumayma
              wrote on last edited by
              #9

              @SGaist I understand thank you very much for the help, I will continue investigating how to solve my problem.

              1 Reply Last reply
              0
              • O Oumayma

                @Pl45m4 I don't really know how Slots work, but this is the code that I have tried. It gives me the following error:
                QObject::connect: No such slot MainWindow::AnimateSlot(rec)
                QObject::connect: (receiver name: 'MainWindow')

                I will continue to inform myself, but if you see what I'm doing wrong, I would appreciate your help.

                void MainWindow::paintEvent(QPaintEvent *e){
                Q_UNUSED(e);
                QPainter MyPainter(this);
                relleno(&MyPainter);
                }

                void MainWindow::relleno(QPainter *paint){
                QRect rec = QRect(0, 0, 95, 35);
                paint->fillRect(rec,QBrush("#c56c00"));
                QTimer timer = new QTimer(this);
                connect(timer, SIGNAL(timeout()), this, SLOT(AnimateSlot(rec)));
                timer->start(1000);
                }
                void MainWindow::AnimateSlot(QRect rec){
                QPoint currPos = rec.topLeft(); // In parent coord
                /
                int moveBy = 180; // Your rect's step size
                QPoint newPos = QPoint (currPos.x() + moveBy, currPos.y());
                rec.moveTo(newPos);
                }

                Thanks a lot.

                O Offline
                O Offline
                Oumayma
                wrote on last edited by
                #10

                @Oumayma I have corrected some of the things mentioned but it still does not work, it does not give me any error but it does nothing.

                Modified code:

                MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent)

                , ui(new Ui::MainWindow)
                

                {
                ui->setupUi(this);
                setCentralWidget(ui->textEdit);
                ui->menubar->setStyleSheet(" border-style: solid;border-width: 2px;border-color: green;");
                QTimer *timer = new QTimer(this);
                connect(timer, SIGNAL(timeout()), this, SLOT(AnimateSlot()));
                timer->start(1000);

                }

                void MainWindow::paintEvent(QPaintEvent *e){
                Q_UNUSED(e);
                QPainter MyPainter(this);
                relleno(&MyPainter,rect());
                }

                void MainWindow::relleno(QPainter *paint,QRect rec){

                paint->fillRect(rec,QBrush("#c56c00"));
                

                }
                void MainWindow::AnimateSlot(){

                QPoint currPos = rect().topLeft(); // In parent coord*/
                int moveBy = 180; // Your rect's step size
                QPoint newPos = QPoint (currPos.x() + moveBy, currPos.y());
                rect().moveTo(newPos);
                

                }
                QRect MainWindow::rect(){
                QRect rec;
                rec.setRect(0, 0, 95, 35);
                return rec;
                }

                thank you very much and sorry for the inconvenience.

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

                  @Oumayma said in Problem Whit QRec and QPainter:

                  rect().moveTo(newPos);

                  What are you expecting from that ?

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

                  O 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @Oumayma said in Problem Whit QRec and QPainter:

                    rect().moveTo(newPos);

                    What are you expecting from that ?

                    O Offline
                    O Offline
                    Oumayma
                    wrote on last edited by
                    #12

                    @SGaist I want to be able to see how my rectangle moves from one position to another.

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

                      @Oumayma said in Problem With QRect and QPainter:

                      @SGaist I want to be able to see how my rectangle moves from one position to another.

                      That I understood, but you are not answering my question:

                      What do you expect from that line, or rather what do you think it does ?

                      @Oumayma said in Problem With QRect and QPainter:

                      rect().moveTo(newPos);

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

                      O 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        @Oumayma said in Problem With QRect and QPainter:

                        @SGaist I want to be able to see how my rectangle moves from one position to another.

                        That I understood, but you are not answering my question:

                        What do you expect from that line, or rather what do you think it does ?

                        @Oumayma said in Problem With QRect and QPainter:

                        rect().moveTo(newPos);

                        O Offline
                        O Offline
                        Oumayma
                        wrote on last edited by
                        #14

                        @SGaist Yes, I'm sorry. I have changed the rect () function, declaring the following in the header file:
                        QRect rec = QRect (0, 0, 95, 35);
                        Thanks to that I can use the same rec in the different functions.

                        Now my functions are these:

                        MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)

                        , ui(new Ui::MainWindow)
                        

                        {
                        ui->setupUi(this);
                        setCentralWidget(ui->textEdit);
                        ui->menubar->setStyleSheet(" border-style: solid;border-width: 2px;border-color: green;");
                        QTimer *timer = new QTimer(this);
                        connect(timer, SIGNAL(timeout()), this, SLOT(AnimateSlot()));
                        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
                        timer->start(1000);
                        }
                        void MainWindow::paintEvent(QPaintEvent *e){
                        Q_UNUSED(e);
                        QPainter MyPainter(this);
                        relleno(&MyPainter);
                        }

                        void MainWindow::relleno(QPainter *paint){
                        paint->fillRect(rec,QBrush("#c56c00"));
                        }
                        void MainWindow::AnimateSlot(){
                        QPoint currPos = rec.topLeft();
                        int moveBy = 95;
                        QPoint newPos = QPoint (currPos.x() + moveBy, currPos.y());
                        rec = QRect(0,0,89,35);
                        rec.moveTo(newPos);
                        }
                        With this I have managed to make my rec move until it ends up disappearing, now I am trying to make the rec change in size as it moves and according to the size of the corresponding Qmenu.
                        0f9c8fa1-2bc0-49da-938b-e98a660e9b31-image.png

                        I also have to ensure that when I get to the last option of the Qmenu I return to the first option of the Qmenu and so on all the time.
                        But as you can see in the following image, the rec continues until the end and disappears.
                        40efe1ab-6add-4fdf-9f30-db8faecafc1f-image.png

                        Sorry for the inconvenience and thanks for the help.

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

                          You are just moving your rectangle every time using the same offset.

                          You need to add logic to handle the number of elements you have in your menu bar and loop on that.

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

                          O 1 Reply Last reply
                          2
                          • SGaistS SGaist

                            You are just moving your rectangle every time using the same offset.

                            You need to add logic to handle the number of elements you have in your menu bar and loop on that.

                            O Offline
                            O Offline
                            Oumayma
                            wrote on last edited by
                            #16

                            @SGaist Could you guide me or give me some information regarding the handling of items for Qmenubar. Thanks a lot.

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

                              You know the number of entries of your menu bar, so use it for the loop.

                              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

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved