PDA

View Full Version : MousePressEvent for QTextEdit



anju123
14th August 2007, 11:05
I have used a QTextEdit on my main widget, Now the problem is that it is not recognizing the MousePress event when I click on the QTextEdit area. I have read similar posts on this problem, but nothing is working. Another strange thing is that the QTextEdit is responding to "Doubleclick" event perfectly but not for "MousePress" event. I am not able to understand this behaviour.
Any kind of help is more than welcome.

rajesh
14th August 2007, 11:29
Hi Anju,
have you used installEventFilter() for QTextEdit?

rajesh
14th August 2007, 11:44
you subdrive QTextEdit and write
void MyTextEdit::mousePressEvent(QMouseEvent *event)
{
//event->ignore();
}

OR

you can use installEventFilter
see the example:


class MyMainClass
{
Q_OBJECT


protected:
bool eventFilter(QObject *target, QEvent *event);
private:
QTextEdit * m_pTextEdit;
};

in constructor:
m_pTextEdit->installEventFilter(this);

bool MyMainClass::eventFilter(QObject *target, QEvent *event)
{
if (target == m_pTextEdit)
{
if (event->type() == QEvent::MouseButtonPress)
{
// you can write your code here, like this:
}
if (event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
std::string str = keyEvent->text().toStdString();
char ch = str[0];
if (!((ch <= '9' && ch >= '0') | (ch <= 'F' && ch >= 'A') |
(ch <= 'f' && ch >= 'a')))
{
if (ch != 9) return true;
else
return QWidget::eventFilter(target, event);
}

}
}
else
return QWidget::eventFilter(target, event);
}

anju123
14th August 2007, 13:03
I have tried the installeventFilter as well, I am now pasting the code as I think it might be the problem with my code.
Here is my class:

SelectableItem_C::SelectableItem_C(QWidget* parent)
{
_label_textedit = new QTextEdit(this);
_label_textedit->setFont(font);
_label_textedit->setAlignment(Qt::AlignCenter);
_label_textedit->setPaletteBackgroundColor(dark_background);
_label_textedit->setAutoFillBackground(true);
_label_textedit->setFrameShape(QFrame::NoFrame);
_label_textedit->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrA nywhere);
_label_textedit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
_label_textedit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
_label_textedit->setTextInteractionFlags(Qt::NoTextInteraction);


QSizePolicy sizePolicy(static_cast<QSizePolicy::Policy>(5), static_cast<QSizePolicy::Policy>(5));
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(_label_textedit->sizePolicy().hasHeightForWidth());
_label_textedit->setSizePolicy(sizePolicy);

_label_textedit->viewport()->setCursor(Qt::ArrowCursor);
_label_textedit->installEventFilter(this);
_label_textedit->viewport()->setMouseTracking(true);

}

and here is the event filter function in this class

bool SelectableItem_C::eventFilter( QObject *obj, QEvent *ev )
{
//printf("got in\n");
if ( obj == _label_textedit )
{
if ( ev->type() == QEvent::MouseButtonPress )
{
emit MousePressed_Signal(this);
return TRUE;
}
}
else
{
//printf("af: %d,%d\n",e->x(),e->y());
// pass the event on to the parent class
return QWidget::eventFilter( obj, ev );
}
return TRUE;
}
I have defined a "MousePressed_Signal" which is doing the desired work.
I could not even come in to the "If condition" with my debugging.
One more thing is that I have overridden the functions "mousePressEvent" and "mouseDoubleClickEvent" for my class. I can see that control goes to the function "mouseDoubleClickEvent" when I double click on the QTextEdit control, but not when I just do a single click on the same QTextEdit ?

rajesh
14th August 2007, 14:46
Anju,
which version of Qt you are using?
I doesn't find setPaletteBackgroundColor() in Qt 4.3

in my code mousePressEvent(QMouseEvent *event) working fine.
anyway I will test your code and write you back.

rajesh
14th August 2007, 14:57
Anju,
I tested your code in my application and mousePressEvent(QMouseEvent *event) working fine. even eventFilter() not required.
I have Qt4.3.1 on windows xp.

elcuco
14th August 2007, 20:51
Try attaching the event filter into the viewport (look into the documentation of QAbstractScrollArea.

anju123
15th August 2007, 03:44
Ok..
So it could be the problem of QT versiosn as well. I am using Qt 4.2.2 on windows Xp

jpn
15th August 2007, 11:31
It's not a problem of any specific Qt version. Read QObject::eventFilter() docs:


In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false.

anju123
16th August 2007, 07:08
Thanks to everybody,
I need to use _label_textedit->viewport()->installEventFilter(this); to get the control for required mousepress event