PDA

View Full Version : MouseEvent on QTextEdit



Sarma
9th March 2006, 11:21
Sir,
I have a textedit which is the child widget of another parent. I have implemented the mouseDoubleClickEvent on the text edit and my requirement is that when I doubleclick on a line, I should get that entire line. Here is a part of my code:


if( e->type() == QEvent::MouseButtonDblClick ) // Mouse Double clicked
{
int *para1,*para2,*index1,*index2;
if( te->hasSelectedText() )
{
system("echo mouse clicked");
te->getSelection(para1,index1,para2,index2,0);
}

QString line=te->text(*para1);
// print line in another widget for testing purpose
//call to another function
return true;
}

Now, when I doubleclick the mouse, I could not see the output on console. And when I remove the if -condition inside, I could see the output on console.But the program is exiting itself by reporting "Bus error" on the console. Kindly help me in solving this problem.
Thanks and regards,
Sarma.

zlatko
9th March 2006, 11:33
Its ok becouse when you catch your doubleclick text is not selected yet :rolleyes:

Sarma
9th March 2006, 11:36
hi,
I understood something. Generally when we double click in a text edit, we see that word being selected which is not happening in my application. Hence the if-condition fails and no output is printed on the console. And when I remove the if-condition, exiting of the program by reporting "Bus error" occurs because of the 7th line.
But that doesnot solve my problem . Can anyone help me in this regard?

Sarma
9th March 2006, 12:35
Dear Wysota sir,

You said that we can get the line by calculating the cursor positions.If it is so, please let me know the steps that I have to write to get the application working.It got struck up since morning just because of this :(

Regards,
Sarma.

jacek
9th March 2006, 13:06
int *para1,*para2,*index1,*index2;
if( te->hasSelectedText() )
{
system("echo mouse clicked");
te->getSelection(para1,index1,para2,index2,0);
}
This should be:
int para1, para2, index1, index2;
if( te->hasSelectedText() )
{
system("echo mouse clicked");
te->getSelection( &para1, &index1, &para2, &index2, 0 );
}
Otherwise QTextEdit::getSelection() will overwrite some random memory location which will cause a segmentation fault.

Sarma
9th March 2006, 13:22
hi jacek,
well, thats not the solution because when there is no text selected, u are not supposed to use getselection() method whatever may be the usage of its parameters.
I have found the solution to maximum extent but I couldnot make it till the end. The have changed the code like this:


int para= te->paragraphAt(QCursor::pos()); // te is the textEdit object
QString line=te->text(para);


But the thing is when I load some file into textedit, the cursor will be at the end of the textedit. So, at any time, the first line returns the last para number.
Now, Please tell me how to move the cursor to the place where Iam clicking the mouse.

Thanks and regards,
Sarma.

jacek
9th March 2006, 13:32
Now, Please tell me how to move the cursor to the place where Iam clicking the mouse.
Shouldn't this happen automatically?

Sarma
9th March 2006, 13:37
jacek,
Please don't pose those kind of questions, my dear. kindly tell the answer.

jacek
9th March 2006, 13:55
Please don't pose those kind of questions, my dear. kindly tell the answer.
Why? Is all you want is a fish?

QTextEdit provides such functionality out of the box. Why do you want to implement something that is already implemented?

Sarma
10th March 2006, 04:55
hi,
Yes, QTextEdit has got such kind of functionality. But I don't know why it is not happenning in my application. Iam showing you a part of my code:


if( e->type() == QEvent::MouseButtonDblClick )
{
int para1= te->paragraphAt(QCursor::pos());
QString line=te->text(para1);
return true;
}


Now, when mouse is DoubleClicked, para1 is holding the number of the last line in the textedit(te). Therefore Iam unable to get any text in "line". Is there any method (or what is the possible step) that I can follow to make it working? :(

Thanks and Regards,
Sarma.

jacek
10th March 2006, 09:14
Where is that code snippet located? Is it from event filter?

Sarma
10th March 2006, 09:21
see the thread by name "How to change paragraph number in QTextEdit?"

jacek
10th March 2006, 09:28
see the thread by name "How to change paragraph number in QTextEdit?"
But in what method that code snippet is located? And why did you open another thread, if it is the same problem?

Sarma
10th March 2006, 09:34
hi,
I think you didnot see the code correctly. There is only one if-condition , present in the code. And I have posted a new thread as It has become a different problem now.

regards,
Sarma.

jacek
10th March 2006, 09:49
There is only one if-condition , present in the code.
But in what method did you place that if statement?