未找到QT 5.6 update()函数标识符

时间:2016-06-05 11:48:41

标签: c++ qt

我正在尝试创建一个程序,在按钮单击时将绘制一个函数。它看起来像这样(mainwindow.h)

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QtCore>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
void paintEvent(QPaintEvent *);
~MainWindow();

private slots:
void on_pushButton_clicked();

protected:
 void paintEvent(QPaintEvent *event); // This is re-implemented from QWidget
protected slots:
 void draw();
private:
 bool drawTheLines;

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
     delete ui;
}

 void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
     if(drawTheLines){
       //draw function here
            }
     QWidget::paintEvent(event);


}
void draw()
   {
      bool drawTheLines = true;
      update();
   }

问题是 - 编译失败,错误显示“C3861:udpate:Identifier not found”

2 个答案:

答案 0 :(得分:3)

你忘了在cpp:

中声明MainWindow :: draw()
void MainWindow::draw()
{
    drawTheLines = true;
    update();
}

答案 1 :(得分:1)

未定义update函数!

也许您想要调用QMainWindow的update函数,但函数draw不是QMainWindow的成员函数,因此函数{{1}中没有隐式this }}

有两种方法可以解决它,但你应该选择例外的方法!

  1. 使draw函数成为您班级的成员函数 draw
  2. 让您的MainWindow功能像这样

    draw
  3. 但是,我认为您可能想要第一个解决方案,因为函数void draw(QMainWindow *window){ bool drawTheLines = true; window->update(); } 中的drawTheLines变量尚未定义。