PyQt中彈出對話框操做

常常有初學者搞不清楚如何在PyQt中彈出對話框,以及如何處理返回值。這篇文章會舉例說明,界面採用手工編寫。app

咱們通常說的對話框指的是模態對話框(Modal Dialogue Box),一旦彈出,就不能對話框之外的窗口進行操做,必須先關閉對話框。佈局

在PyQt中咱們通常從QDialog繼承建立一個類來操做,根據exec_()方法的返回值判斷用戶是【肯定】仍是【取消】了,固然也能夠其餘返回值,具體看文檔。ui

這個例子建立一個主窗口,有一個表格,記錄用戶姓名和年齡,一個【添加】按鈕,點擊彈出對話框,用戶輸入姓名和年齡,點擊【肯定】返回,在主窗體表格中插入一行數據。spa

建立主窗體對象

爲了方便起見使用QWdiget建立主窗體,固然你可使用QMainWindow,用QTableView和QStandardItemModel來建立表格。blog

class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)

# 建立table和model
table = QtGui.QTableView(parent=self)
self.model = QtGui.QStandardItemModel(parent=self)
self.model.setHorizontalHeaderLabels((u'姓名', u'年齡'))
table.setModel(self.model)

# 建立添加按鈕
button = QtGui.QPushButton(u'添加', parent=self)

# 添加信號槽
button.clicked.connect(self.add)

# 建立一個垂直佈局,用於防止表格和按鈕
layout = QtGui.QVBoxLayout()
layout.addWidget(table)
layout.addWidget(button)

self.setLayout(layout)

def add(self):
pass
建立對話框繼承

對話框從QDialog繼承,按鈕這裏使用QButtonBox來建立,用QButtonBox的好處是建立方便,定義參數便可,而且會自動根據不一樣平臺顯示按鈕的位置,和各平臺風格保持一致,固然默認是英文的,你能夠經過國際化來作成中文的。utf-8

這裏沒有作對話框內容的驗證,你能夠覆蓋QDialog的accept方法來進行驗證。文檔

下面是對話框的建立代碼,爲了方便獲取姓名和年齡變量,我寫了兩個方法供外部調用。get

class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.resize(240, 200)

# 表格佈局,用來佈局QLabel和QLineEdit及QSpinBox
grid = QtGui.QGridLayout()

grid.addWidget(QtGui.QLabel(u'姓名', parent=self), 0, 0, 1, 1)

self.leName = QtGui.QLineEdit(parent=self)
grid.addWidget(self.leName, 0, 1, 1, 1)

grid.addWidget(QtGui.QLabel(u'年齡', parent=self), 1, 0, 1, 1)

self.sbAge = QtGui.QSpinBox(parent=self)
grid.addWidget(self.sbAge, 1, 1, 1, 1)

# 建立ButtonBox,用戶肯定和取消
buttonBox = QtGui.QDialogButtonBox(parent=self)
buttonBox.setOrientation(QtCore.Qt.Horizontal) # 設置爲水平方向
buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) # 肯定和取消兩個按鈕
# 鏈接信號和槽
buttonBox.accepted.connect(self.accept) # 肯定
buttonBox.rejected.connect(self.reject) # 取消

# 垂直佈局,佈局表格及按鈕
layout = QtGui.QVBoxLayout()

# 加入前面建立的表格佈局
layout.addLayout(grid)

# 放一個間隔對象美化佈局
spacerItem = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
layout.addItem(spacerItem)

# ButtonBox
layout.addWidget(buttonBox)

self.setLayout(layout)

def name(self):
return self.leName.text()

def age(self):
return self.sbAge.value()
編寫對話框調用代碼

調用對話框只要使用exec_方法便可,它會彈出對話框並根據用戶操做返回值,根據返回值判斷是【肯定】仍是【取消】。

dialog = Dialog(parent=self)
if dialog.exec_():
self.model.appendRow((
QtGui.QStandardItem(dialog.name()),
QtGui.QStandardItem(str(dialog.age())),
))

dialog.destroy()
完整代碼和截圖

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

class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)

# 建立table和model
table = QtGui.QTableView(parent=self)
self.model = QtGui.QStandardItemModel(parent=self)
self.model.setHorizontalHeaderLabels((u'姓名', u'年齡'))
table.setModel(self.model)

# 建立添加按鈕
button = QtGui.QPushButton(u'添加', parent=self)

# 添加信號槽
button.clicked.connect(self.add)

# 建立一個垂直佈局,用於防止表格和按鈕
layout = QtGui.QVBoxLayout()
layout.addWidget(table)
layout.addWidget(button)

self.setLayout(layout)

def add(self):
dialog = Dialog(parent=self)
if dialog.exec_():
self.model.appendRow((
QtGui.QStandardItem(dialog.name()),
QtGui.QStandardItem(str(dialog.age())),
))

dialog.destroy()


class Dialog(QtGui.QDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.resize(240, 200)

# 表格佈局,用來佈局QLabel和QLineEdit及QSpinBox
grid = QtGui.QGridLayout()

grid.addWidget(QtGui.QLabel(u'姓名', parent=self), 0, 0, 1, 1)

self.leName = QtGui.QLineEdit(parent=self)
grid.addWidget(self.leName, 0, 1, 1, 1)

grid.addWidget(QtGui.QLabel(u'年齡', parent=self), 1, 0, 1, 1)

self.sbAge = QtGui.QSpinBox(parent=self)
grid.addWidget(self.sbAge, 1, 1, 1, 1)

# 建立ButtonBox,用戶肯定和取消
buttonBox = QtGui.QDialogButtonBox(parent=self)
buttonBox.setOrientation(QtCore.Qt.Horizontal) # 設置爲水平方向
buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok) # 肯定和取消兩個按鈕
# 鏈接信號和槽
buttonBox.accepted.connect(self.accept) # 肯定
buttonBox.rejected.connect(self.reject) # 取消

# 垂直佈局,佈局表格及按鈕
layout = QtGui.QVBoxLayout()

# 加入前面建立的表格佈局
layout.addLayout(grid)

# 放一個間隔對象美化佈局
spacerItem = QtGui.QSpacerItem(20, 48, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
layout.addItem(spacerItem)

# ButtonBox
layout.addWidget(buttonBox)

self.setLayout(layout)

def name(self):
return self.leName.text()

def age(self):
return self.sbAge.value()


if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())

相關文章
相關標籤/搜索