[Qt-interest] QLineEdit - disable undo/redo Accelerators and pass to containing widget ?
Colin S. Miller
no-spam-thank-you at csmiller.demon.co.uk
Tue Sep 15 20:52:23 CEST 2009
Hi,
I'm trying to prevent a class (here EnhancedLineEdit) which is derived from QLineEdit
from handling the CTRL-Z and CTRL-Y keys, and instead allow the containing class
(here MainWindow) to handle it.
It is my understanding if a class's event() function returns FALSE, then the event
is passed to the containing widget.
However, the CTRL-Z isn't passed on to MainWindow::event() in my code.
TIA,
Colin S. Miller
/* build with
* moc -f foo.cpp -o moc_foo.cpp && \
* LANG=C gcc -o foo moc_foo.cpp -I/usr/include/qt3/ -lqt-mt -g -Wall -Wno-switch
*/
#include <iostream>
#include <qapplication.h>
#include <qlayout.h>
#include <qlineedit.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qevent.h>
#define BLOCK_UNDO_IN_RAW_EVENT
class EnhancedLineEdit : public QLineEdit
{
Q_OBJECT
public:
EnhancedLineEdit(QWidget *parent);
protected:
virtual bool event (QEvent * e); /* overrides parent */
virtual QPopupMenu * createPopupMenu(); /* overrides parent */
virtual void keyPressEvent(QKeyEvent *e);
};
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(void);
private slots:
void undo(void);
void redo(void);
protected:
virtual void keyPressEvent(QKeyEvent *e);
};
EnhancedLineEdit::EnhancedLineEdit(QWidget*parent) : QLineEdit(parent)
{
};
bool EnhancedLineEdit::event(QEvent *evt)
{
#ifdef BLOCK_UNDO_IN_RAW_EVENT
switch (evt->type())
{
case QEvent::KeyPress:
{
QKeyEvent *keyEvt = dynamic_cast<QKeyEvent*>(evt);
if ( keyEvt->state() & ControlButton )
{
switch (keyEvt->key())
{
case Key_Z:
case Key_Y:
return FALSE;
break;
}
}
}
break;
}
#endif
return QLineEdit::event(evt);
}
QPopupMenu *EnhancedLineEdit::createPopupMenu()
{
/* no context menu, also disables its short cuts */
return NULL;
}
void EnhancedLineEdit::keyPressEvent(QKeyEvent* e)
{
std::cout << "In EnhancedLineEdit::keyPressEvent" << std::endl;
#ifndef BLOCK_UNDO_IN_RAW_EVENT
if ( e->state() & ControlButton )
{
switch (e->key())
{
case Key_Z:
case Key_Y:
return;
break;
}
}
#endif
QLineEdit::keyPressEvent(e);
}
MainWindow::MainWindow() : QWidget()
{
QGridLayout *layout;
QMenuBar *menuBar = new QMenuBar(this);
QPopupMenu *edit = new QPopupMenu( this );
EnhancedLineEdit *line;
edit->insertItem( "&Undo", this, SLOT(undo()), CTRL+Key_Z );
edit->insertItem( "&Redo", this, SLOT(redo()), CTRL+Key_Y );
menuBar->insertItem( "&Edit", edit );
layout = new QGridLayout(this, 2, 1);
layout->addWidget(menuBar, 0, 0);
line = new EnhancedLineEdit(this);
layout->addWidget(line, 1, 0);
}
void MainWindow::undo(void)
{
std::cout << "MainWindow::undo" << std::endl;
}
void MainWindow::redo(void)
{
std::cout << "MainWindow::redo" << std::endl;
}
void MainWindow::keyPressEvent(QKeyEvent* e)
{
std::cout << "In MainWindow::keyPressEvent" << std::endl;
QWidget::keyPressEvent(e);
}
int main(int argc, char** argv)
{
QApplication qapp(argc, argv);
MainWindow mainWindow;
mainWindow.show();
qapp.setMainWidget(&mainWindow);
qapp.exec();
}
More information about the Qt-interest-old
mailing list