日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

QT5实践:菜单窗口实现图像打开浏览、存盘

發布時間:2025/3/21 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QT5实践:菜单窗口实现图像打开浏览、存盘 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、從本代碼中學到什么?

以下代碼實現功能有:

  • 1 菜單畫出
  • 2 菜單事件響應
  • 3 文件對話框
  • 4 顯示圖片
  • 5 圖片放大縮小

二、 實現代碼

from PyQt5.QtCore import QDir, Qt from PyQt5.QtGui import QImage, QPainter, QPalette, QPixmap from PyQt5.QtWidgets import (QAction, QApplication, QFileDialog, QLabel,QMainWindow, QMenu, QMessageBox, QScrollArea, QSizePolicy) from PyQt5.QtPrintSupport import QPrintDialog, QPrinterclass ImageViewer(QMainWindow):def __init__(self):super(ImageViewer, self).__init__()self.printer = QPrinter()self.scaleFactor = 0.0self.imageLabel = QLabel()self.imageLabel.setBackgroundRole(QPalette.Base)self.imageLabel.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)self.imageLabel.setScaledContents(True)self.scrollArea = QScrollArea()self.scrollArea.setBackgroundRole(QPalette.Dark)self.scrollArea.setWidget(self.imageLabel)self.setCentralWidget(self.scrollArea)self.createActions()self.createMenus()self.setWindowTitle("Image Viewer")self.resize(500, 700)def open(self):fileName, _ = QFileDialog.getOpenFileName(self, "Open File",QDir.currentPath())if fileName:image = QImage(fileName)print("inmage formate ",image.format()==QImage.Format_RGB32 )if image.isNull():QMessageBox.information(self, "Image Viewer","Cannot load %s." % fileName)returnself.imageLabel.setPixmap(QPixmap.fromImage(image))self.scaleFactor = 1.0self.printAct.setEnabled(True)self.fitToWindowAct.setEnabled(True)self.updateActions()if not self.fitToWindowAct.isChecked():self.imageLabel.adjustSize()def print_(self):dialog = QPrintDialog(self.printer, self)if dialog.exec_():painter = QPainter(self.printer)rect = painter.viewport()size = self.imageLabel.pixmap().size()size.scale(rect.size(), Qt.KeepAspectRatio)painter.setViewport(rect.x(), rect.y(), size.width(), size.height())painter.setWindow(self.imageLabel.pixmap().rect())painter.drawPixmap(0, 0, self.imageLabel.pixmap())def zoomIn(self):self.scaleImage(1.25)def zoomOut(self):self.scaleImage(0.8)def normalSize(self):self.imageLabel.adjustSize()self.scaleFactor = 1.0def fitToWindow(self):fitToWindow = self.fitToWindowAct.isChecked()self.scrollArea.setWidgetResizable(fitToWindow)if not fitToWindow:self.normalSize()self.updateActions()def about(self):QMessageBox.about(self, "About Image Viewer","<p>The <b>Image Viewer</b> example shows how to combine ""QLabel and QScrollArea to display an image. QLabel is ""typically used for displaying text, but it can also display ""an image. QScrollArea provides a scrolling view around ""another widget. If the child widget exceeds the size of the ""frame, QScrollArea automatically provides scroll bars.</p>""<p>The example demonstrates how QLabel's ability to scale ""its contents (QLabel.scaledContents), and QScrollArea's ""ability to automatically resize its contents ""(QScrollArea.widgetResizable), can be used to implement ""zooming and scaling features.</p>""<p>In addition the example shows how to use QPainter to ""print an image.</p>")def createActions(self):self.openAct = QAction("&Open...", self, shortcut="Ctrl+O",triggered=self.open)self.printAct = QAction("&Print...", self, shortcut="Ctrl+P",enabled=False, triggered=self.print_)self.exitAct = QAction("E&xit", self, shortcut="Ctrl+Q",triggered=self.close)self.zoomInAct = QAction("Zoom &In (25%)", self, shortcut="Ctrl++",enabled=False, triggered=self.zoomIn)self.zoomOutAct = QAction("Zoom &Out (25%)", self, shortcut="Ctrl+-",enabled=False, triggered=self.zoomOut)self.normalSizeAct = QAction("&Normal Size", self, shortcut="Ctrl+S",enabled=False, triggered=self.normalSize)self.fitToWindowAct = QAction("&Fit to Window", self, enabled=False,checkable=True, shortcut="Ctrl+F", triggered=self.fitToWindow)self.aboutAct = QAction("&About", self, triggered=self.about)self.aboutQtAct = QAction("About &Qt", self,triggered=QApplication.instance().aboutQt)def createMenus(self):self.fileMenu = QMenu("&File", self)self.fileMenu.addAction(self.openAct)self.fileMenu.addAction(self.printAct)self.fileMenu.addSeparator()self.fileMenu.addAction(self.exitAct)self.viewMenu = QMenu("&View", self)self.viewMenu.addAction(self.zoomInAct)self.viewMenu.addAction(self.zoomOutAct)self.viewMenu.addAction(self.normalSizeAct)self.viewMenu.addSeparator()self.viewMenu.addAction(self.fitToWindowAct)self.helpMenu = QMenu("&Help", self)self.helpMenu.addAction(self.aboutAct)self.helpMenu.addAction(self.aboutQtAct)self.menuBar().addMenu(self.fileMenu)self.menuBar().addMenu(self.viewMenu)self.menuBar().addMenu(self.helpMenu)def updateActions(self):self.zoomInAct.setEnabled(not self.fitToWindowAct.isChecked())self.zoomOutAct.setEnabled(not self.fitToWindowAct.isChecked())self.normalSizeAct.setEnabled(not self.fitToWindowAct.isChecked())def scaleImage(self, factor):self.scaleFactor *= factorself.imageLabel.resize(self.scaleFactor * self.imageLabel.pixmap().size())self.adjustScrollBar(self.scrollArea.horizontalScrollBar(), factor)self.adjustScrollBar(self.scrollArea.verticalScrollBar(), factor)self.zoomInAct.setEnabled(self.scaleFactor < 3.0)self.zoomOutAct.setEnabled(self.scaleFactor > 0.333)def adjustScrollBar(self, scrollBar, factor):scrollBar.setValue(int(factor * scrollBar.value()+ ((factor - 1) * scrollBar.pageStep()/2)))if __name__ == '__main__':import sysapp = QApplication(sys.argv)imageViewer = ImageViewer()imageViewer.show()sys.exit(app.exec_())

三、 關于菜單

要想用窗口菜單,需要以下幾個步驟:

1)QMainWindow窗口需要增加菜單:本例的.createMenus()函數

? ? ? ? ? ? ?self.fileMenu = QMenu("&File", self)----定義菜單標題

? ? ? ? ? ? ?self.viewMenu = QMenu("&View", self)-------定義菜單標題

? ? ? ? ? ? ?self.helpMenu = QMenu("&Help", self)

但是,增加菜單后,該菜單能顯示,不能觸發,因此需要增加響應。

2) 追加響應代理

????????QMainWindow.createActions()

注意,追加響應就是給菜單指定觸發程序。但不是菜單獨有,而是任意需要觸發的地方都可以用。

3)追加菜單的響應按鈕:

? ? ? ? self.fileMenu.addAction(self.openAct)

????????注意,此步驟self.openAct是一個按鈕,但是一個抽象的按鈕。

4)設定抽象的按鈕的觸發函數

? ? ? ?self.openAct = QAction( "&Open...", self, shortcut="Ctrl+O",triggered=self.open )

? ? ? 這里QAction是個抽象按鈕,其文字是"&Open...",快捷鍵:"Ctrl+O";響應函數是open。

?至此整個菜單構造過程完成,但如何開發圖標菜單,這里暫不介紹。

具體菜單詳細解釋,見網文:(1條消息) QT5實踐:如何應用窗口菜單_無水先生的博客-CSDN博客?

總結

以上是生活随笔為你收集整理的QT5实践:菜单窗口实现图像打开浏览、存盘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。