Results 1 to 7 of 7

Thread: Possible to call QMouseEvent inside a function?

  1. #1
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Possible to call QMouseEvent inside a function?

    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.

  2. #2
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to call QMouseEvent inside a function?

    Quote Originally Posted by shuawinn View Post
    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.

  3. The following user says thank you to nroberts for this useful post:

    shuawinn (23rd December 2010)

  4. #3
    Join Date
    Sep 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to call QMouseEvent inside a function?

    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.
    Last edited by shuawinn; 23rd December 2010 at 19:40.

  5. #4
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Possible to call QMouseEvent inside a function?

    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:

    Qt Code:
    1. void MyWidget::keyPressEvent( QKeyEvent* event )
    2. {
    3. if( event->key() == Qt::Key_Space )
    4. {
    5. qDebug() <<"SPACE - "<<m_pointMouseHover;
    6.  
    7. QMouseEvent* newMouseEvent = new QMouseEvent( QEvent::MouseButtonPress, m_pointMouseHover, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
    8. mousePressEvent( newMouseEvent );
    9. delete newMouseEvent;
    10.  
    11. event->accept();
    12. return;
    13. }
    14.  
    15. event->ignore();
    16. }
    To copy to clipboard, switch view to plain text mode 

    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!

  6. #5
    Join Date
    Jan 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Possible to call QMouseEvent inside a function?

    to send an event, you have postEvent and sendEvent.

  7. #6
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to call QMouseEvent inside a function?

    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
    Qt Code:
    1. void QMousePressEvent(QMouseEvent* event)
    2. {
    3. // your button-related or other conditions here //
    4.  
    5. beginPos=event->Pos();
    6.  
    7. buttonFlag=true;
    8. }
    9.  
    10. void QMouseReleaseEvent(QMouseEvent* event)
    11. {
    12.  
    13. if(buttonFlag)
    14. {
    15. endPos=event->Pos();
    16.  
    17. shape->draw(beginPos,endPos);
    18.  
    19. }
    20.  
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to call QMouseEvent inside a function?

    Quote Originally Posted by shuawinn View Post
    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
    Qt Code:
    1. void f(int) {}
    2.  
    3. void g() {
    4. f(int); // meaningless and illegal to pass type as parameter.
    5. }
    To copy to clipboard, switch view to plain text mode 

    This is what you've done.

Similar Threads

  1. Replies: 4
    Last Post: 7th May 2012, 15:39
  2. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 17:00
  3. Cannot call OpenCV 2.0 functions inside Qt slots
    By Asfer in forum Qt Programming
    Replies: 2
    Last Post: 19th February 2010, 11:48
  4. Replies: 3
    Last Post: 5th February 2010, 19:48
  5. trying to call Xlib inside qt app
    By phixx in forum Qt Programming
    Replies: 4
    Last Post: 12th February 2008, 20:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.