一些基本的资料网上都可以找到,这里就说一下这几天遇到的
1,PYQt4 中信号槽机制:
本想点击一个按钮然后实现去另一个函数,最初这样写的:
self.connect(button, QtCore.SIGNAL('clicked()'), QtGui.qApp,结果跟预想的不一样=== 还没有点击按钮,func就执行了;经过查资料之后,发现自己的写法本身就有问题,就是self.func()这里不应该有‘()’ ,于是去掉还不可以。后来发现,不应该有前面的Qt.Core.SLOT.....下面说一下可行的两种机制:
QtCore.SLOT('self.func()'))## self.func 为自己写的一个函数
(1)旧时的信号槽机制:
QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), self.onClicked)例如:
# -*- coding: cp936 -*-这样点击了‘点我’ 才会打印出来‘ssss’
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication([])
w = QtGui.QWidget()
def showMsg():
print 'ssss'
btn = QtGui.QPushButton(u"点我", w)
w.connect(btn, QtCore.SIGNAL("clicked()"), showMsg)
w.show()
app.exec_()
(2)新式机制:
button.clicked.connect(self.onClicked)例如同样的例子:
# -*- coding: cp936 -*-效果一样;
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication([])
w = QtGui.QWidget()
def showMsg():
print 'ssss'
btn = QtGui.QPushButton(u"点我", w)
#w.connect(btn, QtCore.SIGNAL("clicked()"), showMsg)
btn.clicked.connect(showMsg)
w.show()
app.exec_()
不过问过大神之后,以后尽量用新式的机制,比较符合 pyhton的标准,而且直观容易理解;
2,Pyqt几种常用的弹窗(额。。暂且这么叫吧)
fileName = QtGui.QFileDialog.getOpenFileName( self, 'Open' )## 弹出对话框,选择文件
selectDirName=QtGui.QFileDialog.getExistingDirectory(None,"Select a directory","")## 弹出对话框,选择文件夹
def OnInfoButton( self ): QtGui.QMessageBox.information( self, "Pyqt", "information" )##弹出信息框 def OnQuestionButton( self ): r = QtGui.QMessageBox.question( self, "PyQt", "Question", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel )## 弹出询问框 def OnWarningButton( self ): r = QtGui.QMessageBox.warning( self, "PyQT", "warning", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No )#弹出警告框