D
D
Dima Bck2022-02-22 19:59:13
Qt
Dima Bck, 2022-02-22 19:59:13

Why does it crash using update and repaint?

There are 3 functions.
1: calls void Qwidget::repaint ()
2: calls void Qwidget::update ()
Both of these functions work in different threads (so that the 2nd thread is relatively important, does not arrange races 1st thread did through repaint - immediate rendering without building / adding to layout)
3: and there is the paintEvent handler itself at the very beginning of the function there is a mutex so that there is no crash due to the simultaneous use of shared resources ...
The question itself is: why when I write update in both (1st and 2nd) everything is fine, but sometimes what is important does not come (it is overwritten), when both repaints on a friend’s computer lay everything down for me, when I do as I wrote in paragraphs (1st and 2nd), then if everything is fine in the release, and if in the debug it first starts to lag, and then falls at random points in the program, what am I doing wrong, is it possible to do this (or should one be called) what are the options fix this?
UPD:
Something like this code:
TestClass.h:

class TestClass : public QWidget, public QObject {
    Q_OBJECT
    mutable QMutex mDataMutex;

   public:
    TestClass (QWidget *parent = 0);
    ~TestClass ();

   public slots:
    void funk_1();
    void funk_2();
};

TestClass.cpp
#include "TestClass.h"

TestClass::TestClass(QWidget *parent)
    : QWidget(parent){
    this->setWindowFlags(Qt::Widget);
    this->setAttribute(Qt::WA_PaintOnScreen);
    this->setWindowTitle("Win_1");
  this->setWindowFlags(((windowFlags() | Qt::CustomizeWindowHint) & ~Qt::WindowCloseButtonHint ));
}

void TestClass::funk_1() {
  repaint();
}

void TestClass::paintEvent(QPaintEvent* event) {
  QMutexLocker ml(&mDataMutex);

  if (mPainter.begin(this)) {
    if (!mOutFrame->isNull()) {
      mPainter.drawImage(imgPos, *Frame);
    }
    mPainter.end();
  }
}

void TestClass::funk_2() {
  update();
}

TestClass::funk_2 is called about 50 times per second, and TestClass::funk_1 is called 7 times per second. What are the variables:
if (!mOutFrame->isNull()) {
      mPainter.drawImage(imgPos, *Frame);
    }

There is no need to ask, the big class took only what concerns a specific problem, and so each variable is what is needed for mPainter.drawImage.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2022-02-23
@provokator2020

I don't really understand what specific problem you are trying to solve. If you write, it will be easier to help.
Just in case, I will clarify that what you described is the consequences of a potentially incorrect solution to some real problem. For example, there are already ready-made solutions for working with animation.
In terms of code, at the very least you need to call event->accept() and attach the fun c _1/2 slots with a Qt::QueuedConnection argument (rather than calling them directly like obj->foo()). Then the mutex is not needed.
Regarding the object of type QPainter, it is better to create it in the event handler, as recommended in the official documentation.
To the question "why does it accidentally fall?" it is simply impossible to answer without having at least the complete application code, compilation and startup options, and knowledge of the operating system. After that, you need to build Qt in debug mode, build the application with it and watch the threads in the debugger and their state. But I'm not sure that this is all necessary, given the first paragraph. Strictly speaking, the problem may not be in the section of code that you have given, and the code may need to be written completely differently. Very little data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question