PDA

View Full Version : can I use "Ctrl-left mouse click" to trigger an action



qt_gotcha
31st August 2010, 17:39
...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

Urthas
31st August 2010, 21:22
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.

hobbyist
31st August 2010, 22:15
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.

Urthas
31st August 2010, 22:23
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.

qt_gotcha
1st September 2010, 15:53
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:


class nutshellqt : public QMainWindow, private Ui::nutshellqt
{
Q_OBJECT

public:
editor *newedit; //a class basd on plaintextedit with linenumbers, reimplement mouse event

public slots:
void displayVar(); // the function mouse event should call
[...]

in the editor clas I can reimplement mousePressEvent(QMouseEvent *event) and get click and ctrl like you showed.

void editor::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton &&
event->modifiers() == Qt::ControlModifier)
{
//displayVar();
}
}
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

hobbyist
1st September 2010, 18:03
Make your editor::mousePressEvent() emit a signal and connect it to your nutshellqt::displayVar() slot.

qt_gotcha
2nd September 2010, 06:11
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.

qt_gotcha
2nd September 2010, 15:25
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:

virtual void mousePressEvent(QMouseEvent *event) {
if (event->modifiers() == Qt::ControlModifier &&
event->button() == Qt::LeftButton)
emit showVar(event->pos());
}
signals:
void showVar(QPoint point);

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!

hobbyist
2nd September 2010, 15:49
You probably want to pass the event to the base class by adding QPlainTextEdit::mousePressEvent(event) at the end of your custom event handler.