Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, August 2007
How to Center an application ?


Message 1 in thread

Hello,

How do I need to center the application when it start up ?

I am working on Suse Linux 10.2 with Qt 4.3.

Thanks,
-Joshua

--
 [ signature omitted ] 

Message 2 in thread

Joshua Robinson wrote:
> How do I need to center the application when it start up ?

Look at QDesktopWidget. It'll tell you what size screen you are working 
with. From that you can center your application's main window.

--Dave

--
 [ signature omitted ] 

Message 3 in thread

Its working thank you all.

Dave Smith wrote:
> Joshua Robinson wrote:
>> How do I need to center the application when it start up ?
>
> Look at QDesktopWidget. It'll tell you what size screen you are 
> working with. From that you can center your application's main window.
>
> --Dave
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

--
 [ signature omitted ] 

Message 4 in thread

Joshua Robinson wrote:
> Hello,
> 
> How do I need to center the application when it start up ?

See QDesktopWidget for getting the desktop geometry then QWidget docs 
and finally Window Geometry Documentation...

- get the desktop size and your application window's size
- move the window on the X axis to (desktop.width() - window.width()) / 
2 and on the Y axis to (desktop.height() - window.height()) / 2)

--
 [ signature omitted ] 

Message 5 in thread

> -----Original Message-----
> From: Joshua Robinson [mailto:shooki.robinson@xxxxxxxxx]
> Sent: Tuesday, August 14, 2007 8:19 AM
> To: qt
> Subject: How to Center an application ?
> 
> Hello,
> 
> How do I need to center the application when it start up ?
> 
> I am working on Suse Linux 10.2 with Qt 4.3.
> 
> Thanks,
> -Joshua
> 
One thing to be aware of...  You will be getting the geometry on each
screen (for a multi-screen desktop) its easy to get the screen of the
application.. However, if you want to center across everything, you will
need make multiple calls

Scott

--
 [ signature omitted ] 

Message 6 in thread

Here's a bit of code I wrote a few months ago :

void centerWidgetOnScreen (QWidget * widget) {
     QRect rect = QApplication::desktop()->availableGeometry();

     widget->move(rect.center() - widget->rect().center());
}

Just use centerWidgetOnScreen(this) in your window constructor.

It should do the work nicely

--
 [ signature omitted ] 

Message 7 in thread

On 08-28-2007 5:31 AM, "Clément BOURGEOIS" wrote:

> Here's a bit of code I wrote a few months ago :
> 
> void centerWidgetOnScreen (QWidget * widget) {
>      QRect rect = QApplication::desktop()->availableGeometry();

You really should use:

   QRect rect = QApplication::desktop()->availableGeometry(widget);

So that you get the rectangle of the screen the widget is on, not the whole
desktop (which might be several screens). Otherwise you risk having part
(even possibly all) of your widget outside a viewable area.

>      widget->move(rect.center() - widget->rect().center());
> }
> 
> Just use centerWidgetOnScreen(this) in your window constructor.
> 
> It should do the work nicely


Keith
**Please do not reply to me, reply to the list.**


--
 [ signature omitted ] 

Message 8 in thread

This (below) is quite a nice way to do it, because the app starts out
maximised, but minimises to a sensible size, centred.

You might want to replace the hardcoded values with something more
sensible...

XCORRECTION and YCORRECTION are a bit of a hack -- availableGeometry
doesn't quite get the width and height right (or am I doing something
wrong?)

const int MAXWIDTH = 1000, MAXHEIGHT = 800, XMARGIN = 100, XCORRECTION =
5, 
   YMARGIN = 100, YCORRECTION = 30;
const int desktopWidth =
QApplication::desktop()->availableGeometry().width() - XCORRECTION;
const int desktopHeight =
QApplication::desktop()->availableGeometry().height() - YCORRECTION;
const int widgetWidth = QMIN(MAXWIDTH, desktopWidth - XMARGIN * 2);
const int widgetHeight = QMIN(MAXHEIGHT, desktopHeight - YMARGIN * 2);
setGeometry((desktopWidth - widgetWidth) / 2, (desktopHeight -
widgetHeight) / 2, 
   widgetWidth, widgetHeight);
setWindowState(Qt::WindowMaximized);

For multiple windows, you'll need to tweak this a bit.

Sam Dutton


 





SAM DUTTON
SENIOR SITE DEVELOPER

200 GRAY'S INN ROAD
LONDON
WC1X 8XZ
UNITED KINGDOM
T +44 (0)20 7430 4496
F 
E SAM.DUTTON@xxxxxxxxx
WWW.ITN.CO.UK

P  Please consider the environment. Do you really need to print this email?
-----Original Message-----

From: Joshua Robinson [mailto:shooki.robinson@xxxxxxxxx] 
Sent: Tuesday 14 August 2007 16:19
To: qt
Subject: How to Center an application ?

Hello,

How do I need to center the application when it start up ?

I am working on Suse Linux 10.2 with Qt 4.3.

Thanks,
-Joshua

--
 [ signature omitted ]