我想在运行时向QVBoxLayout
添加小部件。但是当我使用该布局的addWidget
方法时它不起作用。然后我想是否需要用于刷新布局或应用程序的方法,但其他不需要它的代码有很好的代表性。
这是我的代码:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from socket import *
from collections import deque
app = QApplication(sys.argv)
class Chatting(QWidget) :
_name = None
serverAddr = ('localhost', 20123)
client = socket(AF_INET, SOCK_DGRAM)
def __init__(self,name,):
super(Chatting, self).__init__()
self._name = name
self.setWindowTitle('Chatting')
self.setFixedWidth(645)
self.setFixedHeight(445)
scrollArea = QScrollArea()
scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scrollArea.setWidgetResizable(False)
self.scrollWidget = QWidget()
self.scrollWidget.setFixedWidth(645)
self.scrollWidget.setFixedWidth(400)
self.verticalLayoutTop = QVBoxLayout(self)
self. scrollWidget.setLayout(self.verticalLayoutTop)
scrollArea.setWidget(self.scrollWidget)
send = QWidget()
horizontalLayoutBottom = QHBoxLayout(self)
self.contentEdit = QLineEdit()
horizontalLayoutBottom.addWidget(self.contentEdit)
self.sendButton = QPushButton('Send')
self.sendButton.clicked.connect(self.send)
horizontalLayoutBottom.addWidget(self.sendButton)
send.setLayout(horizontalLayoutBottom)
mainLayout = QVBoxLayout(self)
mainLayout.addWidget(scrollArea)
mainLayout.addWidget(send)
self.setLayout(mainLayout)
clientThread = ClientThread(client=self.client,parent=self)
self.connect(clientThread,clientThread.signal,self.writeToMessageList)
clientThread.start()
def send(self):
self.client.sendto(self._name + ' say:' + self.contentEdit.text().toUtf8(),self.serverAddr)
self.contentEdit.setText('')
def writeToMessageList(self,message):
print message
'''
I can get data here.
So errors happened at the following two line.
'''
messageLabel = QLabel(message)
self.scrollWidget.layout().addWidget(messageLabel)
class ClientThread(QThread) :
BUFSIZE = 1024
_client = None
def __init__(self,client,parent=None):
QThread.__init__(self, parent)
self._client = client
self.signal = SIGNAL("signal")
def run(self):
self.receive()
def receive(self):
while True:
data,addr = self._client.recvfrom(self.BUFSIZE)
self.emit(self.signal,data)
if __name__ == '__main__':
dialog = Chatting(name='Little pig')
dialog.show()
app.exec_()
答案 0 :(得分:1)
我看到两个问题:
self.scrollWidget.setFixedWidth(645)
self.scrollWidget.setFixedWidth(400)
让布局控件小部件大小。
另一个问题是QVBoxLayout(self)
!如果您将widget作为布局构造函数参数传递,则会立即将布局设置为给定窗口小部件,因此在您的情况下为Chatting
。从那时起,这种布局就无法移动到其他小部件(你应该在日志中有一些警告)。
所以修理如下:
def __init__(self,name,):
super(Chatting, self).__init__()
self._name = name
self.setWindowTitle('Chatting')
self.setFixedWidth(645)
self.setFixedHeight(445)
scrollArea = QScrollArea()
scrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
scrollArea.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
scrollArea.setWidgetResizable(False)
self.scrollWidget = QWidget()
self.verticalLayoutTop = QVBoxLayout()
self. scrollWidget.setLayout(self.verticalLayoutTop)
scrollArea.setWidget(self.scrollWidget)
send = QWidget()
horizontalLayoutBottom = QHBoxLayout(send)
self.contentEdit = QLineEdit()
horizontalLayoutBottom.addWidget(self.contentEdit)
self.sendButton = QPushButton('Send')
self.sendButton.clicked.connect(self.send)
horizontalLayoutBottom.addWidget(self.sendButton)
mainLayout = QVBoxLayout(self)
mainLayout.addWidget(scrollArea)
mainLayout.addWidget(send)
clientThread = ClientThread(client=self.client,parent=self)
self.connect(clientThread,clientThread.signal,self.writeToMessageList)
clientThread.start()