简体   繁体   English

“错误:C2275:'QMouseEvent':非法使用此类型作为表达式”

[英]“error: C2275: 'QMouseEvent' : illegal use of this type as an expression”

'I am currently having an issue tryinh to compile this program. '我目前在尝试编译该程序时遇到问题。 The program is supposed to show the coordinates of the mouse on a GUI QWidget The error is in line 6 of the mainwindow.cpp file' 该程序应该在GUI QWidget上显示鼠标的坐标。错误在mainwindow.cpp文件的第6行中。

//header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QApplication>
#include <QMainWindow>
#include <QMouseEvent>
#include <QMessageBox>
#include <QWidget>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();

    void mouseReleaseEvent(QMouseEvent * event);

    ~MainWindow();

private:
    Ui::MainWindow *ui;

    QMessageBox *msgBox;
};

#endif // MAINWINDOW_H

' mainwindow.cpp file' 'mainwindow.cpp文件'

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

MainWindow::MainWindow()
{
   MainWindow::mouseReleaseEvent (QMouseEvent * event);
}

void MainWindow::mouseReleaseEvent(QMouseEvent * event)
{
    msgBox = new QMessageBox();
    msgBox -> setWindowTitle("Coordinates");
    msgBox -> setText("You released the button");
    msgBox -> show();
}

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

'main.cpp' '的main.cpp'

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow *w = new MainWindow();

    w->setWindowTitle(QString::fromUtf8("QT-capture mouse release"));
            w->resize(300, 250);


    w->show();

    return a.exec();
}

Please help, I know it is something related to pointers and possibly mutators, but I can't see it yet. 请帮忙,我知道它与指针和可能的变数有关,但是我还看不到。 Thank you. 谢谢。

This is illegal: 这是非法的:

MainWindow::MainWindow()
{
    // illegal:
    MainWindow::mouseReleaseEvent (QMouseEvent * event);
}

If you wish to call the handler manually, you need to create an event and pass it: 如果希望手动调用处理程序,则需要创建一个事件并将其传递:

MainWindow::MainWindow()
{
    QMouseEvent event;
    MainWindow::mouseReleaseEvent(&event);
}

But you then need to set the QMouseEvent attributes correctly/ It's hard to tell how to do so without knowing why you want to do that. 但是您随后需要正确设置QMouseEvent属性/在不知道为什么要这么做的情况下很难说出该方法。

Wht are you doing that? 你在干嘛 Those events are emitted automatically upon mouse activity, you don't need to manually call mouseReleaseEvent, it will be called when you release the mouse button. 这些事件在鼠标活动时自动发出,您无需手动调用mouseReleaseEvent,将在释放鼠标按钮时调用它。

If you want to show mouse position, I suggest that you: 如果要显示鼠标位置,建议您:

  • Replace mouseReleaseEvent by mouseMoveEvent mouseMoveEvent替换mouseReleaseEvent
  • Simply remove the call you have in MainWindow::MainWindow() 只需删除您在MainWindow::MainWindow()中进行的调用
  • Have MainWindow::mouseMoveEvent(QMouseEvent * event) write mouse coordinates in a label of the main window rather than using a mesage box (format a QString with mouse coordinates using QMouseEvent::pos and changing label text using QLabel::setText ) MainWindow::mouseMoveEvent(QMouseEvent * event)在主窗口的标签中而不是使用消息框写入鼠标坐标(使用QMouseEvent::pos用鼠标坐标格式化QString并使用QLabel::setText更改标签文本)

Like that: 像那样:

void MainWindow::mouseMoveEvent(QMouseEvent * event)
{ 
    std::stringstream str;
    str << "Mouse position is " << event->pos.x() << ";" << event->pos().y();
    ui->label->setText( str.str().c_str() );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM