Seite 1 von 1

Eventfilter geht nicht

Verfasst: 9. September 2008 14:53
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

Verfasst: 11. September 2008 12:18
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

Verfasst: 11. September 2008 12:22
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?