很全的Qt的标准对话框,包含QInputDialog、QColorDialog、QFontDialog、QMessageBox、QOpenFileDialog...

全部是由官网的C++版本,转换成PyQt5版本。

有些细节忽略了,因为实在不知怎么转换过来。捣鼓了一晚上,总算完成了,好累啊,不过很开心!

效果图:

完整代码:

 # -*- coding: utf-8 -*-
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import * import sys class DialogOptionsWidget(QWidget):
def __init__(self, parent=None):
super(DialogOptionsWidget,self).__init__(parent) def addCheckBox(self, text, value):
pass def addSpacer():
pass def value():
pass class StandardDialog(QDialog):
def __init__(self,parent=None):
super(StandardDialog,self).__init__(parent) self.setWindowTitle("Standard Dialog") frameStyle = QFrame.Sunken | QFrame.Panel mainLayout = QVBoxLayout(self)
toolbox = QToolBox()
mainLayout.addWidget(toolbox) self.errorMessageDialog = QErrorMessage(self) pushButton_integer = QPushButton("QInputDialog.get&Int()")
pushButton_double = QPushButton("QInputDialog.get&Double()")
pushButton_item = QPushButton("QInputDialog.getIte&m()")
pushButton_text = QPushButton("QInputDialog.get&Text()")
pushButton_multiLineText = QPushButton("QInputDialog.get&MultiLineText()")
pushButton_color = QPushButton("QColorDialog.get&Color()")
pushButton_font = QPushButton("QFontDialog.get&Font()")
pushButton_directory = QPushButton("QFileDialog.getE&xistingDirectory()")
pushButton_openFileName = QPushButton("QFileDialog.get&OpenFileName()")
pushButton_openFileNames = QPushButton("QFileDialog.&getOpenFileNames()")
pushButton_saveFileName = QPushButton("QFileDialog.get&SaveFileName()")
pushButton_critical = QPushButton("QMessageBox.critica&l()")
pushButton_information = QPushButton("QMessageBox.i&nformation()")
pushButton_question = QPushButton("QQMessageBox.&question()")
pushButton_warning = QPushButton("QMessageBox.&warning()")
pushButton_error = QPushButton("QErrorMessage.showM&essage()") self.label_integer = QLabel()
self.label_double = QLabel()
self.label_item = QLabel()
self.label_text = QLabel()
self.label_multiLineText = QLabel()
self.label_color = QLabel()
self.label_font = QLabel()
self.label_directory = QLabel()
self.label_openFileName = QLabel()
self.label_openFileNames = QLabel()
self.label_saveFileName = QLabel()
self.label_critical = QLabel()
self.label_information = QLabel()
self.label_question = QLabel()
self.label_warning = QLabel()
self.label_error = QLabel() self.label_integer.setFrameStyle(frameStyle)
self.label_double.setFrameStyle(frameStyle)
self.label_item.setFrameStyle(frameStyle)
self.label_text.setFrameStyle(frameStyle)
self.label_multiLineText.setFrameStyle(frameStyle)
self.label_color.setFrameStyle(frameStyle)
self.label_font.setFrameStyle(frameStyle)
self.label_directory.setFrameStyle(frameStyle)
self.label_openFileName.setFrameStyle(frameStyle)
self.label_openFileNames.setFrameStyle(frameStyle)
self.label_saveFileName.setFrameStyle(frameStyle)
self.label_critical.setFrameStyle(frameStyle)
self.label_information.setFrameStyle(frameStyle)
self.label_question.setFrameStyle(frameStyle)
self.label_warning.setFrameStyle(frameStyle)
self.label_error.setFrameStyle(frameStyle) page = QWidget()
layout = QGridLayout(page)
layout.setColumnStretch(1,1)
layout.setColumnMinimumWidth(1,250)
layout.addWidget(pushButton_integer,0,0)
layout.addWidget(self.label_integer,0,1)
layout.addWidget(pushButton_double,1,0)
layout.addWidget(self.label_double,1,1)
layout.addWidget(pushButton_item,2,0)
layout.addWidget(self.label_item,2,1)
layout.addWidget(pushButton_text,3,0)
layout.addWidget(self.label_text,3,1)
layout.addWidget(pushButton_multiLineText,4,0)
layout.addWidget(self.label_multiLineText,4,1)
layout.addItem(QSpacerItem(0,0,QSizePolicy.Ignored,QSizePolicy.MinimumExpanding),5,0)
toolbox.addItem(page, "Input Dialog") page = QWidget()
layout = QGridLayout(page)
layout.setColumnStretch(1,1)
#layout.setColumnMinimumWidth(1,250)
layout.addWidget(pushButton_color,0,0)
layout.addWidget(self.label_color,0,1)
colorDialogOptionsWidget = DialogOptionsWidget()
colorDialogOptionsWidget.addCheckBox("Do not use native dialog", QColorDialog.DontUseNativeDialog)
colorDialogOptionsWidget.addCheckBox("Show alpha channel" , QColorDialog.ShowAlphaChannel)
colorDialogOptionsWidget.addCheckBox("No buttons" , QColorDialog.NoButtons)
layout.addItem(QSpacerItem(0,0,QSizePolicy.Ignored,QSizePolicy.MinimumExpanding),1,0)
layout.addWidget(colorDialogOptionsWidget, 2, 0, 1 ,2)
toolbox.addItem(page, "Color Dialog") page = QWidget()
layout = QGridLayout(page)
layout.setColumnStretch(1, 1)
layout.addWidget(pushButton_font, 0, 0)
layout.addWidget(self.label_font, 0, 1)
fontDialogOptionsWidget = DialogOptionsWidget()
fontDialogOptionsWidget.addCheckBox("Do not use native dialog", QFontDialog.DontUseNativeDialog)
fontDialogOptionsWidget.addCheckBox("No buttons", QFontDialog.NoButtons)
layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 1, 0)
layout.addWidget(fontDialogOptionsWidget, 2, 0, 1 ,2)
toolbox.addItem(page, "Font Dialog") page = QWidget()
layout = QGridLayout(page)
layout.setColumnStretch(1, 1)
layout.addWidget(pushButton_directory, 0, 0)
layout.addWidget(self.label_directory, 0, 1)
layout.addWidget(pushButton_openFileName, 1, 0)
layout.addWidget(self.label_openFileName, 1, 1)
layout.addWidget(pushButton_openFileNames, 2, 0)
layout.addWidget(self.label_openFileNames, 2, 1)
layout.addWidget(pushButton_saveFileName, 3, 0)
layout.addWidget(self.label_saveFileName, 3, 1)
fileDialogOptionsWidget = DialogOptionsWidget()
fileDialogOptionsWidget.addCheckBox("Do not use native dialog", QFileDialog.DontUseNativeDialog)
fileDialogOptionsWidget.addCheckBox("Show directories only", QFileDialog.ShowDirsOnly)
fileDialogOptionsWidget.addCheckBox("Do not resolve symlinks", QFileDialog.DontResolveSymlinks)
fileDialogOptionsWidget.addCheckBox("Do not confirm overwrite", QFileDialog.DontConfirmOverwrite)
fileDialogOptionsWidget.addCheckBox("Do not use sheet", QFileDialog.DontUseSheet)
fileDialogOptionsWidget.addCheckBox("Readonly", QFileDialog.ReadOnly)
fileDialogOptionsWidget.addCheckBox("Hide name filter details", QFileDialog.HideNameFilterDetails)
layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 4, 0)
layout.addWidget(fileDialogOptionsWidget, 5, 0, 1 ,2)
toolbox.addItem(page, "File Dialogs") page = QWidget()
layout = QGridLayout(page)
layout.setColumnStretch(1, 1)
layout.addWidget(pushButton_critical, 0, 0)
layout.addWidget(self.label_critical, 0, 1)
layout.addWidget(pushButton_information, 1, 0)
layout.addWidget(self.label_information, 1, 1)
layout.addWidget(pushButton_question, 2, 0)
layout.addWidget(self.label_question, 2, 1)
layout.addWidget(pushButton_warning, 3, 0)
layout.addWidget(self.label_warning, 3, 1)
layout.addWidget(pushButton_error, 4, 0)
layout.addWidget(self.label_error, 4, 1)
layout.addItem(QSpacerItem(0, 0, QSizePolicy.Ignored, QSizePolicy.MinimumExpanding), 5, 0)
toolbox.addItem(page, "Message Boxes") pushButton_integer.clicked.connect(self.setInteger)
pushButton_double.clicked.connect(self.setDouble)
pushButton_item.clicked.connect(self.setItem)
pushButton_text.clicked.connect(self.setText)
pushButton_multiLineText.clicked.connect(self.setMultiLineText)
pushButton_color.clicked.connect(self.setColor)
pushButton_font.clicked.connect(self.setFont)
pushButton_directory.clicked.connect(self.setExistingDirectory)
pushButton_openFileName.clicked.connect(self.setOpenFileName)
pushButton_openFileNames.clicked.connect(self.setOpenFileNames)
pushButton_saveFileName.clicked.connect(self.setSaveFileName)
pushButton_critical.clicked.connect(self.criticalMessage)
pushButton_information.clicked.connect(self.informationMessage)
pushButton_question.clicked.connect(self.questionMessage)
pushButton_warning.clicked.connect(self.warningMessage)
pushButton_error.clicked.connect(self.errorMessage) #输入对话框 取整数
def setInteger(self):
intNum, ok = QInputDialog.getInt(self, "QInputDialog.getInteger()","Percentage:", 25, 0, 100, 1)
if ok:
self.label_integer.setText(str(intNum)) #输入对话框 取实数
def setDouble(self):
doubleNum, ok = QInputDialog.getDouble(self, "QInputDialog.getDouble()", "Amount:", 37.56, -10000, 10000, 2)
if ok:
self.label_double.setText(str(doubleNum)) #输入对话框 取列表项
def setItem(self):
items = ["Spring", "Summer", "Fall", "Winter"]
item, ok = QInputDialog.getItem(self, "QInputDialog.getItem()","Season:", items, 0, False)
if ok and item:
self.label_item.setText(item) #输入对话框 取文本
def setText(self):
text, ok = QInputDialog.getText(self, "QInputDialog.getText()", "User name:", QLineEdit.Normal, QDir.home().dirName())
if ok and text:
self.label_text.setText(text) #输入对话框 取多行文本
def setMultiLineText(self):
text, ok = QInputDialog.getMultiLineText(self, "QInputDialog.getMultiLineText()", "Address:", "John Doe\nFreedom Street")
if ok and text:
self.label_multiLineText.setText(text) #颜色对话框 取颜色
def setColor(self):
#options = QColorDialog.ColorDialogOptions(QFlag.QFlag(colorDialogOptionsWidget.value()))
color = QColorDialog.getColor(Qt.green, self, "Select Color") if color.isValid():
self.label_color.setText(color.name())
self.label_color.setPalette(QPalette(color))
self.label_color.setAutoFillBackground(True) #字体对话框 取字体
def setFont(self):
#options = QFontDialog.FontDialogOptions(QFlag(fontDialogOptionsWidget.value()))
#font, ok = QFontDialog.getFont(ok, QFont(self.label_font.text()), self, "Select Font",options)
font, ok = QFontDialog.getFont()
if ok:
self.label_font.setText(font.key())
self.label_font.setFont(font) #目录对话框 取目录
def setExistingDirectory(self):
#options = QFileDialog.Options(QFlag(fileDialogOptionsWidget->value()))
#options |= QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly
directory = QFileDialog.getExistingDirectory(self,
"QFileDialog.getExistingDirectory()",
self.label_directory.text())
if directory:
self.label_directory.setText(directory) #打开文件对话框 取文件名
def setOpenFileName(self):
#options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value()))
#selectedFilter
fileName, filetype = QFileDialog.getOpenFileName(self,
"QFileDialog.getOpenFileName()",
self.label_openFileName.text(),
"All Files (*);;Text Files (*.txt)")
if fileName:
self.label_openFileName.setText(fileName) #打开文件对话框 取一组文件名
def setOpenFileNames(self):
#options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value()))
#selectedFilter
openFilesPath = "D:/documents/pyMarksix/draw/"
files, ok = QFileDialog.getOpenFileNames(self,
"QFileDialog.getOpenFileNames()",
openFilesPath,
"All Files (*);;Text Files (*.txt)") if len(files):
self.label_openFileNames.setText(", ".join(files)) #保存文件对话框 取文件名
def setSaveFileName(self):
#options = QFileDialog.Options(QFlag(fileDialogOptionsWidget.value()))
#selectedFilter
fileName, ok = QFileDialog.getSaveFileName(self,
"QFileDialog.getSaveFileName()",
self.label_saveFileName.text(),
"All Files (*);;Text Files (*.txt)")
if fileName:
self.label_saveFileName.setText(fileName) def criticalMessage(self):
#reply = QMessageBox.StandardButton()
MESSAGE = "批评!"
reply = QMessageBox.critical(self,
"QMessageBox.critical()",
MESSAGE,
QMessageBox.Abort | QMessageBox.Retry | QMessageBox.Ignore)
if reply == QMessageBox.Abort:
self.label_critical.setText("Abort")
elif reply == QMessageBox.Retry:
self.label_critical.setText("Retry")
else:
self.label_critical.setText("Ignore") def informationMessage(self):
MESSAGE = "信息"
reply = QMessageBox.information(self, "QMessageBox.information()", MESSAGE)
if reply == QMessageBox.Ok:
self.label_information.setText("OK")
else:
self.label_information.setText("Escape") def questionMessage(self):
MESSAGE = "疑问"
reply = QMessageBox.question(self, "QMessageBox.question()",
MESSAGE,
QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel)
if reply == QMessageBox.Yes:
self.label_question.setText("Yes")
elif reply == QMessageBox.No:
self.label_question.setText("No")
else:
self.label_question.setText("Cancel") def warningMessage(self):
MESSAGE = "警告文本"
msgBox = QMessageBox(QMessageBox.Warning,
"QMessageBox.warning()",
MESSAGE,
QMessageBox.Retry | QMessageBox.Discard | QMessageBox.Cancel,
self)
msgBox.setDetailedText("详细信息。。。")
#msgBox.addButton("Save &Again", QMessageBox.AcceptRole)
#msgBox.addButton("&Continue", QMessageBox.RejectRole)
if msgBox.exec() == QMessageBox.AcceptRole:
self.label_warning.setText("Retry")
else:
self.label_warning.setText("Abort") def errorMessage(self):
self.errorMessageDialog.showMessage(
"This dialog shows and remembers error messages. "
"If the checkbox is checked (as it is by default), "
"the shown message will be shown again, "
"but if the user unchecks the box the message "
"will not appear again if QErrorMessage.showMessage() "
"is called with the same message.")
self.label_error.setText("If the box is unchecked, the message "
"won't appear again.") app=QApplication(sys.argv)
form=StandardDialog()
form.show()
app.exec_()

PyQt5标准对话框的更多相关文章

  1. 如何修改Qt标准对话框的文字(例如,英文改成中文)

    此篇文章参考qtcn论坛整理而成,因为文字和图片是本人亲自组织,所以仍算原创. http://www.qtcn.org/bbs/read-htm-tid-30650.html http://blog. ...

  2. Qt中的标准对话框之QMessageBox

    1. Qt标准对话框 Qt为开发者提供了一些可复用的对话框类型 Qt提供的可复用对话框全部继承自QDialog类 Qt中的对话框的使用方式和QDialog完全一致 2. 标准对话框的使用步骤 ①定义对 ...

  3. Qt 学习之路 2(15):标准对话框 QMessageBox

    Qt 学习之路 2(15):标准对话框 QMessageBox  豆子  2012年9月18日  Qt 学习之路 2  40条评论 所谓标准对话框,是 Qt 内置的一系列对话框,用于简化开发.事实上, ...

  4. 《转》PyQt4 精彩实例分析* 实例2 标准对话框的使用

    和大多数操作系统一样,Windows及Linux都提供了一系列的标准对话框,如文件选择,字体选择,颜色选择等,这些标准对话框为应用程序提供了一致的观感.Qt对这些标准对话框都定义了相关的类.这些类让使 ...

  5. QT+ 使用标准对话框+关于对话框+问题对话框+文件对话框

    #include "mainwindow.h" #include <QMenuBar> #include <QMenu> #include <QAct ...

  6. 【Qt开发】Qt标准对话框之QMessageBox

    好久没有更新博客,主要是公司里面还在验收一些东西,所以没有及时更新.而且也在写一个基于Qt的画图程序,基本上类似于PS的东西,主要用到的是Qt Graphics View Framework.好了,现 ...

  7. 跟我一起学QT_QT标准对话框_字体选择框

    标准对话框 QT的标准对话框分为以下几种 颜色对话框 文件对话框 字体对话框 输入对话框 消息对话框 进度对话框 错误信息对话框 向导对话框 字体选择框 字体选择框位于 QFontDialog 类中 ...

  8. 跟我一起学QT_QT标准对话框_文件对话框

    标准对话框 QT的标准对话框分为以下几种 颜色对话框 文件对话框 字体对话框 输入对话框 消息对话框 进度对话框 错误信息对话框 向导对话框 文件对话框 QT中的文件对话框QFileDialog类提供 ...

  9. 跟我一起学QT_QT标准对话框_颜色选择框

    标准对话框 QT的标准对话框分为以下几种 颜色对话框 文件对话框 字体对话框 输入对话框 消息对话框 进度对话框 错误信息对话框 向导对话框 颜色对话框 首先学习的是颜色对话框颜色对话框类QColor ...

随机推荐

  1. iOS开发-为UITableViewCell添加横线

    在开发过程中经常会遇到设计稿中Cell分割线样式和系统自带的样式差别很大,如何实现这里做下总结,主要包括如下两步: 1. 取消TableView默认的分割线样式 _tableView.separato ...

  2. 关于 Handler 与 opener

    我们可以使用 urllib.request.Request() 构造请求对象,但是对于一些更高级的操作,比如 Cookies 处理.代理设置 .身份验证等等,Request() 是处理不了的这时就需要 ...

  3. 微信公众号access_token的获取与存储

    如果是一个用户触发,那么只要将access_token放在数据库,文件,nosql就行,取时判断时间是否过期,过期重新去微信获取再放入. 如果是很多用户,多进程并发,特别是分布式那种架构呢?进程A判断 ...

  4. c++学习笔记—c++对txt文件的读取与写入

    一.文件的输入输出 头文件fstream定义了三个类型支持文件IO:ifstream从给定文件读取数据.ofstream向一个给定文件写入数据.fstream读写给定数据.这些类型与cin和cout的 ...

  5. iOS 事件的产生、传递、响应

    一.事件的产生和传递 1.1.事件的产生 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中为什么是队列而不是栈?因为队列的特定是先进先出,先产生的事件先处理才符合常 ...

  6. 杨辉三角(Pascal Triangle)的几种C语言实现及其复杂度分析

    说明 本文给出杨辉三角的几种C语言实现,并简要分析典型方法的复杂度. 本文假定读者具备二项式定理.排列组合.求和等方面的数学知识. 一  基本概念 杨辉三角,又称贾宪三角.帕斯卡三角,是二项式系数在三 ...

  7. docker 快速搭建Nexus3

    1.拉取镜像 docker pull sonatype/nexus3 2.启动容器 : -p : -p : -v /mnt/gv0/nexus-data:/nexus-data sonatype/ne ...

  8. WP8.1学习系列(第二十一章)——本地应用数据

    了解如何存储和检索本地应用数据存储中的设置和文件. 路线图: 本主题与其他主题有何关联?请参阅: 使用 C# 或 Visual Basic 的 Windows 运行时应用的路线图 使用 C++ 的 W ...

  9. 面试题:应用中很多jar包,比如spring、mybatis、redis等等,各自用的日志系统各异,怎么用slf4j统一输出?(上)

    一.问题概述 如题所说,后端应用(非spring boot项目)通常用到了很多jar包,比如spring系列.mybatis.hibernate.各类连接数据库的客户端的jar包.可能这个jar包用的 ...

  10. 基于spring-cloud的微服务(1) 服务注册中心eureka

    eureka是Netflix提供的服务注册中心组建,springcloud将其做了封装,作为自己的微服务架构中的一个注册中心组建 下面的例子在IDEA中启动一个eureka的实例,然后提供一个prov ...