update does not call paintEvent? [closed]

断了今生、忘了曾经 提交于 2019-12-24 23:28:49

问题


`I retrieved the position of the mouse cursor and I want to draw a line by paintevent but is not called by update so nothing is happening

.cpp


void MainWindow::mousePressEvent(QMouseEvent * event)
   {
       if(event->button() == Qt::LeftButton)
       {
           if(mFirstClick)
           {
               mStartX = event->x();
               mStartY = event->y();
               mFirstClick = false;
           }
           else if(!mFirstClick)
           {
               mEndX = event->x();
               mEndY = event->y();
               mFirstClick = true;

               mPaintFlag = true;
               update();
           }


       }
       ui->label_7->setText(QString::number(mStartX)+ ", "+QString::number(mStartY) +"//"+QString::number(mEndX)+ ", "+QString::number(mEndY));


   }

void MainWindow::paintEvent(QPaintEvent * event)
    {
       QMainWindow::paintEvent(event);


        if(mPaintFlag=true)
        {

            QPainter painter(this);
            QPen paintpen(Qt::red);
            paintpen.setWidth(4);

            QPen linepen(Qt::red);
            linepen.setWidth(4);

            QPoint p1;
            p1.setX(mStartX);
            p1.setY(mStartY);

            painter.setPen(paintpen);
            painter.drawPoint(p1);

            QPoint p2;
            p2.setX(mEndX);
            p2.setY(mEndY);

            painter.setPen(paintpen);
            painter.drawPoint(p2);

            painter.setPen(linepen);
            painter.drawLine(p1, p2);

        }

}

.h


protected:

    void mousePressEvent(QMouseEvent * e);
    virtual  void paintEvent(QPaintEvent * e) override;

I did repaint instead of update but nothing is displayed ,how can i make the call to paintevent to draw the line .


回答1:


Not sure what OP did wrong. At least, OP didn't expose an MCVE.

Out of curiosity, I did one on my own – testQMainWindowPaint.cc:

#include <QtWidgets>

class MainWindow: public QMainWindow {

  private:
    QPoint start, end;
    bool firstClick;

  public:
    MainWindow();
    virtual ~MainWindow() = default;
    MainWindow(const MainWindow&) = delete;
    MainWindow& operator=(const MainWindow&) = delete;

  protected:
    virtual void mousePressEvent(QMouseEvent *pQEvent) override;
    virtual void paintEvent(QPaintEvent *pQEvent) override;
};

MainWindow::MainWindow():
  QMainWindow(),
  start(0, 0), end(0, 0), firstClick(true)
{ }

void MainWindow::mousePressEvent(QMouseEvent *pQEvent)
{
  if (pQEvent->button() == Qt::LeftButton) {
    (firstClick ? start : end) = pQEvent->pos();
    firstClick = !firstClick;
    update();
    pQEvent->accept();
  }
}

void MainWindow::paintEvent(QPaintEvent *pQEvent)
{
  QMainWindow::paintEvent(pQEvent);
  if (!firstClick) return;
  QPainter painter(this);
  QPen pen(Qt::red);
  pen.setWidth(4);
  painter.setPen(pen);
  painter.drawLine(start, end);
}

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // init GUI
  MainWindow winMain;
  winMain.show();
  // runtime loop
  return app.exec();
}

Compile with testQMainWindowPaint.pro:

SOURCES = testQMainWindowPaint.cc

QT += widgets

Compiled in cygwin64 on Windows 10:

$ qmake-qt5 testQMainWindowPaint.pro

$ make
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQMainWindowPaint.o testQMainWindowPaint.cc
g++  -o testQMainWindowPaint.exe testQMainWindowPaint.o   -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread 

$ ./testQMainWindowPaint 
Qt Version: 5.9.4

After clicking twice, I made the following snapshot:

Hence, I came to the conclusion: The mistake of OP must be in the non-exposed code. Although, I reproduced code of OP not exactly, I didn't see any critical part which might've prevented rendering of that red line.



来源:https://stackoverflow.com/questions/56481098/update-does-not-call-paintevent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!