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. How to paint above the widget original painting?
Forum Updated to NodeBB v4.3 + New Features

How to paint above the widget original painting?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 1.2k Views 5 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.
  • D Offline
    D Offline
    Daniella
    wrote on last edited by Daniella
    #1

    I'm trying to temporarily draw a border around specific widgets at a given ocasiation, for this i'm installing an eventFilter and painting the border in the QEvent::Paint.

    This does work when its a Qframe/QWidget, but not when its a QLineEdit, QPushButton, in these, the border is not visible:

    QtWidgetsApplication10_Kzzg67opzK.png

    The border probably is being overridden by the widget paint event.
    When i change the painter to draw a rect instead of a roundedRect:
    painter.drawRect(widget->rect());

    QtWidgetsApplication10_O46gUsl2Yu.png

    In this use case, how I could make things be drawn "above" the widget original painting like its happening on the QFrame?

    bool MainWindow::eventFilter(QObject* obj, QEvent* event)
    {
        if (event->type() == QEvent::Paint)
        {
            QWidget* widget = qobject_cast<QWidget*>(obj);
            QPainter painter(widget);
            //painter.setCompositionMode(QPainter::CompositionMode_Source);
            painter.setRenderHint(QPainter::Antialiasing, true);
            painter.setPen(QPen(QColor(Qt::red), 2));
            painter.drawRoundedRect(widget->rect().adjusted(1, 1, -1, -1), 12, 12);
        }
    
        return QWidget::eventFilter(obj, event);
    }
    
    
    
    MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindowClass())
    {
        ui->setupUi(this);
    
        QFrame* frame = new QFrame(this);
        frame->setGeometry(0, 30, 200, 32);
        frame->installEventFilter(this);
        frame->setStyleSheet("background-color: blue; border-radius: 12px;");
        frame->show();
    
        QLineEdit* lineEdit = new QLineEdit(this);
        lineEdit->setGeometry(0, 90, 200, 32);
        lineEdit->installEventFilter(this);
        lineEdit->setStyleSheet("background-color: blue; border-radius: 12px;");
        lineEdit->show();
    }
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • D Daniella

      @Christian-Ehrlicher how to override it from the filter?

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Daniella Event filter is for filtering events i.e. it happens before the event and can pass the event on or stop it. That being said you can't paint over what's done in paintEvent when you're in the filter, because filtering happens before paintEvent.
      As Christian said you should paint in paintEvent of the widget. Call the base implementation and then add your painting on top of it. If you don't want to modify the widget class your other option is to make an "overlay" widget on top of that one and paint in it, but in any case painting goes in paintEvent.

      jeremy_kJ 1 Reply Last reply
      1
      • D Daniella

        I'm trying to temporarily draw a border around specific widgets at a given ocasiation, for this i'm installing an eventFilter and painting the border in the QEvent::Paint.

        This does work when its a Qframe/QWidget, but not when its a QLineEdit, QPushButton, in these, the border is not visible:

        QtWidgetsApplication10_Kzzg67opzK.png

        The border probably is being overridden by the widget paint event.
        When i change the painter to draw a rect instead of a roundedRect:
        painter.drawRect(widget->rect());

        QtWidgetsApplication10_O46gUsl2Yu.png

        In this use case, how I could make things be drawn "above" the widget original painting like its happening on the QFrame?

        bool MainWindow::eventFilter(QObject* obj, QEvent* event)
        {
            if (event->type() == QEvent::Paint)
            {
                QWidget* widget = qobject_cast<QWidget*>(obj);
                QPainter painter(widget);
                //painter.setCompositionMode(QPainter::CompositionMode_Source);
                painter.setRenderHint(QPainter::Antialiasing, true);
                painter.setPen(QPen(QColor(Qt::red), 2));
                painter.drawRoundedRect(widget->rect().adjusted(1, 1, -1, -1), 12, 12);
            }
        
            return QWidget::eventFilter(obj, event);
        }
        
        
        
        MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindowClass())
        {
            ui->setupUi(this);
        
            QFrame* frame = new QFrame(this);
            frame->setGeometry(0, 30, 200, 32);
            frame->installEventFilter(this);
            frame->setStyleSheet("background-color: blue; border-radius: 12px;");
            frame->show();
        
            QLineEdit* lineEdit = new QLineEdit(this);
            lineEdit->setGeometry(0, 90, 200, 32);
            lineEdit->installEventFilter(this);
            lineEdit->setStyleSheet("background-color: blue; border-radius: 12px;");
            lineEdit->show();
        }
        
        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        When you want custom painting you should override the paintEvent.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        D 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          When you want custom painting you should override the paintEvent.

          D Offline
          D Offline
          Daniella
          wrote on last edited by
          #3

          @Christian-Ehrlicher how to override it from the filter?

          Christian EhrlicherC Pl45m4P Chris KawaC 3 Replies Last reply
          0
          • D Daniella

            @Christian-Ehrlicher how to override it from the filter?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @Daniella said in How to paint above the widget original painting?:

            how to override it from the filter?

            ?
            You have to override the paint function.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • D Daniella

              @Christian-Ehrlicher how to override it from the filter?

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

              @Daniella said in How to paint above the widget original painting?:

              how to override it from the filter?

              I'm wondering whether you get any warning that you should not paint outside the widget's paint event.
              Why you want to use eventFilter?!


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

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • D Daniella

                @Christian-Ehrlicher how to override it from the filter?

                Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Daniella Event filter is for filtering events i.e. it happens before the event and can pass the event on or stop it. That being said you can't paint over what's done in paintEvent when you're in the filter, because filtering happens before paintEvent.
                As Christian said you should paint in paintEvent of the widget. Call the base implementation and then add your painting on top of it. If you don't want to modify the widget class your other option is to make an "overlay" widget on top of that one and paint in it, but in any case painting goes in paintEvent.

                jeremy_kJ 1 Reply Last reply
                1
                • D Daniella has marked this topic as solved on
                • Chris KawaC Chris Kawa

                  @Daniella Event filter is for filtering events i.e. it happens before the event and can pass the event on or stop it. That being said you can't paint over what's done in paintEvent when you're in the filter, because filtering happens before paintEvent.
                  As Christian said you should paint in paintEvent of the widget. Call the base implementation and then add your painting on top of it. If you don't want to modify the widget class your other option is to make an "overlay" widget on top of that one and paint in it, but in any case painting goes in paintEvent.

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by
                  #7

                  @Chris-Kawa said in How to paint above the widget original painting?:

                  That being said you can't paint over what's done in paintEvent when you're in the filter, because filtering happens before paintEvent.

                  ...Unless eventFilter() calls the target object's event(), followed by any postprocessing, and finally returning true to subvert the normal call to QObject::event().

                  This breaks if there is more than one event filter expecting to exploit the same path, and is unlikely to make anyone happy.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  Chris KawaC 1 Reply Last reply
                  0
                  • jeremy_kJ jeremy_k

                    @Chris-Kawa said in How to paint above the widget original painting?:

                    That being said you can't paint over what's done in paintEvent when you're in the filter, because filtering happens before paintEvent.

                    ...Unless eventFilter() calls the target object's event(), followed by any postprocessing, and finally returning true to subvert the normal call to QObject::event().

                    This breaks if there is more than one event filter expecting to exploit the same path, and is unlikely to make anyone happy.

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @jeremy_k Well yeah, technically, but why work this hard to shoot yourself in the foot when there's a perfectly valid solution in line with what the system was actually designed for.

                    jeremy_kJ 1 Reply Last reply
                    0
                    • Chris KawaC Chris Kawa

                      @jeremy_k Well yeah, technically, but why work this hard to shoot yourself in the foot when there's a perfectly valid solution in line with what the system was actually designed for.

                      jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by jeremy_k
                      #9

                      @Chris-Kawa said in How to paint above the widget original painting?:

                      @jeremy_k Well yeah, technically, but why work this hard to shoot yourself in the foot when there's a perfectly valid solution in line with what the system was actually designed for.

                      Sometimes the lesson is more memorably when someone else loses a foot.
                      :D

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      Pl45m4P 1 Reply Last reply
                      0
                      • jeremy_kJ jeremy_k

                        @Chris-Kawa said in How to paint above the widget original painting?:

                        @jeremy_k Well yeah, technically, but why work this hard to shoot yourself in the foot when there's a perfectly valid solution in line with what the system was actually designed for.

                        Sometimes the lesson is more memorably when someone else loses a foot.
                        :D

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

                        @jeremy_k said in How to paint above the widget original painting?:

                        Sometimes the lesson is more memorably when someone else loses a foot.
                        :D

                        Emphasizing someone else losing a foot.
                        🤣


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

                        ~E. W. Dijkstra

                        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