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

时间:2022-01-10 18:53:46

'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'

我现在有一个问题tryinh来编译这个程序。程序应该在GUI QWidget上显示鼠标的坐标,错误在主窗口的第6行。cpp文件'

//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'

的主窗口。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.

请帮忙,我知道它与指针和可能的突变体有关,但我还看不出来。谢谢你!

1 个解决方案

#1


1  

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
  • 取代mouseReleaseEvent摇干的
  • Simply remove the call you have in 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)
  • 有主窗口::mouseMoveEvent(QMouseEvent * event)在主窗口的标签中写鼠标坐标,而不是使用一个mesage框(使用QMouseEvent::pos和更改标签文本,使用QMouseEvent:::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() );
}

#1


1  

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
  • 取代mouseReleaseEvent摇干的
  • Simply remove the call you have in 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)
  • 有主窗口::mouseMoveEvent(QMouseEvent * event)在主窗口的标签中写鼠标坐标,而不是使用一个mesage框(使用QMouseEvent::pos和更改标签文本,使用QMouseEvent:::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() );
}