Qt Accidental activation of dragLeaveEvent,what should I do? - c++

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.

Related

Can't move QDockWidget

I want to create a widget that contains several QDockWidgets on purpose of putting it into a QMainWindow. Problem is that if I add QDockWidgets to my QWidget class with layout->addWidget(dockWidget);(I don't know any other way of doing it) and then setLayout(layout) I can't do anything to the QDockWidgets but dock and undock. I can't move them, I can't position them in another place.
QMainWindow has this feature addWidgets that QWidget doesn't have. Using QMainWindow everything works perfect, but I want it to work the same if I add a QWidget object(containing some QDockWidgets) to QMainWindow.
Is there any possibility to make my QWidget fully support those QDockWidgets, and use the on full potential(move, scale, dock, change position)?
Thanks
If you're using a lot of QDockWidgets, simply enabling dock nesting might be the solution to the underlying problem.
If you absolutely need to have a widget inside the QMainWindow, you can try putting another QMainWindow in the first one. You might have to set the windowFlags property of the second QMainWindow to Qt::Widget.

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.

QGraphics scence

I have trouble using the QgraphicsScene in Qt.
When I am initializing the QGraphicsScene in the main.cpp i am able to use that. But when I am try to add that to an Widget & then add that to the main window its not working, I am not able to view anything. What is the problem?
You should put the QGraphicsScene in a QGraphicsView. Then put the QGraphicsView in your widget.

GtkVBox Qt equivalent

In GTK, I used to have a window to which I gtk_container_add()'d a GtkVBox. Then I could pack widgets I wanted to the GtkVBox to have them appear in the window.
Now I've decided to try out Qt, but I can't seem to figure out how to do this in Qt.
Right now what I've done is create a QMainWindow, but I found that you can only pack one main widget into it, which is obviously quite limiting. So I wanted to create something like the GtkVBox and use that as the main widget and then add other widgets to this box.
What I've found by Googling are however only the Q3VBox widget, which seems to be what I want, but is deprecated, and the QVBoxLayout.
I tried to use the QVBoxLayout, but it tells me that I cannot change the layout of my QMainWindow since it already has a layout.
Edit: Here is how I do it (this is in the constructor):
box = new QVBoxLayout;
setLayout(box)
It compiles fine, but during runtime, it prints on the console:
QWidget::setLayout: Attempting to set QLayout "" on HCGWindow "", which already has a layout
(HCGWindow is my app's window, which is a subclass of QMainWindow)
So, how can I create something similar to a GtkVBox in Qt, and if the solution is the Q3VBox, why is it deprecated and what other thing should I use?
Thanks
In fact, here is the recommended solution provided by the Qt documentation.
You should create a QVBoxLayout and add the widgets you want in it. After that, you set the layout on another empty widget and then you set this widget as the central widget of the QMainWindow subclass. Here is an example in code:
QWidget* widget1 = new QWidget(); // This could be anything subclassing QWidget.
QWidget* widget2 = new QWidget();
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(widget1);
layout->addWidget(widget2);
QWidget* central = new QWidget(); // Only a containing QWidget.
central->setLayout(layout);
this->setCentralWidget(central);
Now, you QMainWindow subclass should have the two QWidgets in it, arranged in a QVBoxLayout.
Note here that I did not give any parent to anyone. You could have done it, but when you call addWidget or setCentralWidget, the ownership of the widget (and the layout) is given to the containing class.
If you read a bit about Qt, you'll know that this allows the parent to destroy his children when he is about to be destroyed itself.
Finally, note that QMainWindow is an exception and, from what I know, is the only class with setCentralWidget as a method. If you attempt to create a QWidget subclass, you will be able to use setLayout (as shown in the example above).
Hope this helps.
Try to create QVBoxLayout instance, add your widgets to it and add (not replace) this layout to main window's layout.
Note, QLayout class has no member addLayout, but subclasses has one.
Firstly you must get and remember classname of main window's layout:
qDebug(this.layout()->objectName);
Then add your QVBoxLayout to window's layout:
dynamic_cast<YourWindowLayoutClass>(
this.layout())->addLayout(your_qvboxlayout_object);
I hope it will work.

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