2014-03-05 60 views
1

我正在學習PySide,我似乎無法得到QLineEdit出現。我最好的猜測是因爲QTextEdit被設置爲主窗口小部件並將其覆蓋。這裏是我的代碼:PySide QLineEdit沒有出現

#!/usr/bin/python 

import sys 
from PySide import QtGui 

class Window(QtGui.QMainWindow): 
    def __init__(self): 
    super(Window, self).__init__() 
    self.initUI() 

    def initUI(self): 
    self.setWindowTitle("A Text Editor") 
    self.setGeometry(300,1000,500,500) 
    self.fileName = QtGui.QLineEdit('File Name')  
    self.fileName.resize(self.fileName.sizeHint()) 

    self.text = QtGui.QTextEdit() 
    self.setCentralWidget(self.text) 

    menubar = self.menuBar() 
    exitAction = QtGui.QAction(QtGui.QIcon('exit.png'),'&Exit',self) 
    exitAction.setShortcut('Ctrl+Q') 
    exitAction.triggered.connect(self.close) 
    saveAction = QtGui.QAction(QtGui.QIcon('save.png'),'&Save',self) 
    saveAction.setShortcut('Ctrl+S') 
    saveAction.triggered.connect(self.save) 

    fileMenu = menubar.addMenu('&File') 
    fileMenu.addAction(exitAction) 
    fileMenu.addAction(saveAction) 
    self.show() 

    def save(self): 
    f = open(self.fileName.text(),'w') 
    f.write(self.text.toPlainText()) 
    f.close() 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    win = Window() 
    exit(app.exec_()) 

if __name__ == "__main__": 
    main() 

回答

0

你將要設置一個佈局形式的中央小部件,讓他們都出現。

喜歡的東西:

layout = QtGui.QVBoxLayout() 
layout.addWidget(self.fileName) 
layout.addWidget(self.text) 
centralWidget = QtGui.QWidget() 
centralWidget.setLayout(layout) 
self.setCentralWidget(centralWidget) 
+0

謝謝!當我有機會的時候我會測試它。 – Awalrod