[英]“error: C2275: 'QMouseEvent' : illegal use of this type as an expression”
'我目前在嘗試編譯該程序時遇到問題。 該程序應該在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文件'
#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'
#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();
}
請幫忙,我知道它與指針和可能的變數有關,但是我還看不到。 謝謝。
這是非法的:
MainWindow::MainWindow()
{
// illegal:
MainWindow::mouseReleaseEvent (QMouseEvent * event);
}
如果希望手動調用處理程序,則需要創建一個事件並將其傳遞:
MainWindow::MainWindow()
{
QMouseEvent event;
MainWindow::mouseReleaseEvent(&event);
}
但是您隨后需要正確設置QMouseEvent屬性/在不知道為什么要這么做的情況下很難說出該方法。
你在干嘛 這些事件在鼠標活動時自動發出,您無需手動調用mouseReleaseEvent,將在釋放鼠標按鈕時調用它。
如果要顯示鼠標位置,建議您:
mouseMoveEvent
替換mouseReleaseEvent
MainWindow::MainWindow()
中進行的調用 MainWindow::mouseMoveEvent(QMouseEvent * event)
在主窗口的標簽中而不是使用消息框寫入鼠標坐標(使用QMouseEvent::pos
用鼠標坐標格式化QString
並使用QLabel::setText
更改標簽文本) 像那樣:
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.