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. QPainter does not work though I put them in the paintEvent
Forum Updated to NodeBB v4.3 + New Features

QPainter does not work though I put them in the paintEvent

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.2k 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.
  • A Offline
    A Offline
    Aaron Kim
    wrote on last edited by Aaron Kim
    #1

    Canvas is a derived class of QLabel

    void Canvas::paintEvent(QPaintEvent*){
        QPainter painter(this);
        painter.setPen(Qt::black);
        painter.setFont(QFont("MaestroTimes", 10));
        std::cout << height() << " " << width() << std::endl;
        painter.drawLine(0, static_cast<int>(height()/2), width()-1, static_cast<int>(height()/2));
    }
    
    QLabel* ScoreMaker::makeScore(const QQueue<CommandTimer*>* timers, int interval, int lastInterval, int height){
        int width = 30;
        Canvas* score = new Canvas();
        score->setFixedWidth(static_cast<int>((width + lastInterval) / interval));
        score->setFixedHeight(height);
        score->paintEvent(nullptr);
        return score;
    }
    

    Also, update() and repaint() does not trigger paintEvent so I had to use

    score->paintEvent(nullptr)
    
    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active
    QPainter::setFont: Painter not active
    

    Those errors occur even thought I put them in the paintEvent

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

      Hi,

      Don’t call paintEvent by hand, that’s for the system to do. It will be called automatically when needed. For example, when your widget is shown, when you move it, etc.

      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
      3
      • A Offline
        A Offline
        Aaron Kim
        wrote on last edited by
        #3

        It seems that I have to set parent of widget before calling repaint(). Otherwise, paintEvent is not called.

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

          That’s not mandatory, however you need to call show on the widget depending on how you use it.

          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