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. Trying to create a direction indicator. Other methods than QPainter?
Forum Update on Monday, May 27th 2025

Trying to create a direction indicator. Other methods than QPainter?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.9k Views
  • 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.
  • W Offline
    W Offline
    Wosch
    wrote on last edited by
    #1

    I'm currently using QPainter and designed my lamps for the direction indicator. I want to change the mainwindow by clicking a button. So when I click the button for the left direction indicator, the mainwindow should update the background-colour of my polygon for the left blinker every second. Is there any better method than using QPainter?

    Thanks for your answers.

    J.HilkJ 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      What would make it a better method than QPainter ?
      Also how do u draw the poly?
      Using painter ?

      W 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        What would make it a better method than QPainter ?
        Also how do u draw the poly?
        Using painter ?

        W Offline
        W Offline
        Wosch
        wrote on last edited by Wosch
        #3

        @mrjj yes right now I'm using the painter. I've created a paintevent and then used the qpainter to create the polygons. But i don't know, if i can edit the paintevent and the specific painter, so that the indicators change the colour every time skip. This is all happening in the mainwindow.cpp.

        mrjjM 1 Reply Last reply
        0
        • W Wosch

          @mrjj yes right now I'm using the painter. I've created a paintevent and then used the qpainter to create the polygons. But i don't know, if i can edit the paintevent and the specific painter, so that the indicators change the colour every time skip. This is all happening in the mainwindow.cpp.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Wosch
          it sounds fine to me but not sure what
          "direction indicator. " really is. How it should be drawn but
          painter is fast and flexible.

          W 1 Reply Last reply
          0
          • mrjjM mrjj

            @Wosch
            it sounds fine to me but not sure what
            "direction indicator. " really is. How it should be drawn but
            painter is fast and flexible.

            W Offline
            W Offline
            Wosch
            wrote on last edited by
            #5

            @mrjj it should look like the blinker in your car or better like the dashboard. So when i press the button for the left blinker, the left arrow should switch from white to yellow and back in a time scale. But i'm not sure how to call the qtpaintevent in another method(button pressed()) so i can change the painter I created for the arrows.

            jsulmJ 1 Reply Last reply
            0
            • W Wosch

              @mrjj it should look like the blinker in your car or better like the dashboard. So when i press the button for the left blinker, the left arrow should switch from white to yellow and back in a time scale. But i'm not sure how to call the qtpaintevent in another method(button pressed()) so i can change the painter I created for the arrows.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Wosch No need to call paintEvent from other method. In you button slot you simply set a variable. In your paintEvent you check the value of this variable and depending on it draw what needs to be drown.

              void MainWindow::onButtonCliecked()
              {
                  drawArrow = true;
              }
              
              void MainWindowPaintEvent(...)
              {
                  if (drawArrow) {
                      // Draw arrow
                  }
              }
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Wosch
                Hi
                To triggers something to re-draw, you can call its update()
                You will never call its paintEvent directly.

                So to add to @jsulm

                often you would do
                void MainWindow::onButtonCliecked()
                {
                drawArrow = true;
                update();
                }

                to make it redraw as soon as you change arrow

                1 Reply Last reply
                0
                • W Wosch

                  I'm currently using QPainter and designed my lamps for the direction indicator. I want to change the mainwindow by clicking a button. So when I click the button for the left direction indicator, the mainwindow should update the background-colour of my polygon for the left blinker every second. Is there any better method than using QPainter?

                  Thanks for your answers.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @Wosch hi, since you're specifically asking for an alternative, I'll post a custom class (of a QPushButton in this case) that has a "blinking-functionality" using Q_Property and StyleSheets

                  #ifndef CBUTTON_H
                  #define CBUTTON_H
                  
                  #include <QStyle>
                  #include <QPushButton>
                  #include <QTimer>
                  
                  class cButton : public QPushButton
                  {
                      Q_OBJECT
                      Q_PROPERTY(bool state READ isState WRITE changeState)
                  
                  public:
                      explicit cButton(QWidget * parent = 0) :  QPushButton(parent){
                          this->setStyleSheet("QPushButton{background-color: black;}
                                                                   QPushButton[state = true]{background-color: red;}");
                          changeTimer.setInterval(1000);
                          connect(&changeTimer, &QTimer::timeout, this, cButton::flipState);
                      }
                      void start(){changeTimer.start());
                      void stop(){changeTimer.stop());
                  
                      bool isState(){return state;}
                  
                  public slots:
                     void flipState(){changeState(!isState());}
                  
                      void changeState(bool b){
                          if(state != b){
                               state = b;
                               style()->unpolish(this);
                               style()->polish(this);
                              repaint();
                         }
                      }
                  
                  protected:
                      QTimer changeTimer;
                      bool state = false;
                  
                  };
                  
                  #endif // CBUTTON_H
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • W Offline
                    W Offline
                    Wosch
                    wrote on last edited by
                    #9

                    @J-Hilk @jsulm @mrjj Thank you guys. That's what i was looking for.

                    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