View Full Version : signals for key presses ?

9th June 2009, 08:59
i have a QWidget object and want to add handlers for some key presses, the traditional way
is to inherit QWidget and reimplement the virtual funcitons, my question is: can i add
event handlers without inheriting QWidget ?
is there signals for keyboard events ?:confused:

9th June 2009, 09:17
you can set event filter on you widget and process QKeyEvent.
take a look at QObject::installEventFilter and QObject::eventFilter.

9th June 2009, 09:34
Thank you,this solution is based on inheriting QObject, but i don't want to use inheritance.

9th June 2009, 09:36
what do you want to achive? give us more information.

9th June 2009, 10:04
here is the code:

int main(int argc,char** args)
QApplication a(argc, argv);
QSplitter* splitter = new QSplitter();

QDirModel* model = new QDirModel();
QTreeView* tree = new QTreeView(splitter);
tree->setRootIndex( model->index( QDir::currentPath() ) );

QListView* lview = new QListView(splitter);
lview->setRootIndex( model->index(QDir::currentPath()) );

QStringList* stringList = new QStringList( QString("item 0") );
for (int i = 1;i<10;i++)
stringList->append( QString("item %1").arg(i) );

StringListModel* myCustomModel = new StringListModel(*stringList);
QTreeView* view3 = new QTreeView(splitter);


return a.exec();

as i press enter i want to add new string to stringList and reflect the changes to the view.

9th June 2009, 10:08
I don't see a problem. create a new widget and put your splitter on it, then install event filter on needed widget and process key event.

9th June 2009, 10:12
Yes i tried it and it works, thank you. :)