Results 1 to 9 of 9

Thread: can I use "Ctrl-left mouse click" to trigger an action

  1. #1
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default can I use "Ctrl-left mouse click" to trigger an action

    ...or otherwise intercept ctrl-mouseclick to do something?

    to explain better, I have a plaintextedit with a scripting language. If ctrl-mouse is pressed I want to grab the word unde rthe cursor and do something.
    thanks
    Last edited by qt_gotcha; 31st August 2010 at 17:50.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    You could use a boolean to store whether or not the control key is being pressed. Reimplement your widget's keyPressEvent() to toggle the boolean to true as appropriate, and its keyReleaseEvent() to toggle the boolean to false. Then, in the mouseReleaseEvent(), check the state of the boolean and react accordingly.
    Last edited by Urthas; 31st August 2010 at 21:49.

  3. #3
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    I addition to what already has been proposed, you could alternatively check the Qt::ControlModfier bit in QMouseEvent::modifiers() when the mouse release event occurs.

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    Quote Originally Posted by hobbyist View Post
    I addition to what already has been proposed, you could alternatively check the Qt::ControlModfier bit in QMouseEvent::modifiers() when the mouse release event occurs.
    You are too diplomatic. Rather than "in addition to", you should have said "instead of". Thanks for showing the polished way of doing what I suggested, now I know too.

  5. #5
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    thanks for your suggestions! very helpful

    newby question: the event is done in a plaintextedit that is created in runtime. The ctrl-click within this plaintextedit should activate a function that is not part of this class. So something like:
    Qt Code:
    1. class nutshellqt : public QMainWindow, private Ui::nutshellqt
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. editor *newedit; //a class basd on plaintextedit with linenumbers, reimplement mouse event
    7.  
    8. public slots:
    9. void displayVar(); // the function mouse event should call
    10. [...]
    To copy to clipboard, switch view to plain text mode 

    in the editor clas I can reimplement mousePressEvent(QMouseEvent *event) and get click and ctrl like you showed.
    Qt Code:
    1. void editor::mousePressEvent(QMouseEvent *event)
    2. {
    3. if (event->button() == Qt::LeftButton &&
    4. event->modifiers() == Qt::ControlModifier)
    5. {
    6. //displayVar();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    How can I then call the function displayVar() which is not part of that class? I tried
    nutshellqt::displayVar();
    but that doesn't work obviously, do I do something with "parent"?
    thanks
    Last edited by qt_gotcha; 1st September 2010 at 17:39.

  6. #6
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    Make your editor::mousePressEvent() emit a signal and connect it to your nutshellqt::displayVar() slot.

  7. The following user says thank you to hobbyist for this useful post:

    qt_gotcha (3rd September 2010)

  8. #7
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    thanks that works almost, except that in the function displayVar the textcursor position is reset to the beginning of the plaintextedit, even if I pass the textcursor as variable between the signal and the slot. Trying different solutions now.

  9. #8
    Join Date
    Jul 2009
    Posts
    92
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    ok a final request if you are still following this thread!
    my solution is as follows, I need to give the mouse coordinates ellse these are not known in the slot function:
    so in editor.h:
    Qt Code:
    1. virtual void mousePressEvent(QMouseEvent *event) {
    2. if (event->modifiers() == Qt::ControlModifier &&
    3. event->button() == Qt::LeftButton)
    4. emit showVar(event->pos());
    5. }
    6. signals:
    7. void showVar(QPoint point);
    To copy to clipboard, switch view to plain text mode 
    and this calls displayVar(QPoint point)

    This works, but now the regular left mouse presses are not registered, they are absorbed by this function it seems (double click is ok but that is a different event). Do I have actively opass on clicks to the application?

    solution: implement this thing above in the mouse release function works!

    thanks for all the help!
    Last edited by qt_gotcha; 2nd September 2010 at 15:45.

  10. #9
    Join Date
    Jul 2010
    Posts
    21
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: can I use "Ctrl-left mouse click" to trigger an action

    You probably want to pass the event to the base class by adding QPlainTextEdit::mousePressEvent(event) at the end of your custom event handler.

  11. The following user says thank you to hobbyist for this useful post:

    qt_gotcha (2nd September 2010)

Similar Threads

  1. keyPressEvent - "ctrl + tab" handle two time
    By somename in forum Qt Programming
    Replies: 7
    Last Post: 30th May 2010, 20:29
  2. Shortcut: CTRL + SHIFT + "letters"
    By smarinr in forum Qt Programming
    Replies: 17
    Last Post: 22nd May 2009, 09:34
  3. Background Style Disables QScrollBar "Drag" Action
    By brent99 in forum Qt Programming
    Replies: 1
    Last Post: 3rd June 2008, 20:16
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

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.