스크롤은 화면에 표시되는 내용을 관리하는 Widget을 더 만들어 구성해야 합니다. 개념도는 다음과 같습니다.

프로젝트 이름에서 마우스 오른쪽 버튼을 누르면 다음과 같이 팝업메뉴가 화면에 표시됩니다.

Add New를 실행하면 다음과 같이 화면에 나타납니다.

C++ Class를 선택하고 Choose를 클릭하면 다음과 같이 화면에 나타납니다.

Class name에 MdiView라고 입력하고, Base class를 QWidget으로 설정하고 Include QWidget을 체크하면 다음과 같이 화면에 나타납니다.

Next를 클릭하여 나타난 대화상자에서 Finish를 클릭하면 다음과 같이 mdiview.cpp 파일과 mdiview.h 파일이 추가됩니다.

이제 MdiChild에서 화면에 표시하는 부분을 옮겨보도록 하겠습니다.

mdichild.h를 열어 ImageFile 변수 선언한 부분과 paintEvent 함수 선언부를 잘라내어 아래와 같이 mdiview.h 에 붙여 넣습니다.

mdichild.cpp 파일을 열어 paintEvent 함수를 찾아 가면 다음과 같이 화면에 나타납니다.

paintEvent 함수 부분을 모두 잘라내어 다음과 같이 mdiview.cpp에 옮기고 MdiChild를 MdiView로 수정합니다.

SetInfo라는 함수를 추가하여 다음과 같이 입력합니다.

mdiview.h를 열어 다음과 같이 SetInfo함수를 선언합니다.

mdichild.h를 열어 다음과 같이 MdiView로 변수를 선언하고 스크롤을 하기위해, MdiChild가 QWidget에서 상속받았던것을 QScrollArea에서 상속받도록 수정합니다.

mdichild.cpp를 열어 생성자 함수에 다음과 같이 MdiView를 생성하여 Widget으로 설정합니다.

m_pView = new MdiView();
setWidget(m_pView);
newFile 함수를 다음과 같이 MdiView의 SetInfo함수를 호출하도록 수정합니다.

m_pView->SetInfo(nWidth, nHeight, nColor);
지금까지 개념도에 표현한 MdiChild에서 화면에 표시하는 내용 부분에 해당하는 것을 잘라내어 MdiView로 옮기는 작업을 했습니다.
마지막으로 MdiView Widget의 크기를 설정한 그림크기로 설정만 하면 자동으로 스크롤 기능이 동작하게 됩니다.
mdiview.cpp 파일을 열어 SetInfo 함수에 resize함수를 호출하여 Widget의 크기를 설정하도록 합니다.

resize(nWidth, nHeight);
F5를 눌러 실행하여 새파일 아이콘을 누르면 다음과 같이 스크롤이 생성되어 나타납니다.

예제 프로그램
'QT' 카테고리의 다른 글
QT10. 안드로이드 개발환경 구축 (3) | 2024.09.13 |
---|---|
QT09. DLL 만들기 및 배포하기 (0) | 2022.04.10 |
설치 파일 만들기 (0) | 2022.03.12 |
QT08. 파일 및 인쇄 (0) | 2022.03.09 |
QT07. 메모리 및 마우스 처리 (0) | 2022.03.06 |