0
我剛剛寫了一個小例子,我無法設法讓它運行。pyqt4 mousePressEvent not called(no widget?)
from PyQt4 import QtGui, QtCore
import sys
class Drawer(QtGui.QWidget):
def __init__(self, parent=None):
super(Drawer, self).__init__(parent)
self.setStyleSheet("QWidget { background-color: %s }" % QtGui.QColor(99, 0, 0).name())
def mousePressEvent(self, event):
print 'mouse pressed'
self.update();
class MyApp(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.drawer = Drawer(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = MyApp()
myapp.show()
sys.exit(app.exec_())
插件未示出(無顏色,窗口是灰色)和,如果我按下鼠標沒有打印..
哪裏是我的錯誤?
已解決:作爲qiao只是指着我的評論,我的錯誤是在qt4類場景圖中添加小部件的方式。我想我不得不打電話給初始父母,就這些。這不是enogh,我必須添加一個QLayout並在其中添加孩子(這很明顯:方法addWidget只寫在QLayout中,而不是在QWidget中,並且沒有添加新孩子的可能性的場景圖系統是很奇怪)
對於這個例子它運行正常。但它是一種簡化! :(我有一個更大的GUI設計與QtDesigner,我有:* self.drawer =抽屜(self.ui.drawBox)*。如果我嘗試:* self.ui.drawBox.setCentralWidget(self.drawer)*我得到** AttributeError:'QWidget'對象沒有屬性'setCentralWidget'** – nkint 2012-01-18 17:09:55
@nkint'setCentralWidget'只能應用於'QMainWindow'的實例。對於其他小部件,您必須設置佈局並將子部件添加到佈局中 – qiao 2012-01-18 17:13:41
解決了,謝謝 – nkint 2012-01-18 17:18:42