QLabel通常用於顯示文本,但也可以顯示圖像。 QScrollArea提供了另一個小部件的滾動視圖。如果子窗口小部件超出框架的大小,則QScrollArea會自動提供滾動條。
下面這個例子演示了Qlabel如何擴展其內容的能力QLabel::scaledContents,QScrollArea自動調整內容大小的功能QScrollArea::widgetResizable,可用於實現縮放和縮放功能。此外,該示例顯示如何使用QPainter打印圖像。
使用Image Viewer應用程序,用戶可以查看自己選擇的圖像。 “文件”菜單爲用戶提供了以下功能:
Open… - Open an image file
Print… - Print an image
Exit - Exit the application
加載圖像後,我們可以之下執行下列的功能:
Zoom In - Scale the image up by 25%
Zoom Out - Scale the image down by 25%
Normal Size - Show the image at its original size
Fit to Window - Stretch the image to occupy the entire window
ImageViewer類繼承自QMainWindow。除了重新實現構造函數,並創建多個私有槽以方便菜單項。還創建了四個私有函數。
在構建ImageViewer小部件時,使用createActions()和createMenus()。當加載新圖像時,或當“自適應窗口”選項被切換時,使用updateActions()函數來更新菜單選項。