0

I designed a window with QtDesigner and added a scrollarea and a layout in it, but the scroll area doesn't scroll, it shows the scroll bar but It cant be scrolled at all.

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)
class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(767, 600)
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
    self.pushButton = QtGui.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(230, 300, 75, 23))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.lineEdit = QtGui.QLineEdit(self.centralwidget)
    self.lineEdit.setGeometry(QtCore.QRect(80, 330, 371, 71))
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtGui.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 767, 21))
    self.menubar.setObjectName(_fromUtf8("menubar"))
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtGui.QStatusBar(MainWindow)
    self.statusbar.setObjectName(_fromUtf8("statusbar"))
    self.scrollArea = QtGui.QScrollArea(self.centralwidget)
    self.scrollArea.setGeometry(QtCore.QRect(130, 70, 301, 191))
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setMinimumSize(QtCore.QSize(100, 100))
    self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
    self.scrollAreaWidgetContents = QtGui.QWidget()
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 299, 189))
    self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
    self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(100, 100))
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
    self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.horizontalLayoutWidget = QtGui.QWidget(self.scrollAreaWidgetContents)
    self.horizontalLayoutWidget.setGeometry(QtCore.QRect(-1, -1, 301, 191))
    self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
    self.horizontalLayout = QtGui.QVBoxLayout(self.horizontalLayoutWidget)
    self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))



    p= QtGui.QPalette()
    p.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
    self.scrollArea.setAutoFillBackground(True)
    self.scrollArea.setPalette(p)
    self.retranslateUi(MainWindow)

def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
    self.pushButton.setText(_translate("MainWindow", "send", None))

I want to add labels and this is how I do it

from main_ui import *
import sys
global height
height=10
def send():
        global height
        label=QtGui.QLabel(myapp.ui.horizontalLayoutWidget)
        label.setGeometry(QtCore.QRect(10, height, 46, 13))
        height=height+label.height()
        label.setText(myapp.ui.lineEdit.text())
        label.show()


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()

myapp.show()
QtCore.QObject.connect(myapp.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), send)
myapp.setCentralWidget(myapp.ui.centralwidget)
QtCore.QMetaObject.connectSlotsByName(myapp)
sys.exit(app.exec_())
Nadav Barak
  • 177
  • 1
  • 3
  • 14

1 Answers1

1

Sir, your code is a bit messed.

Here is a small example, see if it works. There is no secret. :D

it's in PyQt5. You just gotta change your imports to PyQt4.

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QScrollArea
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget


class WSlideBar(QWidget):
    """WSlideBar is a personalized slide bar."""

    w_container = None
    v_layout_container = None
    v_scroll_area = None
    v_layout_preview = None


    def __init__(self):
        """Init UI."""

        super(WSlideBar, self).__init__()
        self.init_ui()

    def init_ui(self):
        """Init all ui object requirements."""


        self.setFixedSize(100,500)

        self.setStyleSheet("""
            background: gray;
        """)

        # Container Widget
        self.w_container = QWidget()
        self.w_container.setFixedWidth(100)

        # Layout of Container Widget
        self.v_layout_container = QVBoxLayout()
        self.v_layout_container.setSpacing(100)

        aux = QWidget()
        aux.setFixedSize(10,10)
        aux.setStyleSheet("""background: red;""")

        aux2 = QWidget()
        aux2.setFixedSize(20, 20)
        aux2.setStyleSheet("""background: blue;""")

        aux3 = QWidget()
        aux3.setFixedSize(15, 15)
        aux3.setStyleSheet("""background: yellow;""")

        aux4 = QWidget()
        aux4.setFixedSize(50,50)
        aux4.setStyleSheet("""background: rgb(0,255,0,30%);""")

        aux5 = QWidget()
        aux5.setFixedSize(40, 40)
        aux5.setStyleSheet("""background: green;""")

        aux6 = QWidget()
        aux6.setFixedSize(40, 40)
        aux6.setStyleSheet("""background: green;""")

        self.v_layout_container.addWidget(aux)
        self.v_layout_container.addWidget(aux2)
        self.v_layout_container.addWidget(aux3)
        self.v_layout_container.addWidget(aux4)
        self.v_layout_container.addWidget(aux5)
        self.v_layout_container.addWidget(aux6)

        self.w_container.setLayout(self.v_layout_container)

        self.v_scroll_area = QScrollArea(self)
        self.v_scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        self.v_scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.v_scroll_area.setWidget(self.w_container)

        # Scroll Area Layer add
        self.v_layout_preview = QVBoxLayout()
        self.setLayout(self.v_layout_preview)

        self.v_layout_preview.addWidget(self.v_scroll_area)

def run():
    app = QApplication(sys.argv)
    GUI = WSlideBar()
    GUI.show()
    sys.exit(app.exec_())


run()

OBS: You will just have to change for QPushButtons, QLabels,... instead of small QWidgets and whatever you need.

yurisnm
  • 1,630
  • 13
  • 29
  • It works, but Im using Qt designer, and it creates the window in a different way, by geomerty place. – Nadav Barak Nov 01 '16 at 22:49
  • You mean setGeometry? Because it's is the same. The difference is that setGeometry set (x,y,width,height) instead of fixedSize. It's only syntax, same logic :D – yurisnm Nov 02 '16 at 11:48