Window position with respect to screen and width and height of window in Qt - qt

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.

Related

QML show window on specified screen

I have hybrid QML/QWidget application and I want to show QML window on the specified screen. Here's the code defining a screen number
int activeScreenIndex = QApplication::desktop()->screenNumber(QApplication::activeWindow());
Now I want to show QML component Window on the screen I've defined this way. Another problem is that I create QML windows unisng Instantiator.
Window::screen was introduced in Qt 5.9.
Window {
screen: Qt.application.screens[activeScreenIndex]
}

Correctly Placing a QT Splash Screen on 2 screens

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

Dockable window -- not maximise

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:

Is there a way to center a TideSDK window in the screen?

When working with TideSDK windows, you get to control their position within the screen. However, I can't find any way to retrieve the screen's size nor a way to center the window.
Is there a way to accomplish that?
You can access the user's screen size with window.screen.width and window.screen.height. You can use those to center a window:
var app_window = Ti.UI.currentWindow;
app_window.setX((window.screen.width-app_window.getWidth())/2);
app_window.setY((window.screen.height-app_window.getHeight())/2);
Note that the window object here is not an instance of Ti.UI.UserWindow but the usual browser window object.

QT Layout - initial directions

I am new to QT. I'm trying to understand the layout mechanism by trying to implement this small window seen below. It has the following elements under the QWidget that's the main window:
One big QWidget that stretches on all the client area.
Two QWidget containers on the top of the window. Both should have the same height, but the right one stretches horizontally, as the window grows/shrinks.
one button container widget on the top right, with fixed height and width
Large QWidget container filling the rest of the client area, that should resize as the window resizes.
The parent window itself is resizeable.
I'm looking for hints as to what layout I should use. How do I achieve this programatically? define what stretches automatically, what stays with a fix size? and how the proportions are kept where they need to be kept.
I'd appreciate any pointer you may have.
The easiest, and IMHO best, way to accomplish this is via the QHBoxLayout and QVBoxLayouts. You can do this via the designer in QtCreator, but I find it doesn't work perfectly if you need to adapt things over time. If it's a static set of widgets, I do suggest designing it using the QtCreator designer as it'll greatly simplify your life.
If you're going to do it programatically, the main window should be set to use a QVBoxLayout and then two sub-QVBoxLayout's after that, where the bottom one is configured to take any space it can get. Then in the top QVBoxLayout, add a QHBoxLayout with your two upper components.
to set a widget to fixed size in code you call setFixedSize( int h, int w ) on the widget. To do it in Designer click on the widget and look in the property editor in the QWidget section. open the sizePolicy thingy and set horizontal and/or vertical to fixed. Then open Geometry and set the width and Height.
To make them stretch at different ratios in code you use a separate argument when using a box layout. eg layout->addWidget( button1, 1 ); layout->addWidget (button2, 2); this would cause button2 to expand at twice the rate of button1. To do this in designer, open the sizePolicy property of the widgets and set the HorizontalStrech and/or VerticalSretch. Note that the size policy needs to not be Fixed in this case for the direction you want to set the stretch on. Also it will never let a widget shrink below its minimum size (it would rather mess up the ratio than shrink something too small).

Resources