Hello)

There are two buttons on the form, QPushButton1 and QPushButton2. I need to draw a line between the buttons. Those. pressed the QPushButton1 button and pushed the QPushButton2 button and pressed there. And then a line should be drawn on the form. How to implement it? It is advisable that after drawing the QPushButton1 and QPushButton2 lines are reset.

When QPushButton2 is released, for some reason it reacts only in its own area of ​​the button. Those. They pressed the QPushBUtton2 button and pressed it there, only this way it works.

1) Do I correctly update () every time?

2) Why does pressing the button work only in its area?

    2 answers 2

    Code itself

    void Dialog::paintEvent(QPaintEvent *) { if (_isBeingMouse1 &&_isBeingMouse2 == true) { QPainter p(this); // Π‘ΠΎΠ·Π΄Π°Ρ‘ΠΌ Π½ΠΎΠ²Ρ‹ΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Ρ€ΠΈΡΠΎΠ²Π°Π»ΡŒΡ‰ΠΈΠΊΠ° p.setPen(Qt::red);// Настройки рисования p.setBrush(Qt::white);// Настройки рисования // p.drawLine(10,10,100,100); // РисованиС Π»ΠΈΠ½ΠΈΠΈ // painter->drawLine(firstTextWidget.geometry().center(),secondTextWidget.geometry().center()); p.drawLine(ui->pushButton->geometry().center(),ui->pushButton_2->geometry().center()); } else { //QPainter painter(this); // qDebug()<< "text"; } } void Dialog::on_pushButton_clicked() { if(_isBeingMouse1 == true) _isBeingMouse1 = false; else _isBeingMouse1 = true; update(); } void Dialog::on_pushButton_2_released() { if(_isBeingMouse2 == true) _isBeingMouse2 = false; else _isBeingMouse2 = true; update(); } 

      Good day!

      About the update() method (from the documentation):

      This function doesn’t cause an immediate repaint; Qt Returns to the main event loop. I love you to make it clear.

      Based on these words, from the fact that you are doing update() in the slot processing a button, no particular effect, i.e. the form will be redrawn when the message loop reaches paintEvent() , on the whole, you don't do anything wrong . repaint() inside paintEvent () is highly discouraged (the program will go into an infinite loop).

      Regarding the release: if in a nutshell, then so conceived by the framework itself, then what do you want to do better using the toggle() slot and the down , check and checked properties

      And finally, the most important thing: see the example of Diagram Scene Example from the Qt delivery, there is almost what you want: drawing lines between two objects (even with arrows).

      I think if you replace the objects that are in the examples with the heirs of QGraphicsWidget or QGraphicsProxyWidget, then everything should work out!