Re: event(QEvent * event)
Quote:
Originally Posted by
rajaraob
hi
i am facing some problem to capture key events in subclass of QObject.
What kind of problems?
Quote:
Originally Posted by
rajaraob
Please do let me know your insigths
The only "insight" I can give you at this point(i.e. without knowing what your problem is) is, that you swallow all keypress events. If that is not what you want you should probably rather do
Code:
bool ABC
::event(QEvent * event
) {
int cnt = m_pMasterTab->count();// m_pMasterTab is QTabWidget declared at private member of ABC class
if(event
->type
() == QEvent::KeyPress) { Qt::Key keyPressed = (Qt::Key)k->key();
if (keyPressed == Qt::Key_Left) {
int indx = m_pMasterTab->currentPageIndex();
m_pMasterTab->setCurrentPage ((indx ==0)?cnt:indx-1 ) ;
return true;
} else if (keyPressed == Qt::Key_Right) {
int indx = m_pMasterTab->currentPageIndex();
m_pMasterTab->setCurrentPage ((indx+1)%cnt );
return true;
}
}
//is ABC a direct child of QObject?
//Wouldn't it be better to call the event function of the parent class here?
}
Would it perhaps be easier to use an Event Filter?
Re: event(QEvent * event)
thanks for you reply .
In my above code ,
all keyboard events have been recognised on QTAbWidget except left and right arrows.
is it a bug or any reason in 3.3.5 versions?
thanks
raja rao
Re: event(QEvent * event)
Quote:
Originally Posted by
rajaraob
In my above code ,
all keyboard events have been recognised on QTAbWidget except left and right arrows.
I am sorry, I am not sure I understand. The QTabWidget receives all keyboard event except left and right arrows?
Could you please create a small (but complete) programm that shows this behaviour? Any solution would have to include more information than you gave here. What type is ABC? How is ABC used etc.
Quote:
Originally Posted by
rajaraob
is it a bug or any reason in 3.3.5 versions?
The chances are much better that there is a bug in your code, or that how you think event propagation works, is not how it really does work...
Re: event(QEvent * event)
A complete example, is for example this:
The main.cpp
Code:
#include "qapplication.h"
#include "qlabel.h"
#include "qtabwidget.h"
{
Q_OBJECT
public:
ABC
(QWidget * parent
= 0,
const char * name
= 0, WFlags f
= 0)
protected:
{
if(e
->type
() == QEvent::KeyPress) { Qt::Key keyPressed = (Qt::Key)k->key();
if (keyPressed == Qt::Key_Left) {
int indx = currentPageIndex();
setCurrentPage ((indx ==0)?count():indx-1 ) ;
return true;
} else if (keyPressed == Qt::Key_Right) {
int indx = currentPageIndex();
setCurrentPage ((indx+1)%count() );
return true;
}
}
}
};
int main(int argc, char* argv[])
{
ABC *abc = new ABC();
abc->addTab(label1, "Label1");
abc->addTab(label2, "Label2");
abc->show();
return app.exec();
}
#include "main.moc"
And then:
Code:
qmake -project
qmake
make
Sometimes it is better to inherrit than to wrap....
Now, the problem is that you do want to change the behaviour of the tabwidget, but you do not recognize the key event?
Re: event(QEvent * event)
And here you got a version that uses eventfilters:
Code:
#include "qapplication.h"
#include "qlabel.h"
#include "qtabwidget.h"
class MyEventFilter
: public QObject{
Q_OBJECT
public:
MyEventFilter
(QWidget *parent
= 0,
const char *name
= 0)
{
if (watched->inherits("QTabWidget")) {
if(e
->type
() == QEvent::KeyPress) { Qt::Key keyPressed = (Qt::Key)k->key();
if (keyPressed == Qt::Key_Left) {
int indx = tab->currentPageIndex();
tab->setCurrentPage ((indx ==0)?tab->count():indx-1 ) ;
return true;
} else if (keyPressed == Qt::Key_Right) {
int indx = tab->currentPageIndex();
tab->setCurrentPage ((indx+1)%tab->count() );
return true;
}
}
}
return false;
}
};
int main(int argc, char* argv[])
{
tab->addTab(label1, "Label1");
tab->addTab(label2, "Label2");
tab->installEventFilter(new MyEventFilter(tab));
tab->show();
return app.exec();
}
#include "main.moc"
Re: event(QEvent * event)
Now that we have something which we can actually work with(and guess what the problem is) we can try out what is wrong.
What is wrong, is that the QTabWidget does not even get the left and right key-presses, since those are actually taken up by a child widget of its, QTabBar. To change the behaviour of the tabbar, we have to install an eventfilter onto it. And voila it works (As I would imaging you intendet...)
Code:
#include "qapplication.h"
#include "qlabel.h"
#include "qtabwidget.h"
#include "qtabbar.h"
#include "qtextedit.h"
{
Q_OBJECT
public:
ABC
(QWidget * parent
= 0,
const char * name
= 0, WFlags f
= 0) {
tabBar()->installEventFilter(this);
}
{
if (watched == tabBar()) {
if(e
->type
() == QEvent::KeyPress) { Qt::Key keyPressed = (Qt::Key)k->key();
if (keyPressed == Qt::Key_Left) {
int indx = currentPageIndex();
setCurrentPage ((indx==0)?count()-1:indx-1 ) ;
return true;
} else if (keyPressed == Qt::Key_Right) {
int indx = currentPageIndex();
setCurrentPage ((indx+1)%count() );
return true;
}
}
}
}
protected:
};
int main(int argc, char* argv[])
{
ABC *abc = new ABC();
abc->addTab(label1, "Label1");
abc->addTab(label2, "Label2");
abc->addTab(edit1, "Edit1");
abc->show();
return app.exec();
}
#include "main.moc"
Re: eventFilter(..) & InstallEventFilters()
Hi,
why is my eventFilters() on QTabWidget (..) is not able filter events of left and right arrows key board . where as same code is recognising up and down arrows
regards
raja rao
Re: eventFilter(..) & InstallEventFilters()
Quote:
Originally Posted by
rajaraob
Hi,
why is my eventFilters() on QTabWidget (..) is not able filter events of left and right arrows key board . where as same code is recognising up and down arrows
Because those events (left and right) are taken by the child widget. (The Tabbar) Since the tabbar does not care for the other key events, they are propagated to the qtabwidget.
This is why I had to install a event filter to override this behaviour (which you can only do through sub-classing, because this is the only way you can gain access to it.).