private slots: void setupWidget();
this->setupWidget();
void test::setupWidget(){ QRect screenRect = Application::desktop()->screenGeometry(QApplication::desktop()->primaryScreen()); QRect windowRect(0, 0, 400, 232); //параметры виджета if (screenRect.width() < 400) windowRect.setWidth(screenRect.width()); if (screenRect.height() < 232) windowRect.setHeight(screenRect.height()); windowRect.moveCenter(screenRect.center()); this->setGeometry(windowRect); this->setMinimumSize(80, 60);}
void MyDialog::showEvent( QShowEvent* event){ int x,y; x = QApplication::desktop()->screen( 0)->width()/2 - width()/2; y = QApplication::desktop()->screen( 0)->height()/2 - height()/2; move( x, y); QDialog::showEvent( event);}
/*static*/ const QRect OurApplication::getScreenSize( const QPoint& pnt/*=QPoint()*/ ){ if( pnt.isNull() ) { return qApp->desktop()->availableGeometry(); //-- default system' screen } return qApp->desktop()->availableGeometry( pnt ); //--"current" (under cursor) screen}/*static*/ void OurApplication::makeWidgetCentered( QWidget *pWidget, QWidget *pBaseWidget/*=0*/ ){ if( !pWidget ) { return; } QRect baseRect; if( !pBaseWidget ) { baseRect = getScreenSize( QCursor::pos() ); } else { baseRect = pBaseWidget->rect(); baseRect.setTopLeft( pBaseWidget->mapToGlobal( baseRect.topLeft() ) ); baseRect.setBottomRight( pBaseWidget->mapToGlobal( baseRect.bottomRight() ) ); } QRect widgetGeometry = pWidget->geometry(); widgetGeometry.moveCenter( baseRect.center() ); pWidget->setGeometry( widgetGeometry );}
void PlaceWndInTheDesktopCentre(){ QDesktopWidget *desktop = QApplication::desktop(); int screenWidth, width; int screenHeight, height; int x, y; QSize windowSize; screenWidth = desktop->width(); // get width of screen screenHeight = desktop->height(); // get height of screen windowSize = size(); // size of our application window width = windowSize.width(); height = windowSize.height(); // little computations x = (screenWidth - width) / 2; y = (screenHeight - height) / 2; y -= 20; // move window to desired coordinates move ( x, y );}