my code as follows, i followed the example on the qt doc but nothing is being drawn on my widget, any one knows whats wrong? Thanks!
ui.axWidget_X->installEventFilter(this);
bool qtTest::eventFilter(QObject * obj, QEvent * event)
{
if((QAxWidget *)obj == ui.axWidget_X && ((QMouseEvent*)event)->button() == Qt::LeftButton)
{
if(event->type()== QEvent::MouseButtonPress)
{
origin = ((QMouseEvent*)event)->Pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
return true;
}else if(event->type()== QEvent::MouseButtonRelease)
{
rubberBand->hide();
//
return true;
}else if(event->type() == QEvent::MouseMove)
{
rubberBand->setGeometry(QRect(origin, ((QMouseEvent*)event)>Pos()).normalized());
return true;
}
}
}
Your mousemove event handler is placed within the left button click checking parent condition. Since for a MouseMove event "((QMouseEvent*)event)->button() == Qt::LeftButton" will return false, the MouseMove event is not handled..
Solution:
Move the "else if" part handling MouseMove outside the if condition.
Related
Can anyone please help on below requirement, how to implement.
When I click on radio buttons with keyboard up and down arrows it is moving up and down normally .
But I need ,when we click on propulsion button and then if I press down arrow radio button should go to off.
And when we are on off, if we press up arrow rado button should go to propulsion.enter image description here
Implemented the above using below code
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(obj == ui->mGbPowerModes)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if(keyEvent->key() == Qt::Key_Up)
{
if(ui->mBtnOff->isChecked())
{
on_mBtnPropulsion_clicked();
ui->mBtnPropulsion->setFocus();
ui->mBtnPropulsion->setChecked(true);
}
return true;
}
else if(keyEvent->key() == Qt::Key_Down)
{
if(ui->mBtnPropulsion->isChecked())
{
on_mBtnOff_clicked();
ui->mBtnOff->setFocus();
ui->mBtnOff->setChecked(true);
}
return true;
}
}
return false;
}
return MainWindow::eventFilter(obj, event);
}
here is a minimal Example on how you can specify navigation with arrowkeys like you want it, you can use the "KeyNavigation.X" property to tell the next focus item when you press the defined key. - hope this helps:
Column {
Label {
text: qsTr("Radio:")
}
RadioButton {
id: test1
checked: true
text: qsTr("DAB")
KeyNavigation.down: test2
KeyNavigation.up: test3
}
RadioButton {
id:test2
text: qsTr("FM")
KeyNavigation.down: test3
KeyNavigation.up: test1
}
RadioButton {
id:test3
text: qsTr("AM")
KeyNavigation.down: test1
KeyNavigation.up: test2
}
}
I've derived a class from QQuickPaintedItem in which I want to handle the mousePressEvent and the mouseReleasEvent (and also the mouseMoveEvent but that is not my prolem now).
The mousePressEvent gets called properly everytime the left mouse button is pressed. But the mouseReleaseEvent gets only called after a double click. What I expected is to get the event everytime the button is released. How can I change this?
This is what I do:
MyView::MyView(QQuickItem *parent):
QQuickPaintedItem(parent)
{
setAcceptedMouseButtons(Qt::LeftButton);
}
void MyView::mousePressEvent(QMouseEvent *evt)
{
//gets called after every single mouse click
qDebug("mousePressEvent");
if(evt->button() == Qt::LeftButton)
{
//do something...
evt->accept();
}
else
{
evt->ignore();
}
QQuickPaintedItem::mousePressEvent(evt);
}
void MyView::mouseReleaseEvent(QMouseEvent *evt)
{
//gets only called when releasing the mouse button after a double click
qDebug("mouseReleaseEvent");
if(evt->button() == Qt::LeftButton)
{
//do something...
evt->accept();
}
else
{
evt->ignore();
}
QQuickPaintedItem::mouseReleaseEvent(evt);
}
So I finally found the solution!
Calling the base class implementation is a bad idea since the base class simply calls ignore() on the event. Here is the base class implementation:
void QWindow::mousePressEvent(QMouseEvent *ev)
{
ev->ignore();
}
void QWindow::mouseReleaseEvent(QMouseEvent *ev)
{
ev->ignore();
}
So this is how it works:
MyView::MyView(QQuickItem *parent):
QQuickPaintedItem(parent)
{
setAcceptedMouseButtons(Qt::LeftButton);
}
void MyView::mousePressEvent(QMouseEvent *evt)
{
//gets called after every single mouse click
qDebug("mousePressEvent");
if(evt->button() == Qt::LeftButton)
{
//do something...
evt->accept();
}
else
{
evt->ignore();
}
//DON'T DO THIS:
//QQuickPaintedItem::mousePressEvent(evt);
}
void MyView::mouseReleaseEvent(QMouseEvent *evt)
{
//now gets called with every mouse release since we don't call the base class any more
qDebug("mouseReleaseEvent");
if(evt->button() == Qt::LeftButton)
{
//do something...
evt->accept();
}
else
{
evt->ignore();
}
//DON'T DO THIS:
//QQuickPaintedItem::mouseReleaseEvent(evt);
}
I couldn't find anything in the docs. I found one solution that uses PyQt or something, but I'd rather not have to use that. Also, it would be pretty nice if there was a signal for it, but it doesn't look like there is.
1.When you install an event filter as Sam suggested (m_lineEdit->installEventFilter(this);) you need to handle QEvent::KeyPress and check the key to be equal Qt::Key_Tab:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == m_lineEdit) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Tab)
{
//do what you need;
return true;
}
}
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
2.Another way to do that is to create a new class inherited from QLineEdit and reimplement keyPressEvent:
void LineEdit::keyPressEvent(QKeyEvent* event)
{
if (keyEvent->key() == Qt::Key_Tab)
{
emit tabPressed();
return;
}
QLineEdit::keyPressEvent(event);
}
You should be able to use QObject::installEventFilter(QObject*) to intercept the key press event. There's an example here: http://qt-project.org/doc/qt-4.8/qobject.html#installEventFilter .
I am using QMouseEvent and QKeyEvents in my program. I programmatically make various widgets (QDockWidgets and QCustomPlots).
I would like to use a single click and keypress handler function. However, I am struggling to get the widget that is actually being clicked, so that I can do certain stuff within that widget.
Is there a way to return from a ClickEvent the name of the widget that was clicked?
You can implement eventFilter in your mainWindow and listen for events from widgets there :
bool MainWindow::eventFilter(QObject * obj, QEvent * event)
{
if((myWidget *)obj == widget1 && event->type()==QEvent::KeyPress)
{
int pressedKey = ((QKeyEvent*)event)->key();
...
}
else if((myWidget *)obj == widget2 && event->type()==QEvent::MouseButtonRelease)
{
if(((QMouseEvent*)event)->button() == Qt::LeftButton)
{
...
}
}
return false;
}
Also do not forget to install event filters for your widgets in the mainWindow constructor :
widget1->installEventFilter(this);
widget2->installEventFilter(this);
So I have a ON-OFF button that draws a circle. The trouble I am encountering is that the ON OFF states are random depending on how long I press the button. I guess this is due to the draw() function which also loops my button function in time with framerate. What I want is for the button to turn on when pressed once and turn off when pressed again irrespective of how long the button is pressed. Here is the code.
else if (circle4.pressed()) {
println("button 4 is pressed");
if(drawCirclesPrimary){
drawCirclesPrimary = false;
}
else{
drawCirclesPrimary = true;
}
println("drawCirclesPrimary"+drawCirclesPrimary);
}
I would suggest looking at the Buttons tutorial on processing.org. The following code is a subset of what is contained in the tutorial (you will need to review all the code in the tutorial, however). Comments are mine.
void setup() {
// Create instances of your button(s)
}
void draw() {
// Draw buttons, update cursor position, check if buttons have been clicked.
}
// Provides the overRect() method (among others).
class Button
{
// If the cursor is placed within the footprint of the button, return true.
boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width && mouseY >= y && mouseY <= y+height) {
return true;
}
else {
return false;
}
}
}
class RectButton extends Button
{
// Create a rectangle button with these size/color attributes.
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
// Determines whether the cursor is over the button.
boolean over()
{
if( overRect(x, y, size, size) ) {
over = true;
return true;
}
else {
over = false;
return false;
}
}
// Draws the rectangle button into your sketch.
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
This thread has some example code for drawing an object only while a key is pressed. That's pretty similar to what you want.
Instead of keyPressed and keyReleased, you can use mouseClicked. which is called once after a mouse button has been pressed and then released. Use a boolean variable to store the on/off state. Inside of mouseClicked, toggle the value of that boolean variable.