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
}
}
Related
i made a special videoplayer based of hikvision sdk with qtcreator in cpp
i have an void :
void QtVsPlayer::FullScr()
{
if (QtVsPlayer::isFullScreen()) {
QtVsPlayer::showNormal();
this->ui->menubar->setVisible(true);
if (!Zoomed)
this->ui->statusbar->setVisible(true);
} else {
QtVsPlayer::showFullScreen();
this->ui->menubar->setVisible(false);
this->ui->statusbar->setVisible(false);
}
return;
}
this void work, i have fullscreen video.
now in mousemove event i have
if (!this->ui->actionMasquer_les_controles->isChecked() and
WVideoCtrls->isHidden() and
this->ui->actionAuto_hide_controls->isChecked()) {
if(!Zoomed and QtVsPlayer::isFullScreen() == false)
ui->statusbar->setVisible(true);
WVideoCtrls->show();
WVideoCtrls->activateWindow();
WVideoCtrls->raise();
this->centralWidget()->lower();
this->centralWidget()->stackUnder(WVideoCtrls);
}
if (QtVsPlayer::cursor() == Qt::BlankCursor) {
QtVsPlayer::unsetCursor();
}
return;
the goal is to display videocontrols, like play, pause and so on, but not the status bar in full screen, but it is shown, zoomed is false but isfullscreen is no such value
i tried ui->centralwidget, this->isfullscreen, is->fulsreen, & , &&, and, but statusbar is displayed in fullscreen when mouse move and hide after my timer hider
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.
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 have a simple MouseArea where I'm trying to detect drag actions. Everything seemes to work fine, but wasHeld property somehow allways going false. Am I using it wrong way, or it's just broken?
MouseArea {
id: mousearea
property bool dragging: false
// ...
onPressed: {
// ...
dragging = true;
// ...
}
onReleased: {
if (mouse.wasHeld) { // allways false
dragging = false;
status.text = "was held - stop dragging";
} else {
points.add(mouseX, mouseY); canvas.drawPoint(mouseX,mouseY);
status.text = "wasn`t held - add point";
}
}
onPositionChanged: {
if (dragging) {
// ...
canvas.repaint();
}
}
}
Is there another simple way to recognize whether mouse was held or not?
UPD: solved by timing onPressed signal.
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.