Context sensitive help on Qt - c++

What I want to do is, when I place mouse pointer over any GUI component (like push button, radio buttons, etc.) and press the F1 key, the relevant help should be shown. I was able to get mouse coords, but couldn't get the GUI element underneath it dynamically. I am using Qt 4.7.4 and Qt Creator 2.4.
How can I achieve this?

There is a method in QApplication, that may be useful to you :
QWidget * QApplication::widgetAt ( const QPoint & point ) [static]
Then you should name every widget accordingly to your needs.

Related

How to set Focus on a specific widget

I'm trying to implement GUI with various widgets for OpenGL project. I have a main widget for drawing the scene. The OpenGL widget is associated with Key and Mouse events, therefore the focus should always be on it. I've noticed if I click on say a push button, then the focus is moved to this button which means the focus is no longer associated with OpenGL widget. Clicking the widget by the mouse is not changing the focus. One of the solution is to turn off the focus for all widgets except the OpenGL widget in the GUI as follows
ui->processButton->setFocusPolicy(Qt::NoFocus);
ui->quitButton->setFocusPolicy(Qt::NoFocus);
ui->clearButton->setFocusPolicy(Qt::NoFocus);
ui->textEdit->setFocusPolicy(Qt::NoFocus);
ui->groupBox->setFocusPolicy(Qt::NoFocus);
if I have many widgets, then this solution is annoying especially if I add widgets later on. My question is is there a solution to set the focus on a specific widget?
Your solution is fine, you just shouldn't be enumerating widgets manually:
// C++11
for (auto widget : findChildren<QWidget*>())
if (! qobject_cast<QOpenGlWidget*>(widget)) widget->setFocusPolicy(Qt::NoFocus);
// C++98
foreach (QWidget * widget, findChildren<QWidget*>())
if (! qobject_cast<QOpenGlWidget*>(widget)) widget->setFocusPolicy(Qt::NoFocus);

QT window within window?

I'm setting up a small code editor using QT and following this example. However, i'm curious on how to create windows within windows or widgets within widgets. I'm trying to achieve something similar to these:
http://i.stack.imgur.com/Vn8Ut.png
http://www.hanselman.com/blog/content/binary/Windows-Live-Writer/Download-Visual-Studio-2013-while-your-f_1431E/image_4eb5427c-1ae7-4464-9c26-2282fe8d06c3.png
Is there an example of overlaying widgets like this?
Any alternative soloution for QMessagebox for IOS development (QWidget application only)?
I gave an example of getting another QWidget to be embedded and painted on top of another one. Let me know if you have any questions about how it was done.
The PopUp flag and Qt::Tool options are also relevant.
Be sure to check out: the ToolTip property of a QWidget and the WhatsThis property of QWidget.
http://qt-project.org/doc/qt-5/qwidget.html#toolTip-prop
http://qt-project.org/doc/qt-5/qwidget.html#whatsThis-prop
There are also other ways to make borderless, focusless windows that hover and disappear quickly on command. The Window Flags and Widget Attributes in Qt are very powerful when you are looking to modify Qt Widgets.
When you parent a Widget to another widget, it will draw itself on top of the other. Then you just need to resize and position it properly.
Also subclassing existing widgets can give you more options.
Draw text on scrollbar
Also common Qt::Tools that you will find are QDockWidgets. They are awesome!
Hope that helps.
Take a look at Qt Namespace especially Qt::WA_LayoutOnEntireRect and Qt::WA_StyleSheet. Pass it as a widget attrybutes. The second option looks promising but you have to create style sheet for QWidget.

Qt QGraphicsview how to hook to resize event

we're working on a Qt C++ Widget project and recently ve'we run into trouble. We're Qt rookies.
On our widget there are two QGraphicsView which need to resize automatically when the window is resized (we've done that) and keep the content inside them resized/fit/scaled accordingly to...
So we've figured we need to somehow hook to onResizeEvent or find which slot does it. But we're somehow lost as to how to do it.
P.S.: Please excuse my english.
If you have a QWidget you only need to reimplement the resizeEvent function.
void QWidget::resizeEvent ( QResizeEvent * event ) [virtual protected]
In the Qt documentation you can find the scribble example for a full code example: http://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html
Alternatively you can install an event handler and grab the QEvent::Resize.
How to install event filters is described here: http://qt-project.org/doc/qt-4.8/eventsandfilters.html
You can make your custom class which inherits from QGraphicsView. You should reimplement resizeEvent( QResizeEvent *event ) in your custom QGraphicsView like:
void MyView::resizeEvent(QResizeEvent *event)
{
fitInView(0, 0, 500, 500,Qt::KeepAspectRatio);
QGraphicsView::resizeEvent(event);
}
This way the view will always display the whole scene. I.e. if the window size is changed and the graphicsView is resized, The scene gets scaled and you can see everything appropriately.
If you are resizing in some slot then you can use sender().
Btw you can always use debugger or logs/backtraces to get such information.

Customizing Qt Dock

What I'm looking to do is use the Qt Dock and the Dock widgets as they are, except I would like the show only the widget as opposed to the whole Dock.
to clarify: when a docked widget is brought into view, the whole dock appears and the widget sits on the dock. I would like to hide the portion of the dock which isn't being occupied by the widget, is there any way to do that?
Please let me know if I can clarify further.
I don't think it is feasible with Dock widgets. There are 4 fixed dock areas on a QMainWindow which are shown if a dock widget is dropped onto them.
What you might try is to disable actual docking with void setAllowedAreas(Qt::NoDockWidgetArea) and handle "snapping" yourself with void QWidget::moveEvent ( QMoveEvent * event ), snapping the widget to the main window edges if it comes within x pixels of it.
Hummm this seems difficult...
For a start, use QDockWidget::setTitleBarWidget ( QWidget * widget ) to set a custom title bar. You can create a special widget which has a small minimum size, minimum size hint, and which is not painted (or painted as invisible)...

mouse over transparency in Qt

I am trying to create an app in Qt/C++ with Qt4.5 and want the any active windows to change opacity on a mouseover event...
As i understand it, there is no explicit mouseover event in Qt.
However, I got rudimentary functioning by reimplementing QWidget's mousemoveevent() in the class that declares my mainwindow. But the mainwindow's mousemoveevent is not called whenever the mouse travels over any of the group boxes i have created in there (understandbly since QGroupbox has its own reimplementation of mousemoveevent).
So as a cheap work around, I am still using the mousemoveevent of my mainwindow but a query the global mouse position and based on the (x,y) position of the mainwindow (obtained through ->pos()) and the window size (-> size -> rHeight and rWidth), I check if the mouse is within the bounds of the area of the mainwindow and change the opacity thus.
This has had very limited success. The right border works fine, the the left changes opacity 4 pixels early. The top does not work (presumably because the mouse goes through the menubar and title bar) and the bottom changes way too early.
I thought of creating an empty container QWidget class and then place all the rest in there, but i felt that it would still not solve the big issue of the base widget not receiving the mousemoveevent if it has already been implemented in a child widget.
Please suggest any corrections/errors I have made in my method or any alternate methods to achieve this.
p.s. I doubt this matters, but I am working Qt Creator IDE, not Qt integration into VS2008 (it's the same classes anyways - different compiler though, mingw)
Installing event filters for each of your child widgets might do the trick. This will allow your main window to receive child events such as the ones from you group boxes. You can find example code here.
You may be interested in Event filters. QObject proves a way to intercept all events zipping around your application.
http://doc.trolltech.com/4.5/eventsandfilters.html#event-filters
If I understand what you are attempting to do, I would reimplement the widget's enterEvent() and leaveEvent(). The mouse enter event would trigger the fade-in and the leaveEvent would trigger the fade-out.
EDIT: After re-reading several times, I'm still not sure what you are trying to accomplish. Sorry if my suggestion doesn't help. :-)