Python Qt GUI快速编程-第五章的exercise的python3+pytq5+Qt Dersinger实现

原创 2017年01月03日 16:41:08

**本文是本人用python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第五章的exercise,请大家参考。
改动的地方:
1,QStringList()用list()代替。
2,self.emit(SIGNAL(“acceptedList(QStringList)”), self.stringlist)在这里完全没必要。**


#/home/yrd/eric_workspace/StringListDlg/Ui_StringListDlg.py

# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(404, 244)
        Dialog.setSizeGripEnabled(True)
        self.widget = QtWidgets.QWidget(Dialog)
        self.widget.setGeometry(QtCore.QRect(10, 10, 381, 222))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.listWidget = QtWidgets.QListWidget(self.widget)
        self.listWidget.setObjectName("listWidget")
        self.horizontalLayout.addWidget(self.listWidget)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setContentsMargins(5, -1, 5, -1)
        self.verticalLayout.setObjectName("verticalLayout")
        self.addButton = QtWidgets.QPushButton(self.widget)
        self.addButton.setObjectName("addButton")
        self.verticalLayout.addWidget(self.addButton)
        self.editButton = QtWidgets.QPushButton(self.widget)
        self.editButton.setObjectName("editButton")
        self.verticalLayout.addWidget(self.editButton)
        self.removeButton = QtWidgets.QPushButton(self.widget)
        self.removeButton.setObjectName("removeButton")
        self.verticalLayout.addWidget(self.removeButton)
        self.upButton = QtWidgets.QPushButton(self.widget)
        self.upButton.setObjectName("upButton")
        self.verticalLayout.addWidget(self.upButton)
        self.downButton = QtWidgets.QPushButton(self.widget)
        self.downButton.setObjectName("downButton")
        self.verticalLayout.addWidget(self.downButton)
        self.sortButton = QtWidgets.QPushButton(self.widget)
        self.sortButton.setObjectName("sortButton")
        self.verticalLayout.addWidget(self.sortButton)
        self.closeButton = QtWidgets.QPushButton(self.widget)
        self.closeButton.setObjectName("closeButton")
        self.verticalLayout.addWidget(self.closeButton)
        self.verticalLayout.setStretch(6, 1)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.horizontalLayout.setStretch(0, 1)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "StringListDlg"))
        self.addButton.setText(_translate("Dialog", "&Add..."))
        self.editButton.setText(_translate("Dialog", "&Edit..."))
        self.removeButton.setText(_translate("Dialog", "&Remove..."))
        self.upButton.setText(_translate("Dialog", "&Up"))
        self.downButton.setText(_translate("Dialog", "&Down"))
        self.sortButton.setText(_translate("Dialog", "&Sort"))
        self.closeButton.setText(_translate("Dialog", "Close"))

#/home/yrd/eric_workspace/StringListDlg/StringListDlg.py

# -*- coding: utf-8 -*-

"""
Module implementing StringListDlg.
"""

from PyQt5.QtCore import pyqtSlot,pyqtSignal,Qt
from PyQt5.QtWidgets import QDialog,QApplication,QInputDialog,QMessageBox,QLineEdit

from Ui_StringListDlg import Ui_Dialog
import sys

MAC = True
try:
    from PyQt5.QtGui import qt_mac_set_native_menubar
except ImportError:
    MAC = False

class StringListDlg(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, name, stringlist=None,parent=None):
        """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(StringListDlg, self).__init__(parent)
        self.setupUi(self)

        self.name = name
        if stringlist is not None:
            self.listWidget.addItems(stringlist)
            self.listWidget.setCurrentRow(0)

        self.addButton.clicked.connect(self.add)
        self.editButton.clicked.connect(self.edit)
        self.removeButton.clicked.connect(self.remove)
        self.upButton.clicked.connect(self.up)
        self.downButton.clicked.connect(self.down)
        self.sortButton.clicked.connect(self.listWidget.sortItems)        
        self.closeButton.clicked.connect(self.accept)
        self.setWindowTitle("Edit {0} List".format(self.name))

        if not MAC:
            self.addButton.setFocusPolicy(Qt.NoFocus)
            self.editButton.setFocusPolicy(Qt.NoFocus)
            self.removeButton.setFocusPolicy(Qt.NoFocus)
            self.upButton.setFocusPolicy(Qt.NoFocus)
            self.downButton.setFocusPolicy(Qt.NoFocus)
            self.sortButton.setFocusPolicy(Qt.NoFocus)
            self.closeButton.setFocusPolicy(Qt.NoFocus)       

    def add(self):
        row = self.listWidget.currentRow()
        title = "Add {0}".format(self.name)
        string, ok = QInputDialog.getText(self, title, title)
        if ok and string:
            self.listWidget.insertItem(row, string)

    def edit(self):
        row = self.listWidget.currentRow()
        item = self.listWidget.item(row)
        if item is not None:
            title = "Edit {0}".format(self.name)
            string, ok = QInputDialog.getText(self, title, title,
                    QLineEdit.Normal, item.text())
            if ok and string:
                item.setText(string)

    def remove(self):
        row = self.listWidget.currentRow()
        item = self.listWidget.item(row)
        if item is None:
            return
        reply = QMessageBox.question(self, "Remove {0}".format(
                self.name), "Remove {0} `{1}'?".format(
                self.name, str(item.text())),
                QMessageBox.Yes|QMessageBox.No)
        if reply == QMessageBox.Yes:
            item = self.listWidget.takeItem(row)
            del item


    def up(self):
        row = self.listWidget.currentRow()
        if row >= 1:
            item = self.listWidget.takeItem(row)
            self.listWidget.insertItem(row - 1, item)
            self.listWidget.setCurrentItem(item)


    def down(self):
        row = self.listWidget.currentRow()
        if row < self.listWidget.count() - 1:
            item = self.listWidget.takeItem(row)
            self.listWidget.insertItem(row + 1, item)
            self.listWidget.setCurrentItem(item)

    def reject(self):
        self.accept()


    def accept(self):
        self.stringlist = list()
        for row in range(self.listWidget.count()):
            self.stringlist.append(self.listWidget.item(row).text())

        QDialog.accept(self)    


if __name__ == "__main__":
    fruit = ["Banana", "Apple", "Elderberry", "Clementine", "Fig",
        "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon",
        "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi", "Lemon",
        "Nectarine", "Plum", "Raspberry", "Strawberry", "Orange"]
    app = QApplication(sys.argv)
    form = StringListDlg("Fruit", fruit)
    form.exec_()
    print("\n".join([str(x) for x in form.stringlist]))

运行结果如下:
这里写图片描述

这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

PyQt学习(5)

由ui生成代码,然后在主程序中应用 示例代码: from PyQt4 import QtCore, QtGui from PyQt4.QtGui import QDialog try: ...

Python Qt GUI快速编程-第五章的例子pen的python3+pytq5+Qt Designer实现

本文是本人用python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第五章的例子Pen,请大家参考。...

Python Qt GUI快速编程-第四章的Exercise例子Interest的python3+pytq5+Qt Designer实现

本文是本人用python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第四章的Exercise例子Interest,请大家参考。...

Python Qt GUI快速编程-第四章的计算器python3+pytq5+Qt Designer实现

本代码为本人通过python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第四章的计算器功能,还在原有例子上增加了两个按钮,请大家参考。...

Python Qt GUI快速编程-第四章的Currency例子的python3+pytq5+Qt Designer实现

本文是本人用python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第四章的Currency例子,请大家参考。...

Qt Gui编程中两种创建窗体界面方法

一、手动编写界面代码 Qt编程中所使用的语言是面向对象的C++,在初学GUI编程时,如果仅仅依靠Qt Designer编辑器来通过拖拽一些widget来设计界面,则会很容易迷惑我们,并且很难弄懂其中...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Python Qt GUI快速编程-第五章的exercise的python3+pytq5+Qt Dersinger实现
举报原因:
原因补充:

(最多只允许输入30个字)