QPushButton show/hide based on Mouse over event - c++

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.

Related

Qt Accidental activation of dragLeaveEvent,what should I do?

I have a QPushButton on a QScrollArea, the parent of the QPushButton is the QScrollArea. I drag the object to the QScrollArea and then to the QPushButton which always activates the dragLeaveEvent of the QScrollArea, I don't want this function to activate, what should I do?
Thank you for your replies, I have solved the problem.
The method is very simple, just set the parent of the QPushButton to QScrollArea::widget() and it's solved.

Click on drop-down list of QComboBox

I currently have a QComboBox in a QGraphicsScene and I need it to detect clicks. To see if there is a widget in the clicked position, I use:
void BlockScene::mousePressEvent(QMouseEvent *event)
{
if (itemAt(event->pos()) != m_widgetItem)
{
// ...
}
}
This works well for different widgets except for combo boxes where it only takes into account the original widget and not the drop-down list that appears after a first click.
To know if it came from the scene or not, I tested also by redefining mousePressEvent of the class QComboBox and same problem: It is called only when clicking on the initial widget.
Is there a way to get the drop-down list? To detect a click on it? Ideas?
You can define a custom widget for the view of the combo, or for its completer.
For example, in a subclass of QComboBox, if you need a completer, try this code. MyListView is a subclass of QListView. On that, you can reimplement the mousePressEvent method
completer()->setCompletionMode(QCompleter::PopupCompletion);
MyListView *comboView = new MyListView();
completer()->setPopup(comboView);
if you don't need the completer, do the setView directly on the combobox.
MyListView *comboView = new MyListView();
setView(comboView);

Difference between QPushButton and QToolButton

I'm new to Qt and the difference between QPushButton and QToolButton is not so clear to me.
I know that a QToolButton is usually used in a QToolBar and it usually shows only an icon, without text, but I don't quite understand the main difference between both.
Does it have any bigger difference?
When should I use QPushButton and when should I use QToolButton?
I would like to know this to use the most appropriate button, and I need to perform some GUI tests and maybe it can be relevant.
QPushButton is simply a button. QToolButton is part of a group of widgets in the QtWidgets module that operate on QActions: QMenu and QToolBar are other examples. As a result, QToolButton is much more complex under the hood than QPushButton.
Some examples of how they are different in practice:
QToolButton is tightly integrated with QAction. Changing the icon, text, or other properties of a tool button's default action is reflected on the button.
You can change the layout of the tool button contents (icon only, text only, text beside icon, text below icon). This is not possible for a QPushButton.
QToolButton supports a "split" button type: a sidebar hot zone opens a menu instead of triggering the default action.
Tool buttons can be created directly in a QToolBar by adding an action. Other widgets must be explicitly added to the toolbar.
A tool button is meant to be displayed in a grid, so it has smaller default internal margins than a push button.
QPushButton is more for "Ok"/"Close" type buttons that contain text with an optional icon.
A QToolButton should generally have an icon.
A QPushButton should always have text.
From Qt doc: http://doc.qt.io/qt-5/qtoolbutton.html#details
"A tool button is a special button that provides quick-access to specific commands or options. As opposed to a normal command button, a tool button usually doesn't show a text label, but shows an icon instead."
When i want a button in the GUI simple with only an icon, I use QToolButton. But when i want a classic button, i use QPushButton.
No big differences,

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.

How to hide completely a QGridLayout?

I have a button followed by a QGridLayout full of widgets.
I want to show/hide QGridLayout at every button click, but reading documentation of QGridLayout I see there's no show()/hide() implementation, also no setVisible() method available.
How do I achieve this?
Layouts only affect the size/position of the widgets added to them - for visibility (and anything else - event handling, focus, enable+disable) you care about the parent widget, as mentioned above. QLayout::parentWidget() gives you the widget which owns the layout, which you can then show and hide.
You didn't mention which version of Qt you're using. (I'm looking at the 4.4 documentation.)
I haven't tried this, but here are two ideas:
QGridLayout inherits the function QLayoutItem::widget(). If your layout is a widget, this will return a QWidget* on which you can call show() or hide().
If your QGridLayout is not a QWidget, you can nest it within a QWidget, and you can show() / hide() that widget instead.
I assume you have multiple QGridLayout instances, only one should be visible based on the button that has been clicked. You can use a QStackedWidget for this:
The QStackedWidget class provides a
stack of widgets where only one widget
is visible at a time.
Then, for each widget in the QStackedWidget you should associate a separate QGridLayout.
See the Qt documentation for more details