How to set QTextEdit to only start editing on mouse click/release? - c++

I tried setFocusPolicy(Qt::TabFocus); but the cursor starts flashing and gets focus as soon as the mouse is pressed on it.
Is there a way to change it to mouse click?
I'm using Qt 4.6.3 on a touch screen device.
When QTextEdit gets the focus, then the KineticScroller stops working.
What I want to achieve:
1) On mouse press event QTextEdit does nothing.
2) On mouse release event, if the release is within the rect() of QTextEdit, then it gets focus and start editing.
3) If not in rect() don't do anything.
I know how to acheive 2) and 3) by rewriting mousePressEvent() and mouseReleaseEvent() but I have no I idea how not to let it get focus on mouse press.
Can someone share some thoughts on why setFocusPolicy(Qt::TabFocus); doesn't do this for me?

Related

How to detect mouse click on the entire window?

I want to check if the mouse button is clicked or released on my window in order to change the cursor. For now I have the signals, connected to the main window, but the problem is that , when I hover my mouse cursor over the button or GLArea, the events I linked to the window do not work, so the cursor does not change. By the way, all buttons in my application have their own signals, so, to change the cursor state I should duplicate the function below for all widgets, which does not seem good.
window->signal_button_release_event().connect(sigc::bind(sigc::ptr_fun(&MouseBtnReleased), window));
window->signal_button_press_event().connect(sigc::bind(sigc::ptr_fun(&MouseBtnClicked), window));

How to enable mouse tracking on a QWindow

I'm using a QWindow (Not a QMainWindow) with OpenGL. I need to use a QWindow to correctly control the OGL context.
I'm trying to follow the Scribble example to implement something similar to panning, but I can't find a paradigmatic way to trigger the mouseMoveEvent().
How can I get a "tooltip" effect where mouseMoveEvent() is constantly triggered, similar to setMouseTracking()?
It works fine for me. I created a test program with a MainWindow that inherits QWindow instead of QMainWindow, and handles the mouse move event to print the cursor position:
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
qDebug("%d, %d", e->pos().x(), e->pos().y());
}
It works, as I move the mouse I get events even without pressing any mouse buttons.
If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved.
you can call hasMouseTracking() or setMouseTracking() to control mouse's tracing state.
When mouse is traced, mouseMoveEvent() will be called, and you can reimpletment mouseMoveEven() to acquire mouse position, just as #sashoalm did.
BTW, mouse event is always transfered to your app, but filtered by it's parent or itself. You can reimpletment eventFilter() to code your own filter.

Drag and drop a button using CCControlbutton

I am new to cocos2d-x and I am developing a game in x-code using cocos2d-x2.0.4. In my game I created a button using CCcontrolbutton. Now I want to drag my button to one place to another. I tried with all the CCControlEvents but it doesn't work. Now I want to know, is it possible to drag and drop a button using CCControlbutton. I pasted my code which I have used to create the button.
button1 = CCControlButton::create(CCScale9Sprite::create("7.png"));
button1->setAdjustBackgroundImage(false);
button1->setPosition( ccp(winwsize/6, winhsize/7) );
button1->addTargetWithActionForControlEvents(this, cccontrol_selector(plus::add),CCControlEventTouchDragOutside);
button1->addTargetWithActionForControlEvents(this, cccontrol_selector(plus::add),CCControlEventTouchDragInside);
this->addChild(button1, 4);
In add() I have given the code to enter next scene. But now it is entering while clicking the button. But i want while drag it to one position to another. If it is possible to drag a button using CCControlbutton then please give me some sample codes. Thanks.
The events CCControlEventTouchDragInside and CCControlEventTouchDragOutside are happening when the user's touch (his fingertip) is entering or leaving your button while already or still touching the touchscreen (or while the mouse button is still pressed while the mouse pointer moves).
You would need to observe the dragging process yourself. Starting with a click on your button, change the button's position while the user is dragging (to visualize the dragging process), and then call plus::add() when the dragging ends in your target area.

What could be the reason of twice tap (touch) to press onscreen button, that presses from single mouse click?

I must say first - I AM NOT THE PROG OWNER, I don't know how it works and etc, I just need an advice that I could give them.
The program is running on windows based tablet PC with windows 8.
There is some prog that uses OpenGL ES 2.0. It renders some buttons and displays those.
Those buttons can be pressed by mouse left button, but if you use sensor screen, you must tap that button twice for single press.
Shortly - some button displayed.
Mouse left button single click -> button pressed
Single finger tap -> button not pressed
Double finger tap -> button pressed.
I don't understand why that happens. Single tap should imitate single click... Weird.
Anyone have any ideas?
I am not sure about this, but I think this may have something to do with how the program checks if the button is pressed. (long long time ago in my C++ days) If it checks the coordinates on click event then there is a possibility that when you touch the button, it only registers the location of the press, and not the press itself. On the second touch the location is already on the button so the click event is true on that button.
Anyone with more insight please feel free to edit ;-)

Qt mouse leave event while button is pressed

I need to detect if the mouse pointer leaves my custom widget even if a mouse button is pressed.
According to this post, Qt does not cause a leaveEvent in case a button is pressed, at least not in version 4.4. I am working with 4.7.3, but still do not get a leaveEvent in the described case. I also tried with the various drag-related events, but no luck. Does anyone have an idea how to deal with this?
Actually there is an even better option than using the mouse events of the parent widget: You can implement the mouseMoveEvent function of the child widget and get the QMouseEvent::pos() position which is relative to the top left corner of the widget. That means, if you know the size of the widget (use for example QWidget::rect()), you can compute within the widget if the mouse pointer is still on the widget or not without having to change the parent widget.
Well, I did something similar to #gregseth.
Write your own MouseLeaveEvent that is invoked from MouseMoveEvent
when the mouse's position is outside of the widget's boundary.
As Qt's documentation says "Mouse move events will occur only when a mouse button is pressed down"
I got the same problem. The only workaround I found is to manage the mouseMove event of the parent widget, and to check if the position of the cursor is inside or outside the boundaries of the widget you want the event on.