更新不调用paintEvent

更新不调用paintEvent

本文介绍了更新不调用paintEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`我检索了鼠标光标的位置,我想通过paintevent画一条线,但没有被更新调用,所以什么也没发生

.cpp

void MainWindow::mousePressEvent(QMouseEvent * event){if(event->button() == Qt::LeftButton){如果(mFirstClick){mStartX = event->x();mStartY = event->y();mFirstClick = 假;}否则如果(!mFirstClick){mEndX = 事件->x();mEndY = 事件-> y();mFirstClick = true;mPaintFlag = 真;更新();}}ui->label_7->setText(QString::number(mStartX)+ ", "+QString::number(mStartY) +"//"+QString::number(mEndX)+", "+QString::数字(mEndY));}void MainWindow::paintEvent(QPaintEvent * 事件){QMainWindow::paintEvent(事件);如果(mPaintFlag=真){QPainter 画家(this);QPen 画笔(Qt::red);画笔.setWidth(4);QPen 线笔(Qt::red);linepen.setWidth(4);Q点p1;p1.setX(mStartX);p1.setY(mStartY);画家.setPen(画笔);画家.drawPoint(p1);Q点p2;p2.setX(mEndX);p2.setY(mEndY);画家.setPen(画笔);画家.drawPoint(p2);画家.setPen(线笔);画家.drawLine(p1, p2);}}

.h

保护:void mousePressEvent(QMouseEvent * e);虚拟无效paintEvent(QPaintEvent * e)覆盖;

我做了重绘而不是更新,但没有显示任何内容,我如何调用paintevent来绘制线条.

解决方案

不确定 OP 做错了什么.至少,OP 没有公开

因此,我得出结论:OP的错误一定在未公开的代码中.虽然,我没有完全复制 OP 的代码,但我没有看到任何可能阻止渲染那条红线的关键部分.

`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.

解决方案

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.

这篇关于更新不调用paintEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:58