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 Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 2.0k 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.
  • W Offline
    W Offline
    Wosch
    wrote on 13 Dec 2017, 11:21 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 1 Reply Last reply 15 Dec 2017, 08:01
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 13 Dec 2017, 12:10 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 14 Dec 2017, 01:20
      0
      • M mrjj
        13 Dec 2017, 12:10

        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 14 Dec 2017, 01:20 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.

        M 1 Reply Last reply 14 Dec 2017, 07:58
        0
        • W Wosch
          14 Dec 2017, 01:20

          @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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 14 Dec 2017, 07:58 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 14 Dec 2017, 12:06
          0
          • M mrjj
            14 Dec 2017, 07:58

            @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 14 Dec 2017, 12:06 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.

            J 1 Reply Last reply 15 Dec 2017, 06:19
            0
            • W Wosch
              14 Dec 2017, 12:06

              @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.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 15 Dec 2017, 06:19 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
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 15 Dec 2017, 07:20 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
                  13 Dec 2017, 11:21

                  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 Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 15 Dec 2017, 08:01 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 15 Dec 2017, 11:43 last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    1/9

                    13 Dec 2017, 11:21

                    • Login

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