Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a PyQt widget resizable by dragging?

I have a QScrollArea containing a widget with a QVBoxLayout. Inside this layout are several other widgets. I want the user to be able to drag the lower borders of those widgets to resize them in the vertical direction. When they are resized, I don't want them to "steal" size from the other widgets in the scrolling area; instead I want the entire scrolled "page" to change its size. So if you enlarge one of the widgets, it should push the other widgets down (out of the viewport of the scroll area); if you shrink it, it should pull the other widgets up. Dragging the border of one widget should not change the size of any of the other widgets in the vertical scroll; it should just move them.

I began by using a QSplitter. If I use that, I can drag to change the size of a widget, but there doesn't seem to be a way to get it to "push/pull" the others as I described above, rather than growing/shrinking them. But I can't find any other way to give a widget a draggable handle that will allow me to change its size. How can I accomplish this?

Here is a simple example of what I'm doing. (In this example I've commented out the splitter, but if you uncomment it you can see what happens with that version.)

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.Qsci import QsciScintilla, QsciLexerPython


class SimplePythonEditor(QsciScintilla):
    def __init__(self, parent=None):
        super(SimplePythonEditor, self).__init__(parent)
        self.setMinimumHeight(50)

class Chunk(QFrame):
    def __init__(self, parent=None):
        super(Chunk, self).__init__(parent)


        layout = QVBoxLayout(self)
        sash = QSplitter(self)
        layout.addWidget(sash)
        sash.setOrientation(Qt.Vertical)

        editor = self.editor = SimplePythonEditor()
        output = self.output = SimplePythonEditor()
        output.setReadOnly(True)

        sash.addWidget(editor)
        sash.addWidget(output)

        self.setLayout(layout)
        print(self.sizePolicy())

class Widget(QWidget):

    def __init__(self, parent= None):
        global inout
        super(Widget, self).__init__()

        #Container Widget        
        widget = QWidget()
        #Layout of Container Widget
        layout = QVBoxLayout(self)

        #sash = QSplitter(self)
        #layout.addWidget(sash)
        #sash.setOrientation(Qt.Vertical)

        for num in range(5):
            editor = SimplePythonEditor()
            editor.setText("Some stuff {}".format(num))

            layout.addWidget(editor)
            #sash.addWidget(editor)

        widget.setLayout(layout)

        #Scroll Area Properties
        scroll = QScrollArea()
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll.setWidgetResizable(True)
        scroll.setWidget(widget)
        scroll.setMaximumHeight(500)

        #Scroll Area Layer add 
        vLayout = QVBoxLayout(self)
        vLayout.addWidget(scroll)
        self.setLayout(vLayout)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    dialog = Widget()
    dialog.show()

    app.exec_()
like image 474
BrenBarn Avatar asked May 12 '16 19:05

BrenBarn


1 Answers

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowTitle("MainWindow")
        MainWindow.resize(500, 500)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        MainWindow.setCentralWidget(self.centralwidget)
        QMetaObject.connectSlotsByName(MainWindow)


class Ewindow(QMainWindow,QApplication):
    """docstring for App"""
    resized = pyqtSignal()

    def __init__(self,parent):
        super(Ewindow,self).__init__(parent=parent)
        self.setGeometry(500, 500, 800,800)
        self.setWindowTitle('Mocker')
        self.setWindowIcon(QIcon('icon.png'))
        self.setAttribute(Qt.WA_DeleteOnClose)

        ui2 = Ui_MainWindow()
        ui2.setupUi(self)
        self.resized.connect(self.readjust)

    def resizeEvent(self, event):
        self.resized.emit()
        return super(Ewindow, self).resizeEvent(event)

    def readjust(self):

        self.examForm.move(self.width()-self.examForm.width(),0)
        self.btn_skip.move(self.width()-self.btn_skip.width(),self.height()-100)
        self.btn_next.move(self.btn_showAnswers.x()+self.btn_showAnswers.width(),self.height()-100)
        self.btn_prev.move(0,self.height()-100)
        self.btn_showAnswers.move(self.btn_prev.x()+self.btn_prev.width(),self.height()-100)
        self.btn_home.move(self.width()-200,self.height()-150)

        self.lbscreen1.resize(self.width()-self.examForm.width(),self.height()-200)
        self.examForm.resize(200,self.height()-150)
        self.btn_skip.resize(self.examForm.width(),100)
        self.btn_next.resize(self.btn_prev.width(),100)
        self.btn_prev.resize(self.width()*0.25,100)
        self.btn_showAnswers.resize(self.btn_prev.width(),100)
        self.btn_home.resize(200,50)

here is an example code of a resizable window it moves and stretches widgets as you resize the window. The idea is to keep widget coordinates and sizes relative to each other.

so i had to make a class Ui_MainWindow and set it for my window class ui2.setupUi(self) and also declare the resized = pyqtSignal() which i'd be using to run the readjust function which resets size and coordinates of the widgets like so self.resized.connect(self.readjust).

i hope this helps!

like image 160
FMARAD Avatar answered Oct 19 '22 14:10

FMARAD