2017-02-19 3 views
1

나는이 예제 코드가 있습니다QT에서 QDialog를 중앙에 배치하는 방법?

QDialog *dialog = new QDialog(this); 
QPoint dialogPos = dialog->mapToGlobal(dialog->pos()); 
QPoint thisPos = mapToGlobal(this->pos()); 
dialog->exec(); 

을하지만 대화는 자신의 부모를 중심으로하지 않습니다. 미리 감사드립니다.

UPDATE :

내가 MainWindow를에 생성자에서 대화 상자를 호출하고 있습니다 :

MainWindow::MainWindow(QWidget *parent) 
    : QMainWindow(parent) 
{ 

    this->panelInferior = new WidgetTabsInferior; 
    this->acciones = new Acciones(this); 

    crearAcciones(); 
    crearBarraMenu(); 
    crearToolbar(); 
    crearTabsEditor(); 
    crearArbolDir(); 
    crearDockWindows(); 
    crearStatusBar(); 

    setWindowIcon(QIcon(":imgs/logo.png")); 

    connect(this->pestanasEditor , SIGNAL(currentChanged(int)),this,SLOT(cambioTab(int))); 

    this->dialogo = new AcercaDe(this); 
    this->dialogo->move(x() + (width() - dialogo->width())/2, 
       y() + (height() - dialogo->height())/2); 
    this->dialogo->show(); 
    this->dialogo->raise(); 
    this->dialogo->activateWindow(); 

} 

하지만 수는 다음과 같습니다

enter image description here

+0

http://www.qtcentre.org/threads/43802-Centering-child-window-in-parent http://stackoverflow.com/questions/18385916/how-to-keep-a-qwidget-or -qdialog-centered-to-the-parent-widget –

+0

[부모 위젯에 QWidget (또는 QDialog)를 중앙에서 유지하는 방법?] (http://stackoverflow.com/questions/18385916/how-to- keep-a-qwidget-or-qdialog-centered-the-parent-widget) –

답변

2

내가

inline void CenterWidgets(QWidget *widget, QWidget *host = 0) { 
    if (!host) 
     host = widget->parentWidget(); 

    if (host) { 
     auto hostRect = host->geometry(); 
     widget->move(hostRect.center() - widget->rect().center()); 
    } 
    else { 
     QRect screenGeometry = QApplication::desktop()->screenGeometry(); 
     int x = (screenGeometry.width() - widget->width())/2; 
     int y = (screenGeometry.height() - widget->height())/2; 
     widget->move(x, y); 
    } 
} 

github에서이 코드가 나는 그것이 QT4 버그 생각이

0

당신은 인 QDialog의 형상을 변경해야 :

dialog->move(x() + (width() - dialog->width())/2, 
      y() + (height() - dialog->height())/2); 

move() 기능 이동은 부모를 존중하므로 전역으로 매핑 할 필요가 없습니다.

생성자에서 부모의 위치와 크기가 아직 설정되지 않았습니다. 별도의 방법 대화 상자를 실행하려고 또는 생성자에 필요한 경우, 그것은 이벤트 루프의 다음 반복에 표시됩니다

QTimer::singleShot(0, [=]() { 
    // ... your dialog code 
}); 

처럼 뭔가를 시도 할 수 있습니다.

+0

고맙습니다. 제 질문을 업데이트했습니다. 대화 상자가 제대로 작동하지 않습니다. :( –

-1

을 도움이되기를 바랍니다. 우분투에서 Qt4를 사용했고 부모 위젯 센터를 존중하지 않습니다.

그러나 Qt5를 사용하면 정상적으로 작동하는 것 같습니다.

또한 move()을 사용하여 사용자의 위치에 도달 할 수 있습니다.

관련 문제