2016-11-19 81 views
-1

我正在使用pyQT创建器和python.it工作在GUI上,它是一个使用绘制矩形来保存使用Qpainter的聊天文本的聊天应用程序。所有绘制的小部件都显示在scrollArea(verticalLayout内)中。更改新窗口小部件的大小

迄今为止GUI正在工作,你可以在下面的代码见:

from PyQt5 import QtCore, QtGui, QtWidgets 
class Bot_Bubble(QtWidgets.QLabel): 
    def __init__(self,text): 
    super(Bot_Bubble,self).__init__(text) 
    self.setContentsMargins(5,5,5,5) 

def paintEvent(self, e): 
    p = QtGui.QPainter(self) 
    p.setRenderHint(QtGui.QPainter.Antialiasing,True) 
    p.fillRect(self.rect(), QtGui.QColor("#F0F8FF")) 
    p.drawRoundedRect(self.rect(), 3.0, 3.0) 
    super(Bot_Bubble,self).paintEvent(e) 

class Ui_chatbotgui(object): 
    def setupUi(self, chatbotgui): 
    chatbotgui.setObjectName("chatbotgui") 
    chatbotgui.resize(361, 566) 
    chatbotgui.setStyleSheet("background-color:rgba(0, 0, 0, 0)") 
    self.gridLayout = QtWidgets.QGridLayout(chatbotgui) 
    self.gridLayout.setContentsMargins(11, 11, 11, 11) 
    self.gridLayout.setSpacing(6) 
    self.gridLayout.setObjectName("gridLayout") 
    self.scrollArea = QtWidgets.QScrollArea(chatbotgui) 
    self.scrollArea.setLayoutDirection(QtCore.Qt.RightToLeft) 
    self.scrollArea.setStyleSheet("background-color: rgb(255, 255, 255);\n""border-style: solid;\n""border-color: white;\n""border-width: 3px;\n""border-radius: 10px;") 
    self.scrollArea.setWidgetResizable(True) 
    self.scrollArea.setObjectName("scrollArea") 
    self.scrollAreaWidgetContents = QtWidgets.QWidget() 
    self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents") 
    self.verticalLayout_2 =QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents) 
    self.verticalLayout_2.setContentsMargins(11, 11, 11, 11) 
    self.verticalLayout_2.setSpacing(6) 
    self.verticalLayout_2.setDirection(QtWidgets.QVBoxLayout.TopToBottom) 
    self.verticalLayout_2.setObjectName("verticalLayout_2") 
    self.verticalLayout_2.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint) 
    self.scrollArea.setWidget(self.scrollAreaWidgetContents) 
    self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1) 
    self.lineEdit = QtWidgets.QLineEdit(chatbotgui) 
    self.lineEdit.setEnabled(True) 
    self.lineEdit.setLayoutDirection(QtCore.Qt.RightToLeft) 
    self.lineEdit.setAutoFillBackground(False) 
    self.lineEdit.setStyleSheet("background-color: rgb(255, 255, 255);\n""border-style: solid;\n""border-color: white;\n""border-width: 3px;\n""border-radius: 10px;") 
    self.lineEdit.setObjectName("lineEdit") 
    self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1) 
    self.lineEdit.returnPressed.connect(self.showtext) 
    self.retranslateUi(chatbotgui) 
    QtCore.QMetaObject.connectSlotsByName(chatbotgui) 
    self.scrollArea.raise_() 
def retranslateUi(self, chatbotgui): 
    _translate = QtCore.QCoreApplication.translate 
    chatbotgui.setWindowTitle(_translate("chatbotgui", "المساعد الذكي")) 

def showtext(self): 

    text = self.lineEdit.text() 
    label = Bot_Bubble(text) 
    self.verticalLayout_2.addWidget(label) 
    self.verticalLayout_2.addStretch() 

if __name__ == "__main__": 
    import sys 
    app = QtWidgets.QApplication(sys.argv) 
    chatbotgui = QtWidgets.QWidget() 
    ui = Ui_chatbotgui() 
    ui.setupUi(chatbotgui) 
chatbotgui.show() 
sys.exit(app.exec_()) 

但whidth绘控件的i'have一些问题所花费的VerticalLayout的宽度相同(我想是与用户文本的宽度相同)?!

谢谢你,

回答

0

您可以使用setSizePolicyQSizePolicy改变部件将多大的存储空间的布局里面,你的情况:

# in showtext() method 
label.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) 
相关问题