子类 QGraphicsScene 出错
Posted
技术标签:
【中文标题】子类 QGraphicsScene 出错【英文标题】:Error With subclass QGraphicsScene 【发布时间】:2018-04-28 21:00:13 【问题描述】:我正在尝试将省略号添加到 Qt 中的场景中,但出现此错误:
XXXXXX/mainwindow.cpp:20:错误:“类 mqlabel”没有名为“setScene”的成员 ui->imageLabel->setScene(场景);
还有这个:
XXXXXX/build-MyPic-Desktop_Qt_5_10_1_GCC_64bit-Debug/ui_mainwindow.h:136: 错误: 没有匹配函数调用‘QGridLayout::addWidget(mqlabel*&, int, int, int, int)’ gridLayout_2->addWidget(imageLabel, 0, 0, 3, 1); ^
重要的是,我将 imageLabel(我在 .ui 文件中的场景)提升为 mqlabel
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QGraphicsScene>
#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
namespace Ui
class MainWindow;
//class MainWindow : public QGraphicsView
class MainWindow : public QMainWindow
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
//explicit MainWindow(QGraphicsView *parent = 0);
~MainWindow();
private slots:
void on_actionOpen_triggered();
void on_actionExit_triggered();
void mouse_pressed();
void mouse_pos();
private:
Ui::MainWindow *ui;
QImage image;
QGraphicsScene* scene;
QGraphicsItem* item;
QGraphicsEllipseItem *ellipse;
QGraphicsView *view = new QGraphicsView(this);
;
#endif // MAINWINDOW_H
MainWINdow.cpp
#include <QtWidgets>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "mqlabel.h"
using namespace std;
//MainWindow::MainWindow(QGraphicsView *parent) :
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui->setupUi(this);
connect(ui->imageLabel, SIGNAL(mouse_pressed()), this,SLOT(mouse_pressed()));
connect(ui->imageLabel, SIGNAL(mouse_pos()), this,SLOT(mouse_pos()));
scene = new QGraphicsScene();
ui->imageLabel->setScene(scene);
MainWindow::~MainWindow()
delete ui;
delete scene;
void MainWindow::on_actionOpen_triggered()
QFileDialog dialog(this, tr("Open File"));
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first()))
void MainWindow::on_actionExit_triggered()
QCoreApplication::quit();
void MainWindow::mouse_pressed()
QPen outlinePen(Qt::black);
QBrush greenBrush(Qt::NoBrush);
ui->pos_x->setText(QString("X: %1 ").arg(ui->imageLabel->x));
ui->pos_y->setText(QString("Y: %1 ").arg(ui->imageLabel->y));
ui->lblMouse_Current_Event->setText("Mouse Pressed");
QGraphicsEllipseItem *ellipse = scene->addEllipse(pos().x(),pos().y(), 300, 60, outlinePen, greenBrush);
void MainWindow::mouse_pos()
ui->lblMouse_Current_Event->setText("Mouse Moving");
ui->pos_x->setText(QString("X: %1 ").arg(ui->imageLabel->x));
ui->pos_y->setText(QString("Y: %1 ").arg(ui->imageLabel->y));
mqlabel.h
#ifndef MQLABEL_H
#define MQLABEL_H
#include <QWidget>
#include <QImage>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
class mqlabel : public QGraphicsScene
Q_OBJECT
public:
explicit mqlabel(QWidget *parent = 0);
void mouseMoveEvent(QGraphicsSceneMouseEvent *move);
void mousePressEvent(QGraphicsSceneMouseEvent *press);
int x, y;
signals:
void mouse_pressed();
void mouse_pos();
public slots:
;
#endif // MQLABEL_H
mqlabel.cpp
#include "mqlabel.h"
#include <iostream>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsView>
#include <QMouseEvent>
using namespace std;
mqlabel::mqlabel(QWidget *parent) : QGraphicsScene(parent)
void mqlabel::mouseMoveEvent(QGraphicsSceneMouseEvent *move)
QGraphicsScene::mouseMoveEvent(move);
this->x = move->pos.x();
this->y = move->pos.y();
emit mouse_pos();
void mqlabel::mousePressEvent(QGraphicsSceneMouseEvent *press)
QGraphicsScene::mousePressEvent(press);
this->x = press->pos.x();
this->y = press->pos.y();
emit mouse_pressed();
【问题讨论】:
【参考方案1】:不完全确定您想通过为图形场景对象设置场景来实现什么。但是 QGraphicsScene 类实际上没有任何称为 setScene 的成员。 QGraphicsView 有那个方法。
【讨论】:
以上是关于子类 QGraphicsScene 出错的主要内容,如果未能解决你的问题,请参考以下文章