Eventfilter geht nicht

Alles rund um die Programmierung mit Qt
Antworten
Wotan
Beiträge: 10
Registriert: 2. Mai 2008 17:45

Eventfilter geht nicht

Beitrag von Wotan »

Hi Leute,

Hab heute des erste mal mit Eventfilter gearbeitet aber es haut ned hin. Hier der Code:

Code: Alles auswählen

class KonsoleDialog : public QDialog, private Ui_KonsoleDialog
{
	Q_OBJECT
public:
	KonsoleDialog(QWidget* parent, TcpSocket* socket);
	~KonsoleDialog();
	
	bool eventFilter(QObject* watched, QEvent* event);
	
protected slots:
	void onExit();

protected:
	TcpSocket* m_pSocket;
	
	void setupConnections();
	void handleCommandString(const QString& str);
};

Code: Alles auswählen

KonsoleDialog::KonsoleDialog(QWidget* parent, TcpSocket* socket)
: QDialog(parent), m_pSocket(socket)
{
	//Setup the user interface
	setupUi(this);
	//Connect the signal and slots
	setupConnections();
	//install the event filter
	edtInput->installEventFilter(this);
}

KonsoleDialog::~KonsoleDialog()
{
}

void KonsoleDialog::setupConnections()
{
	connect(this, SIGNAL(rejected()), this, SLOT(onExit()));
}

void KonsoleDialog::onExit()
{
	if(m_pSocket)
	{
		m_pSocket->sendString("exit");
	}
}

bool KonsoleDialog::eventFilter(QObject* watched, QEvent* event)
{
	//If the watched object is the input widget and a key was pressed
	if((watched==edtInput) && (event->type()==QEvent::KeyPress))
	{
		QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(event);
		
		if((pKeyEvent->key()==Qt::Key_Enter) || (pKeyEvent->key()==Qt::Key_Return))
		{
			//Handle the string
			handleCommandString(edtInput->text());
			return true;
		}
	}
	return QWidget::eventFilter(watched, event);
}

void KonsoleDialog::handleCommandString(const QString& str)
{
	if(m_pSocket)
	{
		//Send the string and revice the response
		m_pSocket->sendString(str);
		edtOutput->append(str);
		edtInput->clear();
		
		if(str=="exit")
		{
			//If the str was exit we know everything is done
			hide();
		}
		else
		{
			//Otherwise we'll append the result
			edtOutput->append(m_pSocket->reciveString());
		}
	}
}
Ich will dass immer wenn man enter drückt der text von edtInput mit handleCommandString() verarbeitet wird. aber das passiert einfach nicht.
Der Filter ist installiert. Was mach ich falsch?

Mfg Wotan
SaLu
Beiträge: 50
Registriert: 18. August 2008 18:06

Beitrag von SaLu »

mhm ich weiß nicht ob es was bringt aber versuch mal deine eventfilter methode Protected zu machen. Du überschreibst sie ja.

wenn nicht schau mal bis wohin er überhaupt läuft
Christian81
Beiträge: 7319
Registriert: 26. August 2004 14:11
Wohnort: Bremen
Kontaktdaten:

Beitrag von Christian81 »

Es ist egal ob public oder private oder was auch immer.

'geht gar ned' ist auch ne gute Fehlermeldung (wie immer hier im Forum :cry: ) - Schonmal einen Breakpoint in KonsoleDialog::eventFilter() gemacht und geschaut ob dort Dein Programm überhaupt mal ankommt?
MfG Christian

'Funktioniert nicht' ist keine Fehlerbeschreibung
Antworten