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

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

    @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

  • Lifetime Qt Champion

    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;

    to make it redraw as soon as you change arrow

  • Moderators

    @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_PROPERTY(bool state READ isState WRITE changeState)
        explicit cButton(QWidget * parent = 0) :  QPushButton(parent){
            this->setStyleSheet("QPushButton{background-color: black;}
                                                     QPushButton[state = true]{background-color: red;}");
            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;
        QTimer changeTimer;
        bool state = false;
    #endif // CBUTTON_H

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

Log in to reply