X Close Button in QToolButton - c++

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.

Related

Qt: QDialog - activate windows taskbar icon (C++)

I just can not understand one seemingly basic thing.
If we want to show our custom dialog, we can do smth like this:
OurDialog * dlg = new OurDialog; // (this);
dlg->setAttribute(Qt::WA_DeleteOnClose);
dlg->show();
dlg->activateWindow();
hide(); // hide MainWindow
Depending on giving a parent to constructor or not we can make taskbar icon visible or not.
But how to make the icon not only visible but also active?
Moreover, if we move the string
hide(); // hide MainWindow
before
dlg->show();
The taskbar icon will be active, but in this case we'll get a "blinking effect" on showing the dialog.
So is there any possibility to hide MainWindow, show Dialog and make the taskbar icon active?
Thank you!

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().

Extract the top corner position of QToolButton Widget

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)));

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);
}
}

QPushButton show/hide based on Mouse over event

I have one question regarding the QPushButton.
i want the QPushButton behaviour in such a way that it should be shown only when the focus is there on QPushButton, and when the focus is out then it should hide. Below is the image that have "View" button, it displayed only when the focus is there on the QPushButton.
Thanks,
Neel
Subclass QWidget.
Create a QPushButton member.
Override the QWidget::enterEvent and QWidget::leaveEvent protected methods to show / hide the QPushButton.
Override the QWidget::resizeEvent to resize the QPushButton.