My application on startup creates a sort-of "Splash Screen" with quick access buttons to Open, New, etc... This splash window is placed in the middle of the screen, and the application main window is placed behind it.
Here is the code to do it:
void MainWindowButtonDialog::showMe()
{
setModal(false);
setWindowFlags(
#ifdef Q_WS_WIN
Qt::SplashScreen |
#endif
Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
QDesktopWidget *d = QApplication::desktop();
QRect t = d->availableGeometry(this);
move(mapToGlobal(this->geometry().topLeft()).x() + t.center().x() - width() / 2,
t.center().y()- height() / 2);
show();
raise();
}
However I have some problems when the application runs on a two monitor setup. What happens is the following: the Splash screen regardless where the application starts, is created always on the first screen. So if the Window manager decided to open the application on the second screen the splash screen will be far far away on the middle of the first screen... Which is pretty ugly :(
Any idea how can this be fixed?
If you add the splash screen as a child to the MainWindow, it should have its initial coordinates set such that it displays right on top of its parent.
Try calling availableGeometry() not with a widget as parameter, but with a screen number. Something like
int screen = d->primaryScreen();
QRect t = d->availableGeometry(screen);
Related
I am working on some software to display semitransparent images in a row.
It worked fine using glut functions for resizing etc. but then i decided to move to Qt for a GUI.
all glut functions are stripped and i use a QGLWidget to render my OpenGL stuff.
When i open the program, everything works as wanted but as soon as i resize the mainwindow (and thus the QGLWidget is being resized) the contents of my openGL widget disappear or appear cluttered.
Strangely though the widget is completely grey (my glclearcolor) when there is no cluttered render of my scene content.
This is how it should look like (after starting the program):
http://abload.de/img/okw0qh0.png
And this is how it looks like after resizing the mainwindow:
http://abload.de/img/errort1peu.png
(sometimes it is just grey)
my resizeGL function looks like this and gets called properly:
void prosta3dwidget::resizeGL(int width, int height)
{
if (height == 0) return; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height; //get aspect ratio
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.5f, 100.0f);
glViewport(0, 0, width, height);
update(); //redraw content
glMatrixMode(GL_MODELVIEW);
}
Okay, noticing that this must have something to do with the depth buffer (gluPerspective manual notes that depth buffer precision is affected by zNear and zFar values: https://www.opengl.org/sdk/docs/man2/xhtml/gluPerspective.xml )
i disabled the GL_DEPTH_TEST (uncommented glEnable(GL_DEPTH_TEST)) and everything seems to work as desired. yet i dont fully understand why.
problem solved, new mistery achieved.
I have an Qt application that can be displayed either "normal" (a normal window that's neither maximized nor minimized), maximized or fullscreen. I achieve that by calling the functions QWidget::showNormal(), QWidget::showMaximized() and QWidget::showFullscreen().
Let me first of all explain what does work:
Going from normal to maximized and back works. Qt remembers the window size and position of the normal window and then restores that when exiting the maximized state.
Going from normal to fullscreen and back works. Also here the size and position of the normal window is maintained.
What doesn't work?
When going from normal to maximized, then to fullscreen, back to maximized and then back to normal that does not work as expected. The window will not be resized to the size that it had originally, before maximizing. Instead the window will be as large as the screen (basically as large as the OS allows the window to be).
If I now go to fullscreen again then in the right upper corner and on the bottom edge there are weird artifacts that seem to look like parts of an old Vista window frame with visual effects disabled.
Here is an image that should illustrate the process I just explained:
I tried to save the last size of the window using QWidget::saveGeometry() or just saving the size obtained by QWidget::size() when a window state change event occured, but there were always cases when it behaved strangely. Apart from that, that last step when going full screen again looks like a bug to me.
What do you think?
UPDATE:
If I resize the window manually (to an arbitrary size) before the last step (which is going to fullscreen again), the fullscreen works without problems.
Try doing showNormal() before showFullScreen() when the window is maximised and going to fullscreen.
when going to fullscreen
if (wasMaximized = isMaximized())
{
setVisible(false); // prevents window animation on showNormal() call
showNormal();
setVisible(true);
}
showFullScreen();
when returning from fullscreen
if (isFullScreen())
{
if (wasMaximized)
showMaximized();
else
showNormal();
}
The new Qt 5.6 release apparently fixed that bug.
This will helpful for you
void toggleFullScreen()
{
if (isFullScreen()) {
if (wasMaximized) {
showMaximized();
} else {
showNormal();
}
} else {
wasMaximized = isMaximized();
showFullScreen();
}
}
You need to declare wasMaximized yourself (It must be a global variable for you MainWindow class)
I have a bad English, so I can't explain all of this code. Sorry
I have a QGLWidget, which I want to resize to a given resolution (bigger than the containing window).
My intention is, that the window expands until the widget fits inside, but can't find a way to do it.
I tried several commands after resizing the QGLWidget to make it work without success.
I will list the results here:
do nothing else: The Widget overlaps the whole window. Eventually it will be resized to fit back into the smaller window
mainWindow.adjustSize(): The widget gets resized to (0, 0)
mainWindow.resize(mainWindow.sizeHint()): see above
mainWindow.resize(mainWindow.minimumSizeHint()): see above
I also read in this thread, that before doing the mainWindow resize I the event loop needs to be run to recalculate the new sizes, so I inserted QCoreApplication::processEvents to do so, without any visible effect.
So how do I resize the window via the widget?
Edit
The GLWidget is not the only widget of the window.
It is embedded in splitter together with a group box.
http://qt-project.org/doc/qt-4.8/qwidget.html#sizePolicy-prop
http://qt-project.org/doc/qt-4.8/qsizepolicy.html#Policy-enum
http://qt-project.org/doc/qt-4.8/qwidget.html#setFixedSize
So assuming that you have your QGLWidget nested inside your QMainWindow as the central widget, you need to set the size policy of your QGLWidget.
For example:
QGLWidget * glw; // in your header for QMainWindow
...
// In your constructor for QMainWindow
glw = new QGLWidget;
this->setCentralWidget(glw);
glw->setFixedSize(500, 500);
this->adjustSize();
Hope that helps.
I have an app that needed to be very similar to your requirements, so I'll post my solution here. An image covering the window which is freely expandable and shrinkable, and can be changed to the original size, and remain expandable / shrinkable after that.
I used a QLabel widget to display the image, but it should work with other widget types too. I created the widget with an initial size and the QSizePolicy::Ignored.
label->resize (w, h); // initial size
label->setSizePolicy (QSizePolicy::Ignored, QSizePolicy::Ignored);
The label widget was in a QVBoxLayout with a few buttons in the window, but this may work with other layout types too.
The window and image widget can be resized to the image's original size with this code:
label->resize (w, h); // change to original size
label->setMinimumSize (w, h); // prevent it from collapsing to zero immediately
window->adjustSize (); // resize the window
label->setMinimumSize (0, 0); // allow shrinking afterwards
I have made a gui which contains dockable windows.
If i click on maximize square on the docable window it comes out but does not occupies the full screen of my gui.
Example if i click syntax window it does not occupy full screen
Which property do i have to change to make docable window to occupy full screen ?
Please see the attached image.
You will need to create your own TitleBarWidget and set it with:
void QDockWidget::setTitleBarWidget ( QWidget * widget )
So you will be able to have as many buttons as you want and maximize it. Following code will help you with it:
QDockWidget *dockWidget = qobject_cast<QDockWidget*>(parentWidget());
dockWidget->showMaximized();
Edit: To keep the 2 existing buttons functionality:
The docking will be done with setFloating(bool ). So:
QDockWidget *dockWidget = qobject_cast<QDockWidget*>(parentWidget());
dockWidget->setFloating( !dockWidget->isFloating () );
For the close, parent close() method will work.
And, last edit, i promise ;).
You will need to have the title to show it on your titleWidget:
And it is in windowTitle : QString property of parent:
I am Using Qt 4.8
I am trying to bind mouse cursor to the center of my application.
If application is in fullscreen it works with following code
int middleX = QApplication::desktop()->width() >> 1;
int middleY = QApplication::desktop()->height() >> 1;
QPoint newMousePos;
newMousePos.setX(middleX);
newMousePos.setY(middleY);
QCursor::setPos(newMousePos);
and it works.
But how do I do this when application is not fullscreen mode?
I tried few codes from the web but I could not found them working. I understand that I need to get current geometry of window i.e. current window position w.r.t. monitor and width and height of window.
but what are the functions to be used for that?
Thanks in advance
All it takes is this:
QCursor::setPos(geometry().center());
This will put the cursor in the dead center of your application window main widget, agnostic of the size and position of the window on the screen.