Eventfilter geht nicht
Verfasst: 9. September 2008 14:53
Hi Leute,
Hab heute des erste mal mit Eventfilter gearbeitet aber es haut ned hin. Hier der Code:
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
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());
}
}
}
Der Filter ist installiert. Was mach ich falsch?
Mfg Wotan