Seite 1 von 1

Mouse Events in abgeleiteter Klasse

Verfasst: 1. Mai 2011 16:20
von darhunak
Hallo

Ich schreibe ein kleines Qt Programm welches eine Webcam für Bewegungserkennung benutzt.

Ich habe die Hauptklasse:

Code: Alles auswählen

class WebcamMotionDetector : public QMainWindow, private Ui::WebcamMotionDetectorClass
WebcamMotionDetectorClass besitzt unter anderem ein QGraphicsView*, in welchem die Bilder angezeigt werden.

Dazu habe ich eine Klasse zur Bildakquisition, abgeleitet von QGraphicsView:

Code: Alles auswählen

class motionDetectionGraphicsView : public QGraphicsView
welche als Pointer im Konstruktor der Klasse WebcamMotionDetector instanziiert wird:

Code: Alles auswählen

localGraphicsView_ = new motionDetectionGraphicsView( parent );
Wenn ich in WebcamMotionDetector die Methode

Code: Alles auswählen

mousePressEvent( QMouseEvent* event )
neu implementiere, dann kann ich die Mauskoordinaten lesen und ausgeben. Diese sind aber auf das Hauptfenster bezogen und nützen mir nichts, da ich die Koordinaten benutzen möchte, um eine Region of Interest im QGraphicsView (von mir aus auch der QGraphicsScene) zu erstellen, also z.B. ein QGraphicsRectItem.

Wenn ich aber die Methode

Code: Alles auswählen

mousePressEvent( QMouseEvent* event )
oder auch

Code: Alles auswählen

mousePressEvent( QMouseGraphicsSceneEvent* event )
in der Klasse motionDetectionGraphicsView neu implementiere, wird die Methode scheinbar nicht ausgeführt. (Textausgabe als Test -> passiert nicht...)

Der Weg des Bildes ist folgendermassen:
cv::Mat (von OpenCV) -> QImage -> QPixmap -> QGraphicsScene::addPixmap( pixmap ) -> QGraphicsView::addScene( scene ) -> QGraphicsView::show( )
(Das funktioniert alles! Nur als Info, falls ich besser an einer anderen Stelle auf die Mauskoordinaten zugreifen soll.)

Ich brauche im Endeffekt die Koordinaten des Mauszeigers in Bildkoordinaten.

ui-File erstellt mit Qt Designer
Projekt erstellt mit Qt Plugin in Visual Studio 2008

Ich habe gegoogelt, den Assistant auf den Kopf gestellt aber komme nicht auf die Lösung. Ev. ist die Lösung ziemlich einfach und trivial... Ich habe für den Fall schon ein Facepalm für mich bereitgelegt. :oops:

Ich hoffe das reicht als Info, sonst gerne fragen.

Vielen Dank schon mal im Voraus!

Verfasst: 1. Mai 2011 16:45
von franzf
Setzt du wirklich für jedes neue Bild gleich eine neue Scene? Warum eigentlich QGraphicsView? Wie viele Bilder zeigst du gleichzeitig an? Tuts nicht ein simples QLabel (+QRubberBand für die Auswahl)?
Das hier

Code: Alles auswählen

mousePressEvent( QMouseGraphicsSceneEvent* event )
gibt es gar nicht im QGraphicsView, gibts nur in QGraphicsItem.

Verfasst: 1. Mai 2011 17:26
von darhunak
Nein, das ist nur der Weg der Darstellung. Die QGraphicsScene wird einmal erstellt, bei jedem Bild aber mit

Code: Alles auswählen

scene_->clear( );
gelöscht, dann kommt ein neues QPixmap dazu

Code: Alles auswählen

scene_->addPixmap( *pixmap_ );
Das geschieht für jedes Bild (ca. 10 x pro Sekunde)

Ich habe beide Arten von Mouse Events versucht...
Dann soll ich von QGraphicsItem erben?

Code: Alles auswählen

class motionDetectionGraphicsView : public QGraphicsItem
Dann wäre die Klasse ja etwas, was in die QGraphicsScene eingefügt würde? Ich bin jetzt echt ein bisschen am Schwimmen...

Mit QGraphicsView kann ich die Region of Interest gut darstellen. Kann ich das mit QLabel auch?

Verfasst: 1. Mai 2011 18:47
von franzf
Nein, du sollst nicht von QGraphicsItem ableiten, die View scheint mir schon in Ordnung - wenngleich auch hier das Ableiten aus OOP-Sicht nicht zwingend notwendig ist, dein GraphicsView ist jetzt nicht so speizialisiert, dass es etwas neues ist, im Prinzip reicht dir die öffentliche Schnittstelle, das mousePressEvent bekommst du auch mit nem eventFilter. Aber egal...

Wenn etwas nicht so will, wie man es sich vorstellt, extrahiert man die Codeteile und bastelt ein minimales Beispiel:

Code: Alles auswählen

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QApplication>
#include <QMouseEvent>
#include <QDebug>

class GV : public QGraphicsView
{
    void mousePressEvent(QMouseEvent* e) {
        qDebug() << "Mouse Press at" << e->pos();
        QGraphicsView::mousePressEvent(e);
    }
};

int main(int argc, char** argv) {
    QApplication app(argc, argv);
    GV gv;
    QGraphicsScene scene;
    scene.addRect(QRect(10, 10, 100, 100));
    gv.setScene(&scene);
    gv.show();
    return app.exec();
}
Funktioniert problemlos! Somit muss sich das Problem in deinem Code verstecken. Kannst du mal schauen, ob du mousePressEvent() wirklich richtig geschrieben hast. Wenn du esnichtfindest,packst du dein Projekt (OHNE Binaries bitte!) in ein.zip und hängst es über die Forenfunktion (add Attachement, unter dem TextEdit für den Post) an deinen post an.

// nachtrag:
und ein scene->clear(), nur um danach das selbe Layout wieder zu erstellen, ist Leistungsverschwendung. Du kannst dir doch die PixmapItems merken, und dann setPixmap() aufrufen. Weniger unnötiger Code und weniger Rechenleistung.

Verfasst: 1. Mai 2011 19:50
von darhunak
Danke! Aber "leider" ist mousePressEvent richtig geschrieben...
Das mit dem ganzen Projekt ohne Binaries ist so eine Sache, da man zum Kompilieren/Ausführen die libs/dlls von OpenCV benötigt.

Wie gesagt, das Programm macht alles was es soll, bis auf die Erkennung der Mausposition in

Code: Alles auswählen

motionDetectionGraphicsView::mousePressEvent( QMouseEvent* event )
Nachfolgend aber der komplette Code. (Ist hoffentlich nicht zuviel...):

### main.cpp ###

Code: Alles auswählen

#include "webcammotiondetector.h"
#include <QtGui/QApplication>

int main( int argc, char *argv[ ] )
{
	QApplication a( argc, argv );
	WebcamMotionDetector w;
	w.show( );
	return a.exec( );
}
### webcammotiondetector.h ###

Code: Alles auswählen

#ifndef __WEBCAMMOTIONDETECTOR_H_INCLUDED__
#define __WEBCAMMOTIONDETECTOR_H_INCLUDED__

// #define _CRT_SECURE_NO_WARNINGS

// INCLUDES GO HERE:

#include <QtGui>
#include "ui_webcammotiondetector.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include "motionDetectionGraphicsView.h"

/////////////////////////////////////////////////////////////////////////////
/// \class WebcamMotionDetector 
/// \brief [Enter here brief description of the class]
/// 
/// [Enter here detailed description of the class]
/// 
/// 
/// \sa [Enter list of related classes here]
///
class WebcamMotionDetector : public QMainWindow, private Ui::WebcamMotionDetectorClass
{
	Q_OBJECT

public:

	WebcamMotionDetector( QWidget *parent = 0, Qt::WFlags flags = 0 );

	~WebcamMotionDetector( );

public slots:

		void on_pushButton_start_clicked( );

		void on_pushButton_stop_clicked( );

		void on_pushButton_setroi_clicked( );

		void on_pushButton_initcam_clicked( );

		void on_actionAbout_activated( );

		void on_horizontalSlider_sensitivity_valueChanged( );

private:

	Ui::WebcamMotionDetectorClass ui;

	motionDetectionGraphicsView* localGraphicsView_;

protected:

};

#endif //__WEBCAMMOTIONDETECTOR_H_INCLUDED__
### webcammotiondetector.cpp ###

Code: Alles auswählen

#include "webcammotiondetector.h"

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for WebcamMotionDetector]
///
/// [Detailed description for WebcamMotionDetector]
///
/// @param[in] parent [add parameter description]
/// @param[in] flags [add parameter description]
/// 
/// 
WebcamMotionDetector::WebcamMotionDetector( QWidget *parent, Qt::WFlags flags )
	: QMainWindow( parent, flags )
{
	ui.setupUi( this );

	localGraphicsView_ = new motionDetectionGraphicsView( parent );

	localGraphicsView_->setGraphicsView( ui.graphicsView_image );

	ui.graphicsView_image->setScene( localGraphicsView_->getScene( ) );
	ui.graphicsView_image->show( );

	// First entry in event log
	localGraphicsView_->setTextEdit( ui.plainTextEdit_eventlog );
	ui.plainTextEdit_eventlog->appendPlainText( "INFO: Webcam motion detection program started.\n" );

	// Enable slider tracking
	ui.horizontalSlider_sensitivity->setTracking( true );
	ui.horizontalSlider_sensitivity->setValue( 30 );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for ~WebcamMotionDetector]
///
/// [Detailed description for ~WebcamMotionDetector]
///
/// 
/// 
WebcamMotionDetector::~WebcamMotionDetector( )
{
	delete localGraphicsView_;
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for on_pushButton_initcam_clicked]
///
/// [Detailed description for on_pushButton_initcam_clicked]
///
/// 
/// 
void WebcamMotionDetector::on_pushButton_initcam_clicked( )
{
	if( localGraphicsView_->initDefaultCam( ) == true )
	{
		ui.plainTextEdit_eventlog->appendPlainText( "INFO: Camera initialized\n" );
	}
	else
	{
		ui.plainTextEdit_eventlog->appendPlainText( "ERROR: Could not initialize camera!\n" );
	}
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for on_pushButton_start_clicked]
///
/// [Detailed description for on_pushButton_start_clicked]
///
/// 
/// 
void WebcamMotionDetector::on_pushButton_start_clicked( )
{
	localGraphicsView_->startGrabbing( );

	ui.pushButton_setroi->setEnabled( false );

	ui.plainTextEdit_eventlog->appendPlainText( "INFO: Motion detection started.\n" );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for on_pushButton_stop_clicked]
///
/// [Detailed description for on_pushButton_stop_clicked]
///
/// 
/// 
void WebcamMotionDetector::on_pushButton_stop_clicked( )
{
	localGraphicsView_->stopGrabbing( );

	ui.pushButton_setroi->setEnabled( true );

	ui.plainTextEdit_eventlog->appendPlainText( "INFO: Motion detection stopped.\n" );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for on_pushButton_setroi_clicked]
///
/// [Detailed description for on_pushButton_setroi_clicked]
///
/// 
/// 
void WebcamMotionDetector::on_pushButton_setroi_clicked( )
{
	// Nothing to be done until now...
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for on_actionAbout_activated]
///
/// [Detailed description for on_actionAbout_activated]
///
/// 
/// 
void WebcamMotionDetector::on_actionAbout_activated( )
{
	localGraphicsView_->showAbout( );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for on_horizontalSlider_sensitivity_valueChanged]
///
/// [Detailed description for on_horizontalSlider_sensitivity_valueChanged]
///
/// 
/// 
void WebcamMotionDetector::on_horizontalSlider_sensitivity_valueChanged( )
{
	localGraphicsView_->setSensitivity( ui.horizontalSlider_sensitivity->value( ) );
}
### motionDetectionGraphicsView.h ###

Code: Alles auswählen

#ifndef __MOTIONDETECTIONGRAPHICSVIEW_H_INCLUDED__
#define __MOTIONDETECTIONGRAPHICSVIEW_H_INCLUDED__

// #define _CRT_SECURE_NO_WARNINGS

#include <QtGui>
#include "ui_webcammotiondetector.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

const int MAXDEVICE_ = 5;

/////////////////////////////////////////////////////////////////////////////
/// \class motionDetectionGraphicsView 
/// \brief [Enter here brief description of the class]
/// 
/// [Enter here detailed description of the class]
/// 
/// 
/// \sa [Enter list of related classes here]
///
class motionDetectionGraphicsView : protected QGraphicsView
{
	Q_OBJECT

public:

	motionDetectionGraphicsView( QWidget* parent = 0 );
	~motionDetectionGraphicsView( );

	void setGraphicsView( QGraphicsView* _in );

	void setTextEdit( QPlainTextEdit* _in );

	QGraphicsScene* getScene( );

	bool initDefaultCam( );

	void startGrabbing( );

	void stopGrabbing( );

	void setSensitivity( int _sensitivity );

	void showAbout( );

private:

	QGraphicsView* localGraphicsView_;

	QPlainTextEdit* localTextEdit_;

	QGraphicsScene* scene_;
	QPixmap* pixmap_;

	QImage* qimageShow_;
	QImage* qimageNew_;

	QDir folder_;
	QString folderString_;

	QString filename_;

	QTime time_;
	QString timeString_;

	QDate date_;
	QString dateString_;

	// Image acquisition
	cv::VideoCapture* cam_;
	bool camOpened_;
	int device_;

	// Difference image
	cv::Mat imgRaw_;
	cv::Mat imgOld_;
	cv::Mat imgNew_;
	cv::Mat imgDiff_;
	cv::Mat imgShow_;

	unsigned char thresh_;
	double size_;
	double sizeThresh_;

	int imgHeight_;
	int imgWidth_;
	int ii_;
	int jj_;

	bool motionDetected_;

	// Mouse positions for ROI
	double x1_;
	double y1_;
	double x2_;
	double y2_;

	// Timer for repeated call to getDifferenceImage( )
	QTimer* timer_;
	

private slots:

	void getDifferenceImage( );

protected:

	void mousePressEvent( QMouseEvent* event );

	void mouseReleaseEvent( QMouseEvent* event );

	void mouseMoveEvent( QMouseEvent* event );

};

#endif //__MOTIONDETECTIONGRAPHICSVIEW_H_INCLUDED__
### motionDetectionGraphicsView.cpp ###

Code: Alles auswählen

#include "motionDetectionGraphicsView.h"

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for motionDetectionGraphicsView]
///
/// [Detailed description for motionDetectionGraphicsView]
///
/// @param[in] parent [add parameter description]
/// 
/// 
motionDetectionGraphicsView::motionDetectionGraphicsView( QWidget* parent )
	: QGraphicsView( parent ), camOpened_( false ), thresh_( 40 ), sizeThresh_( 0.0 ), scene_( NULL )
	, pixmap_( NULL ), qimageShow_( NULL ), qimageNew_( NULL ), cam_( NULL ), device_( 0 )
{
	// Load start image
	scene_ = new QGraphicsScene;
	pixmap_ = new QPixmap;
	pixmap_->load( "./images/test.jpg" );
	scene_->addPixmap( *pixmap_ );

	// Enable mouse tracking
	//setMouseTracking( true );

	// Make new VideoCapture device
	cam_ = new cv::VideoCapture;

	// New timer
	timer_ = new QTimer( this );
	connect( timer_, SIGNAL( timeout( ) ), this, SLOT( getDifferenceImage( ) ) );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for ~motionDetectionGraphicsView]
///
/// [Detailed description for ~motionDetectionGraphicsView]
///
/// 
/// 
motionDetectionGraphicsView::~motionDetectionGraphicsView( )
{
	if( scene_ != NULL )
	{
		delete scene_;
		scene_ = NULL;
	}

	if( pixmap_ != NULL )
	{
		delete pixmap_;
		pixmap_ = NULL;
	}

	if( qimageShow_ != NULL )
	{
		delete qimageShow_;
		qimageShow_ = NULL;
	}

	if( qimageNew_ != NULL )
	{
		delete qimageNew_;
		qimageNew_ = NULL;
	}


	if( cam_ != NULL )
	{
		delete cam_;
		cam_ = NULL;
	}

	if( timer_ != NULL )
	{
		delete timer_;
		timer_ = NULL;
	}
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for setGraphicsView]
///
/// [Detailed description for setGraphicsView]
///
/// @param[in] _in [add parameter description]
/// 
/// 
void motionDetectionGraphicsView::setGraphicsView( QGraphicsView* _in )
{
	localGraphicsView_ = _in;
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for setTextEdit]
///
/// [Detailed description for setTextEdit]
///
/// @param[in] _in [add parameter description]
/// 
/// 
void motionDetectionGraphicsView::setTextEdit( QPlainTextEdit* _in )
{
	localTextEdit_ = _in;
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for getScene]
///
/// [Detailed description for getScene]
///
/// 
/// \return QGraphicsScene
/// 
QGraphicsScene* motionDetectionGraphicsView::getScene( )
{
	return scene_;
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for startGrabbing]
///
/// [Detailed description for startGrabbing]
///
/// 
/// 
void motionDetectionGraphicsView::startGrabbing( )
{
	// Set date
	date_ = QDate::currentDate( );
	time_ = QTime::currentTime( );
	dateString_ = date_.toString( "dd_MM_yy" );
	timeString_ = time_.toString( "hh_mm_ss" );

	folderString_ = QString( ".\\" ) + dateString_ + QString( "_@_" ) + timeString_;

	if( folder_.exists( folderString_ ) == false )
	{
		if( folder_.mkdir( folderString_ ) == false )
		{
			localTextEdit_->appendPlainText( "ERROR: Could not create output folder! Images will be stored at path of executable!" );
			folderString_ = ".\\";
		}
	}

	timer_->start( 100 );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for stopGrabbing]
///
/// [Detailed description for stopGrabbing]
///
/// 
/// 
void motionDetectionGraphicsView::stopGrabbing( )
{
	timer_->stop( );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for setSensitivity]
///
/// [Detailed description for setSensitivity]
///
/// @param[in] _sensitivity [add parameter description]
/// 
/// 
void motionDetectionGraphicsView::setSensitivity( int _sensitivity )
{
	sizeThresh_ = static_cast< double >( _sensitivity ) / 100.0;
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for showAbout]
///
/// [Detailed description for showAbout]
///
/// 
/// 
void motionDetectionGraphicsView::showAbout( )
{
	pixmap_->load( "./images/about.jpg" );
	scene_->clear( );
	scene_->addPixmap( *pixmap_ );
	localGraphicsView_->show( );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for initDefaultCam]
///
/// [Detailed description for initDefaultCam]
///
/// 
/// \return bool
/// 
bool motionDetectionGraphicsView::initDefaultCam( )
{
	camOpened_ = false;

	if( cam_ != NULL )
	{
		delete cam_;
		cam_ = NULL;
	}

	try
	{
		cam_ = new cv::VideoCapture( device_ );
		camOpened_ = cam_->isOpened( );
		device_ = ( device_ + 1 ) % MAXDEVICE_;
	}
	catch( ... )
	{
		camOpened_ = false;

		if( cam_ != NULL )
		{
			delete cam_;
			cam_ = NULL;
		}

		device_ = 0;

		cam_ = new cv::VideoCapture( device_ );
		camOpened_ = cam_->isOpened( );
		device_ = ( device_ + 1 ) % MAXDEVICE_;
	}

	

	if( camOpened_ == true )
	{
		( *cam_ ) >> imgRaw_;
		cv::cvtColor( imgRaw_, imgOld_, CV_BGR2RGB );
		( *cam_ ) >> imgRaw_;
		cv::cvtColor( imgRaw_, imgNew_, CV_BGR2RGB );

		imgHeight_ = imgNew_.rows;
		imgWidth_ = imgNew_.cols;

		imgNew_.copyTo( imgShow_ );

		if( qimageShow_ != NULL )
		{
			delete qimageShow_;
			qimageShow_ = NULL;
		}

		qimageShow_ = new QImage( imgShow_.data, imgWidth_, imgHeight_, imgNew_.step, QImage::Format_RGB888 );

		if( pixmap_ != NULL )
		{
			delete pixmap_;
			pixmap_ = NULL;
		}

		pixmap_ = new QPixmap;
		*pixmap_ = QPixmap::fromImage( *qimageShow_ );

		scene_->clear( );
		scene_->addPixmap( *pixmap_ );
		localGraphicsView_->show( );

		return true;
	}

	return false;
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for getDifferenceImage]
///
/// [Detailed description for getDifferenceImage]
///
/// 
/// 
void motionDetectionGraphicsView::getDifferenceImage( )
{
	motionDetected_ = false;

	if( camOpened_ == true )
	{
		// This has to be done twice because...?
		( *cam_ ) >> imgRaw_;
		( *cam_ ) >> imgRaw_;

		cv::cvtColor( imgRaw_, imgNew_, CV_BGR2RGB );
		imgNew_.copyTo( imgShow_ );

		imgDiff_ = cv::abs( imgOld_ - imgNew_ );

		size_ = 0.0;
		for( ii_ = 0 ; ii_ < imgHeight_ ; ++ii_ )
		{
			for( jj_ = 0 ; jj_ < imgWidth_ ; ++jj_ )
			{
				if( imgDiff_.at< cv::Vec3b >( ii_, jj_ )[ 0 ] > thresh_ || imgDiff_.at< cv::Vec3b >( ii_, jj_ )[ 1 ] > thresh_ || imgDiff_.at< cv::Vec3b >( ii_, jj_ )[ 2 ] > thresh_ )
				{
					imgShow_.at< cv::Vec3b >( ii_, jj_ )[ 0 ] = 255;
					imgShow_.at< cv::Vec3b >( ii_, jj_ )[ 1 ] = 128;
					imgShow_.at< cv::Vec3b >( ii_, jj_ )[ 2 ] = 0;
					size_ += 1.0;
				}
			}// end for jj_
		}// end for ii_

		if( qimageShow_ != NULL )
		{
			delete qimageShow_;
			qimageShow_ = NULL;
		}

		if( qimageNew_ != NULL )
		{
			delete qimageNew_;
			qimageNew_ = NULL;
		}

		qimageShow_ = new QImage( imgShow_.data, imgWidth_, imgHeight_, imgNew_.step, QImage::Format_RGB888 );
		qimageNew_ = new QImage( imgNew_.data, imgWidth_, imgHeight_, imgNew_.step, QImage::Format_RGB888 );

		// Save image if enough change was detected
		if( ( size_ / static_cast< double >( imgHeight_ * imgWidth_ ) ) > sizeThresh_ )
		{
			motionDetected_ = true;
		}

		if( motionDetected_ == true )
		{
			time_ = QTime::currentTime( );
			timeString_ = time_.toString( "hh_mm_ss.zzz" );
			filename_ = folderString_ + QString( "\\" ) + QString( "event_@_" ) + timeString_ + QString( ".png" );
			qimageNew_->save( filename_, "PNG", 100 );
			localTextEdit_->appendPlainText( QString( "EVENT: @ " ) + timeString_ );
		}

		if( pixmap_ != NULL )
		{
			delete pixmap_;
			pixmap_ = NULL;
		}

		pixmap_ = new QPixmap;
		*pixmap_ = QPixmap::fromImage( *qimageShow_ );

		scene_->clear( );
		scene_->addPixmap( *pixmap_ );
		localGraphicsView_->show( );

		// Update
		imgNew_.copyTo( imgOld_ );
	}
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for mousePressEvent]
///
/// [Detailed description for mousePressEvent]
///
/// @param[in] event [add parameter description]
/// 
/// 
void motionDetectionGraphicsView::mousePressEvent( QMouseEvent* event )
{
	localTextEdit_->appendPlainText( "Mouse pressed..." );

	x1_ = event->pos( ).x( );
	y1_ = event->pos( ).y( );

	QString text_( "INFO: Mouse at position: " );
	QString xPosText_;
	QString yPosText_;

	xPosText_.setNum( x1_ );
	yPosText_.setNum( y1_ );

	text_ += ( xPosText_ + QString( " " ) + yPosText_ );

	localTextEdit_->appendPlainText( text_ );
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for mouseReleaseEvent]
///
/// [Detailed description for mouseReleaseEvent]
///
/// @param[in] event [add parameter description]
/// 
/// 
void motionDetectionGraphicsView::mouseReleaseEvent( QMouseEvent* event )
{
}

/////////////////////////////////////////////////////////////////////////////
/// \brief [brief description for mouseMoveEvent]
///
/// [Detailed description for mouseMoveEvent]
///
/// @param[in] event [add parameter description]
/// 
/// 
void motionDetectionGraphicsView::mouseMoveEvent( QMouseEvent* event )
{
}

Verfasst: 1. Mai 2011 20:37
von Experimentierer
Hallo darhunak

Um an die Mouse Events zu kommen kannst du deine QGraphicsView auf ein QLabel legen und auf den QLabel "this->installEventFilter(this);" anwenden und diese in "bool eventFilter(QObject *o,QEvent *e)" abfangen.

Da sich das QLabel der QGraphicsView anpasst, weist du was und wo auf der QGraphicsView mit der Maus machst :D .

Verfasst: 1. Mai 2011 22:17
von franzf
"this->installEventFilter(this)"... was soll das bringen? Da kann ich gleich selber event() implementieren oder noch besser schon auf die spezialisierte Version (also mousePressEvent) zugreifen.

Das Problem: Absolutes Chaos :D
Deine Klasse erbt von QGraphicsView. In deiner ui steht nur ein QGraphicsView. Jenes gv aus dem ui legts du jetzt per setGraphicsView als Member in deine eigene GV-beerbende Klasse.Das gv ist dasjenige, welches sichtbar ist, dein eigenes abgeleitetes local_gv hingegen nicht. Deshalb kommt auch kein event an (welch Wunder).

Was du wahrscheinlich willst, ist dass dein eigenes gv direkt im ui eingefügt wird. Das geht ganz einfach mittels Rechtsklich auf das gv im Designer -> "Als Platzhalter für benutzerdefinierte Klasse festlegen" -> Klasse, Header usw. korrekt einfügen, dann kannst du dir die nicht funktionierende Krücke über deine Setter sparen.
Außerdem:
* delete uf enern NULL-Zeiger ist definiert, selber abfragen redundant und inperformant
* im Destruktor den Zeiger auf NULL setzen ist absolut unnötig
* deine scene liegt nicht in deiner eigenen view, sondern nur in der aus dem ui

Verfasst: 2. Mai 2011 10:34
von darhunak
Hallo Leute

Danke für die Antorten!

@Experimentierer:
Event Filter habe ich auch schon gelesen, wollte ich aber mangels Verständnis nicht probieren.

@franzf:
Danke für die Mühe, dass Du Dir den Code durchgeschaut hast! Ja, das mit dem Chaos habe ich auch schon von anderer Stelle (etwas weniger humorvoll) gehört :(

Habe mir schon fast gedacht, dass bei meinen Pointerzuweisungen irgendwo der Hund liegt. Da aber andere Dinge wie z.B.

Code: Alles auswählen

localGraphicsView_->setTextEdit( ui.plainTextEdit_eventlog ); 
funktioniert hat, habe ich nichts geändert.
Was du wahrscheinlich willst, ist dass dein eigenes gv direkt im ui eingefügt wird. Das geht ganz einfach mittels Rechtsklich auf das gv im Designer -> "Als Platzhalter für benutzerdefinierte Klasse festlegen" -> Klasse, Header usw. korrekt einfügen, dann kannst du dir die nicht funktionierende Krücke über deine Setter sparen.
Ja, eigentlich schon, wusste aber nicht wie das geht... Werde ich versuchen und das Ergebnis poste ich hier.

Verfasst: 3. Mai 2011 11:07
von darhunak
*Schlägt seinen Kopf auf die Tischplatte*

Warum hasst mich Qt? :(

In Qt Designer "Promote to" (ist Englisch installiert) damit ich meinen eigene Klasse erstellen kann, abgeleitet von QGraphicsView.
Name der Klasse stimmt, Name des Header-Files stimmt auch und gefunden wird es scheinbar auch, denn keiner der 26 Errors sagt, dass VS das Header-File nicht gefunden hat.

### Error Log ###

Code: Alles auswählen

Error	1	error C2143: syntax error : missing ';' before '*'	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	40
Error	2	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	40
Error	3	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	40
Error	4	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	76
Error	5	error C2061: syntax error : identifier 'motionDetectionGraphicsView'	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	76
Error	6	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	77
Error	7	error C2227: left of '->setObjectName' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	77
Error	8	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	78
Error	9	error C2227: left of '->setGeometry' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	78
Error	10	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	143
Error	11	error C2227: left of '->setToolTip' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	143
Error	12	fatal error C1903: unable to recover from previous error(s); stopping compilation	c:\dev\opencv2_1\include\opencv\cxmat.hpp	227
Error	13	error C2065: 'localGraphicsView_' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\webcammotiondetector.cpp	40
Error	14	error C2227: left of '->setTextEdit' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\webcammotiondetector.cpp	40
Error	15	error C2143: syntax error : missing ';' before '*'	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	40
Error	16	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	40
Error	17	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	40
Error	18	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	76
Error	19	error C2061: syntax error : identifier 'motionDetectionGraphicsView'	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	76
Error	20	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	77
Error	21	error C2227: left of '->setObjectName' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	77
Error	22	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	78
Error	23	error C2227: left of '->setGeometry' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	78
Error	24	error C2065: 'graphicsView_image' : undeclared identifier	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	143
Error	25	error C2227: left of '->setToolTip' must point to class/struct/union/generic type	f:\Eigene Dateien\Programming\WebcamMotionDetector\WebcamMotionDetector\GeneratedFiles\ui_webcammotiondetector.h	143
Error	26	fatal error C1903: unable to recover from previous error(s); stopping compilation	c:\dev\opencv2_1\include\opencv\cxmat.hpp	227

Ich schnalls echt nicht mehr... Kann mir das jemand erklären?! :?

Verfasst: 3. Mai 2011 12:40
von franzf
Jetzt bräuchte man halt wieder Code...
Er beschwert sich über einen "undeclared identifier", im Zusammenhang mit deiner Klasse.
Da der Fehler sowohl im ui_xyz.h, als auf der webcammotiondetector.cpp auftritt, ist es kein direktes Problem mit dem designer, du hast wahrscheinlich einfach nur nen Fehler in deinem Header.
Heißt die Klasse wirklich immer noch so? Hast du einen ";" am Ende der Klassendefinition?

Verfasst: 3. Mai 2011 12:52
von ScyllaIllciz
Hast Du mal überprüft ob Du eine Headerdatei im "promo to" Dialog angegen hast? Es sieht so aus als ob er die Klasse, die Du angegeben hast, nicht kennt. Und das passiert nur wenn die Include Datei fehlt.

Verfasst: 3. Mai 2011 13:15
von darhunak
Asche auf mein Haupt... Der Fehler war der:

### ui_webcammotiondetector.h ###

Code: Alles auswählen

...
#include <motionDetectionGraphicsView.h>
...
Das ist korrekt.

### motionDetectionGraphicsView.h ###

Code: Alles auswählen

...
#include <#include "ui_webcammotiondetector.h">
...
und DAS ist überhaupt nicht nötig... Gegenseitige Abhängigkeit funktioniert so nicht wirklich. Copy/Paste ist halt Fluch und Segen zugleich :D

Jetzt funktioniert sowohl mousePressEvent, mouseMoveEvent und mouseReleaseEvent.

Ich habe jetzt Zugriff auf die Mauskoordinaten relativ zur abgeleiteten Klasse.
Jetzt muss ich nur noch die Koordinaten auf die Bildkoordinaten umrechnen (motionDetectionGraphicsView ist grösser als das Bild).

Danke für Eure Hilfe und Geduld!