PDA

View Full Version : Possible to call QMouseEvent inside a function?



shuawinn
23rd December 2010, 19:04
I am a beginner, and I have a question about QMouseEvents. I am creating a CAD system on the Parasolid Kernel and I am trying to get my user interface to be able to draw a line using the kernel. I am using a QMouseEvent to feed in the coordinates, and I need two sets of coordinates, but I would like to have the function that is receiving the coordinates call a function that grabs them and I am wondering if this is possible. Here is my code:

void ParaConnectWidget::linedrawer()
{
mousePressEvent(QMouseEvent *)
mousePressEvent(QMouseEvent *)
}

void ParaConnectWidget::mousePressEvent(QMouseEvent *e)
{
//coordinates is a vector that will store the coordinates which will be passed into //another function that utilizes the coordinates to create the line using the kernel

coordinates.append(e->x());
coordinates.append(e->y());
}
I am getting C2059 error which means it isn't even reaching the first mousePressEvent call, so I am wondering if this is just an incorrect way to call the function?

Many thanks.

nroberts
23rd December 2010, 19:15
void ParaConnectWidget::linedrawer()
{
mousePressEvent(QMouseEvent *)
mousePressEvent(QMouseEvent *)
}

That's not legal C++; you don't pass types as function parameters, only variables. You could probably build a QMouseEvent and pass it on, but I doubt that would work as you intend it to.

Most UI libraries have a way to queue new events, I'd assume Qt is no different. I don't know how though and again, I doubt that's what you want anyway.

IMNSHO, this is an incorrect use of the event system. Your linedrawer() function should be constructing something to tell some object to draw a line, something that would also be constructed by the event system when user interaction would result in the same outcome.

shuawinn
23rd December 2010, 19:29
Awesome, that answers one question. So I can't call mousePressEvent as a function inside another function? I wasn't aware that it was a type.

ymalaika
26th December 2010, 10:54
It's true that MousePressEvent is a function, but I like to think of it differently than ordinary functions that you just call directly. It's an event handler, meant to handle event objects, and it's essentially a single component of a system of objects working together.

I believe you can still do what I think you're trying to do by constructing a new QMouseEvent object first. The following code snippet "fakes" a mouse press when the user presses the spacebar:



void MyWidget::keyPressEvent( QKeyEvent* event )
{
if( event->key() == Qt::Key_Space )
{
qDebug() <<"SPACE - "<<m_pointMouseHover;

QMouseEvent* newMouseEvent = new QMouseEvent( QEvent::MouseButtonPress, m_pointMouseHover, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
mousePressEvent( newMouseEvent );
delete newMouseEvent;

event->accept();
return;
}

event->ignore();
}


Even though this is more complex than simple function calls as you're starting, I've found that this approach makes it far easier to do the sort of things more complicated widgets end up needing to do.

Hope this helps!

minirop
26th December 2010, 11:58
to send an event, you have postEvent (http://doc.qt.nokia.com/latest/qcoreapplication.html#postEvent) and sendEvent (http://doc.qt.nokia.com/latest/qcoreapplication.html#sendEvent).

AlexSudnik
26th December 2010, 20:43
Hey!

As guys have already mentioned,event handling is based on virtual functions,meaning that you're free to reimplement them in your derived classes.

I think in the most simple case,what you're trying to achieve might look like that:

*Note that you should reimplement those methods in your rendering widget


void QMousePressEvent(QMouseEvent* event)
{
// your button-related or other conditions here //

beginPos=event->Pos();

buttonFlag=true;
}

void QMouseReleaseEvent(QMouseEvent* event)
{

if(buttonFlag)
{
endPos=event->Pos();

shape->draw(beginPos,endPos);

}


}

nroberts
27th December 2010, 19:26
Awesome, that answers one question. So I can't call mousePressEvent as a function inside another function? I wasn't aware that it was a type.

I'm not sure what you mean by this. All functions are types. You "can" call mousePressEvent as a function inside of another but it's likely to misbehave. What is not legal C++ is doing something like


void f(int) {}

void g() {
f(int); // meaningless and illegal to pass type as parameter.
}

This is what you've done.