-1

i'm working on GUI using pyQT creator and python.it is a chat application that uses a painted rectangle to hold the chat's text using Qpainter. all painted widgets are show in scrollArea ( inside verticalLayout).

so far the GUI is working as you can see in the following codes:

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_())

but i'have some problem in whidth of the painted widgets it takes the same width of the verticalLayout ( i want is to be the same width of the user text)?!

thank you,

Eman S.
  • 145
  • 1
  • 8

1 Answers1

0

You can use setSizePolicy and QSizePolicy to change how much space the widgets will take inside the layout, in your case:

# in showtext() method
label.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
Ceppo93
  • 1,026
  • 10
  • 11