2012-07-27 2 views
3

신청서에 두 개의 창과 두 개의 클래스가 있다고 가정 해 봅시다 : class MainWindow: public QMainWindowclass SomeDialog: public QWidget.QWidget을 올바르게 정리하는 방법/일련의 창을 관리하는 방법?

내 메인 창에 버튼이 있습니다. 클릭하면 두 번째 창을 표시해야합니다. 나는 이렇게한다 :

SomeDialog * dlg = new SomeDialog(); 
dlg.show(); 

이제 사용자는 창에서 무엇인가를 수행하고 그것을 닫는다. 이 시점에서 나는 그 창에서 어떤 데이터를 얻고 싶습니다. 그러면 저는 가정합니다. delete dlg입니다. 그러나 그 창문이 닫히는 사건을 어떻게 잡을 수 있습니까?

아니면 메모리 누수가없는 다른 방법이 있습니까? 어쩌면 시작시 각 창에 대한 인스턴스를 만드는 것이 더 좋을 것입니다. 그런 다음 Show()/Hide()을 입력하면됩니까?

어떻게 이러한 사례를 관리합니까?

답변

1

나는 당신이 Qt는 :: WA_DeleteOnClose 창 플래그를 찾고 있습니다 생각 : http://doc.qt.io/archives/qt-4.7/qt.html#WidgetAttribute-enum

QDialog *dialog = new QDialog(parent); 
dialog->setAttribute(Qt::WA_DeleteOnClose) 
// set content, do whatever... 
dialog->open(); 
// safely forget about it, it will be destroyed either when parent is gone or when the user closes it. 
+0

ctor는 위젯 속성이 아닌 창 플래그를 사용합니다. 올바른 호출은 dialog-> setAttribute (Qt :: WA_DeleteOnClose) –

+0

@FrankOsterfeld입니다. 내가 입력하지 않았을 때 나는 컴파일 할 수 없었다. 결코 체크하지 않았다. – MrFox

+0

@FrankOsterfeld 대화 상자가 닫히는 것을 의미합니까? 단추가 있고 슬롯을 close()라고 말하면 어떻게됩니까? 대화 상자가 메모리에서 삭제됩니까? –

1

QWidget에서 서브 클래스 화해

virtual void QWidget::closeEvent (QCloseEvent * event)

http://doc.qt.io/qt-4.8/qwidget.html#closeEvent

은 또한 당신이 보여주고 싶은 위젯처럼 보이는 재 구현은 대화이다. 따라서 QDialog 또는 하위 클래스를 사용하는 것이 좋습니다. show()/exec()hide() 대신 동적으로 대화 당신이 그것을 보여주고 싶은 모든 시간을 만드는 사용하는 것이 좋습니다

void accepted() 
void finished (int result) 
void rejected() 
3

: QDialog 당신이 연결할 수 있습니다 유용한 신호가 있습니다. QWidget 대신 QDialog을 사용하십시오. 메인 윈도우의 생성자에서

그것을 만들고 단순히 보여 버튼의 clicked() 이벤트에 연결된 슬롯에서 그것을

MainWindow::MainWindow() 
{ 
    // myDialog is class member. No need to delete it in the destructor 
    // since Qt will handle its deletion when its parent (MainWindow) 
    // gets destroyed. 
    myDialog = new SomeDialog(this); 
    myDialog->hide(); 
    // connect the accepted signal with a slot that will update values in main window 
    // when the user presses the Ok button of the dialog 
    connect (myDialog, SIGNAL(accepted()), this, SLOT(myDialogAccepted())); 

    // remaining constructor code 
} 

을 숨기고, 필요에 전달하면 대화

void myClickedSlot() 
{ 
    myDialog->setData(data); 
    myDialog->show(); 
} 

void myDialogAccepted() 
{ 
    // Get values from the dialog when it closes 
} 
에 일부 데이터
관련 문제