Extract the top corner position of QToolButton Widget - c++

I am trying to extract the cordinates of a toolbutton. So when there is a popup it always starts from the top left corner of the widget. I am doing something like this
menu_something->popup(mapToGlobal(ui.toolButton->pos()));
However the menu shows up no where close to the toolButton. Any suggestions ?

Running with QDialog (pushable and styleable ComboBox Dialog):
Get coordinates by calling this in a subclass of QToolButton
QPoint mypoint = QWidget::mapToGlobal(QPoint(0,0));
and use
QRect myrect;
myrect.setCoords(topleft.x(),topleft.y(),topleft.x()+width,10);
YourPopupDialog.setGeomentry(myrect);
YourPopupDialog.setFocus();
YourPopupDialog.show();
to position the dialog upon the button.
Edit for use with QMenu running in slot conntected to clicked():
QMenu menu;
menu.addAction("Text 0");
menu.addAction("Text 1");
menu.exec(ui->toolButtonMenuButton->mapToGlobal(QPoint(0,0)));

Related

How to know if the mouse hovers over a widget in QT?

I am trying to implement in my program, that if the mouse hovers over my QChartView widget, that I get back the coordinates of the cursor.
I already tried, by installing an event filter on the widget
ui->chartView->setMouseTracking(true);
ui->chartView->installEventFilter(this);
and then writing the method for a mouse event
void MainWindow::mouseMoveEvent(QMouseEvent* event) {
qDebug() << event->pos();
}
but, i only get output when I click on the Mainwindow and hold the mousebutton which I clicked. When I click on the widget chartView I dont get any output.
I need to get output, when the mouse is hovering over the chartview
You don't need an event filter. You need to reimplement the methods QWidget::enterEvent() and QWidget::leaveEvent().
enterEvent() is called when the mouse enters the widget and leaveEvent() when it leaves it again.

Qt: Creating button on QGraphicsItem

I have QGraphicsScene and QGraphicsView. I'm adding QGraphicsRectItem to scene and I've encounter some difficulties...
I want to create button on my Item. I've tried to create QPushButton but when I used show() method it appears in new window. I've also tried do another QGraphicsRectItem that could be button, but I don't know how to place it in my QGraphicsRectItem that is placed on scene.

I want to show menu when cursor hovers over pushbutton and close menu when cursor is moved away

I am using Qt on Ubuntu.
I have a menu on QPushButton. I want to show menu when cursor hovers over the QPushButton and close menu when cursor is moved away.
Showing a popup menu on "hover" event seems to violate the user experience, as users expect to see the popup when they click the button. This is called a menu button. If you really want to use hover event, you may subclass the QPushButton class and use its respective events. However if you would like to use a menu button, you can try this:
QMenu *menu = new QMenu();
QAction *testAction = new QAction("test menu item", this);
menu->addAction(testAction);
button->setMenu(menu);
Documentation on QPushButton::setMenu.
You have to implement your owen QPushButton. Let's start by checking the MouseMoveEvent to handle when the mouse hover the widget.
To check if the cursos pos is inside your widget:
void CustomPushButton::mousePressEvent(QMouseEvent *e) {
const QRect widgetRect = ui->followersWidget->geometry();
const QPoint mousePos = ui->followersWidget->mapFromGlobal(QCursor::pos()); // or e->pos()
if (widgetRect.contains(mousePos)) {
// Mouse inside the widget, lets show the menu
} else {
// Mouse outside the widget, if the menu is open, close it.
}
QWidget::mousePressEvent(e);
}
To show/hide the menu you could use the QMenu::popup(..), from Qt Doc:
Displays the menu so that the action atAction will be at the specified global position p. To translate a widget's local coordinates into global coordinates, use QWidget::mapToGlobal().

How to keep a QSlider activated to allow movements with arrows at any time

I would like to be able to move a QSlider with arrows of the keyboard at any time.
I want to be able to click anywhere on the QWindow and keep QSlider activated to move the cursor with the arrows.
My problem is that move the cursor with arrows is only allowed if we click on the QSlider before.
I hope my question is clear enough.
Does anyone know how to move the QSlider with arrows of the keyboard without clicking on the QSlider before please?
There are two approaches:
In Qt terms, you'd like to give slider the focus. Widgets have the setFocus method, so you need to call slider->setFocus(Qt::OtherFocusReason).
Since you want the slider to get focus whenever the underlying window has focus, you need to put the setFocus call in your implementation of focusInEvent for the parent widget.
You can forward the key events from the underlying widget to the slider. In the parent widget, reimplement keyPressEvent and keyReleaseEvent. When the desired keys are detected, forward them to the slider:
// same for keyReleaseEvent!
void MyWindow::keyPressEvent(QKeyEvent * ev) {
if (ev->key() == Qt::Key_Up || ev->key() == Qt::Key_Down) {
slider->event(ev);
}
}

X Close Button in QToolButton

How can I display an X close button in the top right corner of subclassed QToolButton, and control its the behavior?
Subclass QToolButton and in constructor of your class e.g. MyToolButton add a child QPushButton on top of your tool button.