Frage zu QGraphicsView mouseMoveEvent

Alles zu Entwicklungsumgebungen und Editoren für Qt
Antworten
Bolzen
Beiträge: 1
Registriert: 4. April 2023 21:39

Frage zu QGraphicsView mouseMoveEvent

Beitrag von Bolzen »

Hallo Leute,

da ich als Anfänger mittlerweile am Verzweifeln bin, wende ich mich hoffnungsvoll an euch. Es geht um folgendes. Ich möchte im Qt Creator bei Click auf einen pushbutton eine QGraphicsScene und anschließend daraus eine QGraphicsView erstellen. Das ganze ging auch schon mal, bis ich die meine eigene Klassen ("myScene" und "myGraphicsView") erstellt habe.

Mein PROBLEM: Das mouseMoveEvent spricht nicht mehr an! Ging vorher aber...Beim MouseMoveEvent soll nur "hhh" ausgegeben werden, testweise...Die GrafikScene selbst funktioniert wunderbar und alles wird dargestellt.

Vielleicht kann mir jemand von euch sagen, wo der Fehler liegt...Hier mal Ausschnitte davon. Mittlerweile bin ich nur noch am "sinnlosen herumprobieren" angekommen, da ich nicht weiß woran es liegt, da es ja vorher ging... :roll: :roll: :roll:


mainwindow.cpp:

Code: Alles auswählen


void MainWindow::on_pushButton_3_clicked() {
QWidget* lab1=nullptr;

lab1=new QWidget(this,Qt::Dialog);
lab1->setWindowTitle("Importierte ...-Datei");
lab1->showMaximized();

myScene*scn=new myScene(lab1,&Formen);
myGraphicsView* view=new myGraphicsView(scn,lab1);
view->setMouseTracking(1);
lab1->setMouseTracking(1);
}

myscene.h:

Code: Alles auswählen


#ifndef MYSCENE_H
#define MYSCENE_H


#include <QGraphicsScene>
#include <QPointF>
#include "myStructs.h"
#include "qdebug.h"
#include <QLabel>
#include <QPen>

class myScene:public QGraphicsScene
{


private:

    struct forms *Fo=new struct forms;
    struct line *L= new struct line;
    std::vector<struct forms>*vecFptr=new std::vector<struct forms>;
    std::vector<struct line>*vecLptr=new std::vector<struct line>;
    std::vector<struct line>LINES;
    QVector<QPointF>QPoints;
    QVector<qreal>QPointsx;
    QVector<qreal>QPointsy;

public:

    float xmin=1000000.0,ymin=1000000.0;
    float xmax=0.0,ymax=0.0;
    int numLines=0;
    int numPoints=0;
    QGraphicsScene *scene=nullptr;
    QWidget* w=nullptr;
    QLabel *lab=nullptr;
    std::vector<struct forms>* _F=nullptr;
    


//Konstruktor
myScene(QWidget* parent=nullptr, std::vector<struct forms> *F=nullptr):QGraphicsScene(parent){
_F=F;
w=new QWidget(parent,Qt::Dialog);
w->setMouseTracking(1);
scene=new QGraphicsScene(this->w);

//Zeiger auf Vektor selbst
this->vecFptr=F;
qDebug()<<F->size();

//Zeiger auf Formen
this->Fo=&*F->begin();

//Zeiger auf Linienzug
this->vecLptr=&F->at(0).Linienzug;

//Zeiger auf Linien
this->L=&F->at(0).Linienzug[0];

//Ermittlung Anzahl Linien numLines
int imax=(int)vecFptr->size();
numLines=0;
int _lineCount=0;
for (int i=0;i<imax;i++){
    this->vecLptr=&F->at(i).Linienzug;
    for(int j=0;j<(unsigned int)vecLptr->size();j++){
        this->L=&F->at(i).Linienzug[j];
        _lineCount++;
        LINES.resize(_lineCount);
        LINES[_lineCount-1].Number=L->Number;
        LINES[_lineCount-1].hasNeighbour=L->hasNeighbour;
        LINES[_lineCount-1].length=L->length;
        LINES[_lineCount-1].points[0]=L->points[0];
        LINES[_lineCount-1].points[1]=L->points[1];
    }
    numLines=numLines+vecLptr->size();
}

//Ermittelung aller existierenden Punkte
int h=0;
for (int z=0;z<(int)F->size();z++){

    QPoints.resize(F->at(z).Linienzug.size()+h+1);
    QPointsx.resize(F->at(z).Linienzug.size()+h+1);
    QPointsy.resize(F->at(z).Linienzug.size()+h+1);



for (unsigned int i=0;i<F->at(z).Linienzug.size();i+=1){
for (unsigned int j=0;j<2;j++){
QPoints[h+i+j].rx()=F->at(z).Linienzug[i].points[j].x.toFloat();
QPoints[h+i+j].ry()=F->at(z).Linienzug[i].points[j].y.toFloat();
QPointsx[h+i+j]=F->at(z).Linienzug[i].points[j].x.toFloat();
QPointsy[h+i+j]=F->at(z).Linienzug[i].points[j].y.toFloat();
}

}
h=QPoints.size();
}
numPoints=QPoints.size();


int z=0;
    for (unsigned int i=0;i<(unsigned int)numPoints-1;i+=1){
    for (unsigned int j=0;j<2;j++){
    xmin=std::min((qreal)xmin,QPoints[z].rx());
    ymin=std::min((qreal)ymin,QPoints[z].ry());
    xmax=std::max((qreal)xmax,QPoints[z].rx());
    ymax=std::max((qreal)ymax,QPoints[z].ry());
    }
    z++;
    xmin=std::min((qreal)xmin,QPoints[z].rx());
    ymin=std::min((qreal)ymin,QPoints[z].ry());
    xmax=std::max((qreal)xmax,QPoints[z].rx());
    ymax=std::max((qreal)ymax,QPoints[z].ry());
    }
qDebug()<<xmin;
qDebug()<<xmax;
qDebug()<<ymin;
qDebug()<<ymax;



    QPen outlinePen;
        outlinePen.setWidth(2);
        outlinePen.setColor(QColor(200,200,100));
        QColor a[7]={Qt::blue,Qt::green,Qt::yellow,Qt::red,Qt::black,Qt::magenta,Qt::cyan};

        for (int j=0; j<(int)F->size();j++){
            outlinePen.setColor(a[j%7]);
            qDebug()<<F->size();
            h=F->at(j).Linienzug.size();
            qDebug()<<h;
            for (int i=0; i<h;i++){
                qDebug()<<F->at(j).Linienzug[i].points[0].x.toFloat();
            this->scene->addLine(F->at(j).Linienzug[i].points[0].x.toFloat()-this->xmin,F->at(j).Linienzug[i].points[0].y.toFloat()-ymin,F->at(j).Linienzug[i].points[1].x.toFloat()-xmin,F->at(j).Linienzug[i].points[1].y.toFloat()-ymin,outlinePen);
            }
        }

        QRectF* rect1=nullptr;
        rect1=new QRectF(0,0,1000,1000);
        scene->setSceneRect(*rect1);



    };

//Destruktor
    ~myScene(){};

signals:

protected slots:

};

#endif // MYSCENE_H
myGraphicsView.h:

Code: Alles auswählen

#include "qlabel.h"
#include <QGraphicsView>
#include <QWidget>
#include <QDialog>
#include <QDebug>
#include "myStructs.h"
#include "myscene.h"
#include <QGraphicsView>
#include <QPointF>
#include <QLineEdit>
#include <QTimer>
#include <QMouseEvent>



#ifndef MYGRAPHICSVIEW_H
#define MYGRAPHICSVIEW_H

#endif // MYGRAPHICSVIEW_H
class myGraphicsView : public QGraphicsView{

private:
int xold=0,yold=0;
int xpos=0,ypos=0;
unsigned long way=0;


public:

myGraphicsView(myScene*_scene=NULL,QWidget *parent = NULL);

QGraphicsScene *_sc =nullptr;
QGraphicsView*_view=nullptr;
QLabel*lab=nullptr;

~myGraphicsView();

private:




protected:
 void mouseMoveEvent(QMouseEvent*event);

private slots:


};
myGraphicsView.cpp:

Code: Alles auswählen

#include "myGraphicsView.h"
#include "myscene.h"

#include <qmath.h>

myGraphicsView::myGraphicsView(myScene *_scene, QWidget *parent):QGraphicsView(_scene->scene,parent){


this->_view=new QGraphicsView(_scene->scene,parent);
parent->setMouseTracking(1);
_view->setMouseTracking(1);

_view->showMaximized();

};

void myGraphicsView::mouseMoveEvent(QMouseEvent*){
       qDebug()<<"hhh";
};

myGraphicsView::~myGraphicsView(){};
;

Liebe Grüße

Bolzen
Antworten