PDA

View Full Version : Problem of mouseDoubleClickEvent



Sarma
7th March 2006, 10:00
hi ,
I have a parent widget to which many widgets are added. Similarly, a QTextEdit is also added to it. But how to catch mouseDoubleClickEvent on the textedit. I have overwritten the mouseDoubleClickEvent(QMouseEvent *), but it is able to catch the event when mouse is double-clicked on the parent widget. kindly help me out, if possible.

Thanks in advance,
Sarma.

jpn
7th March 2006, 10:02
Use an event filter (unless you want to inherit QTextEdit and override the doubleClickEvent).

http://doc.trolltech.com/3.3/qobject.html#installEventFilter
http://doc.trolltech.com/3.3/eventsandfilters.html

Sarma
7th March 2006, 10:12
Sir,
Sorry that I couldnot understand that. Iam showing you the part of my code:

CPT::CPT(QWidget *parent):QWidget(parent)
{
//various child widgets are added
QTextEdit *te=new QTextEdit(this);
//some other widgets
}

Since te is the child widget of CPT, I cannot catch the mouseDoubleClickEvent on it , if I write the following:
void CPT::mouseDoubleClickEvent(QMouseEvent *)
{ ... }

How can I catch this event when mouse is double clicked on te. Kindly show the code part if possible.

Thanks alot ,
Sarma.

jpn
7th March 2006, 10:31
class CPT : public ...
{
...
protected:
bool eventFilter(QObject* o, QEvent* e);
...
};

CPT::CPT(QWidget *parent):QWidget(parent)
{
//various child widgets are added
QTextEdit *te=new QTextEdit(this);
te->installEventFilter(this);
//some other widgets
}

bool CPT::eventFilter(QObject* o, QEvent* e)
{
if (e->type() == QEvent::MouseButtonDblClick)
{
QMouseEvent* m = (QMouseEvent*) e;
// process double click
return true; // eat event
}
// standard event processing
return false;
}

Sarma
7th March 2006, 10:46
sorry sir for posting it twice. well, it is not working. Inorder to test it, I have written the following step in line 22 :
system("echo Double click detected");

But when I run it and double click on te, I couldnot find anything on the console.
Also please tell me how to use the code tags in the forums.

Thanks,
Sarma.

jpn
7th March 2006, 10:56
Did you notice line 13:

te->installEventFilter(this);
Basically it tells that you wanna filter all events going to that text edit (in this object).
So you can catch the mouse double click event without subclassing the text edit.

Wrap your code with [C O D E] .. [/C O D E] (without spaces)

Sarma
7th March 2006, 11:08
Sir,
But I have not subclassed QTextEdit. Please show me the way of executing the system("..........") command (I mean, where I have to write it and so on). Don't think otherwise. I am not an expert in Qt. So I need things to be clear for better understanding.

Thanks once again,
Sarma.

zlatko
7th March 2006, 11:18
For install eventFilter you dont must sublassing your textedit. Read previous posts closely!

Sarma
7th March 2006, 11:35
hi jojo,
Kindly explain me clearly. I couldnot understand what you have said. Where should I place the commands that I want to perform on occurance of this event.

Sarma
7th March 2006, 12:47
Sir,
I could see the message on the console. But the thing is that I could catch the event only when I double click along the borders of TextEdit. What is the procedure of making it onto the whole TextEdit area. Please help me out.

Thanks a lot,
Sarma.

jpn
7th March 2006, 13:52
I could see the message on the console. But the thing is that I could catch the event only when I double click along the borders of TextEdit.
Seems so. So the double click event goes to some child of the text edit.


What is the procedure of making it onto the whole TextEdit area. Please help me out.
I did a little test.
I installed the same event filter also to all child objects of the text edit:

te->installEventFilter(this);
QObject* child = 0;
QObjectList children = te->children();
foreach (child, children)
{
child->installEventFilter(this);
}

Then in the event filter I printed the name of the object like this:


if (e->type() == QEvent::MouseButtonDblClick)
{
qDebug() << o->objectName();
return true;
}

The output was: "qt_scrollarea_viewport"

So, the conclusion:
QTextEdit inherits QScrollView (or QScrollArea in Qt 4, which I'm using), and it's the viewport child which seems to receive the double click event.

The solution:


CPT::CPT(QWidget *parent):QWidget(parent)
{
//various child widgets are added
QTextEdit *te=new QTextEdit(this);
te->installEventFilter(this);
te->viewport()->installEventFilter(this);
//some other widgets
}

Sarma
7th March 2006, 14:16
Thanks alot jojo,
Thats working really good. So, In this way can I also implement contextMenuEvent for the TextEdit?
That must be true.Isn't It?

jpn
7th March 2006, 14:20
Thanks alot jojo,
Thats working really good. So, In this way can I also implement contextMenuEvent for the TextEdit?
That must be true.Isn't It?
You're welcome! Yep, just add a new branch to your filter :)

Sarma
7th March 2006, 14:36
hi jojo,
How is that done? Is it like this:


bool CPT::eventFilter(QObject *o,QEvent *e)
{

if( e->type() == QEvent::MouseButtonDblClick )
{
// QMouseEvent *m = (QMouseEvent *)e;
system("echo mouse double clicked");
return true;
}
else if( e->button() == QEvent::RightButton ) // Right button pressed
{
system("echo Right button pressed");
return true;
}

return false;
}

But it is giving me an error saying that there is no button() method in QEvent. Kindly solve this problem too.
with regards,
Sarma.

jpn
7th March 2006, 14:39
else if (e-type() == QEvent::ContextMenu)
See: http://www.qtcentre.org/forum/showpost.php?p=6292&postcount=11

Sarma
7th March 2006, 15:00
So, I have used what you have suggested. I want to show the following menu on the right click event. After implementing this, I couldnot see any context menu (though the textedit contains some text).


if( e->type() == QEvent::ContextMenu ) // Right button pressed
{
if(!te->lines())
{
QPopupMenu *context=new QPopupMenu(this);
context->insertItem("Reload Trace Log File",this,SLOT(reload_log()));
context->insertItem("Find in Trace Log",this,SLOT(defaults()));
context->show();
}
else {}
return true;
}
Even I have tried it without line 8 and also without "this" in line5

jpn
7th March 2006, 15:10
Does changing:

context->show();
to


QContextMenuEvent* menuevent = static_cast<QContextMenuEvent*>(event);
context->exec(menuevent->globalPos());

help?

Sarma
8th March 2006, 05:05
No.It is showing following Errors:


Error: Taking address of the bound function QWidget::event(QEvent*).
Error: Using static_cast to convert from int to QContextMenuEvent* not allowed.


If I change "event" in line1 to "e" (as we have used e previously for defining the events), I am not finding the above errors but Iam unable see the context menu on right clicking the mouse.

jpn
8th March 2006, 06:31
If I change "event" in line1 to "e"
(as we have used e previously for defining the events), I am not finding the above errors
Oops, sorry for the typo.


but Iam unable see the context menu on right clicking the mouse.
Are you hundred percent sure about this condition:

if(!te->lines())
Wouldn't that only show the context menu when there are NO lines at all...