PyQt: Displaying QTextEdits over the window
I want to display some QTextEdits over my main window at arbitrary locations. Below is my first attempt. It doesn't quite work. If I create the text edits before I show the window, the text edits appear, but if I create them after I have shown the window they don't app开发者_Go百科ear. What's up with that? How can I get the ones created later to show up?
import sys, random
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
win = QtGui.QMainWindow()
win.resize(500,500)
def new_text():
print "new text"
text = QtGui.QTextEdit(win)
text.move(random.random() * 400, random.random() * 400)
for i in range(3):
new_text()
timer = QtCore.QTimer()
timer.connect(timer, QtCore.SIGNAL("timeout()"), new_text)
timer.start(500)
win.show()
app.exec_()
Oh, I got it. You have to call show on each widget before it appears. I guess QMainWindow.show recursively calls the method for all of its children. So just add text.show() to the end of the new_text function and it works.
精彩评论