如何通过拖动使PyQt小部件可调整大小? [英] How can I make a PyQt widget resizable by dragging?

查看:500
本文介绍了如何通过拖动使PyQt小部件可调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个QScrollArea,其中包含带有QVBoxLayout的小部件.在此布局内还有其他几个小部件.我希望用户能够拖动这些小部件的下边框以在垂直方向上调整它们的大小.调整大小时,我不希望它们从滚动区域中的其他小部件中窃取"大小;相反,我希望整个滚动的页面"更改其大小.因此,如果放大其中一个小部件,则应将其他小部件向下推(超出滚动区域的视口);否则,将其向下推.如果缩小它,它应该将其他小部件拉起.拖动一个窗口小部件的边框不应更改垂直滚动中其他窗口小部件的大小.它应该移动它们.

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.

我从使用QSplitter开始.如果使用该控件,则可以拖动以更改小部件的大小,但是似乎没有一种方法可以像上面所述的那样推/拉"其他控件,而不是增大/缩小它们.但是我找不到其他方法给小部件提供可拖动的句柄,使我可以更改其大小.我该怎么做?

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

推荐答案

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.

所以我必须做一个class Ui_MainWindow并将其设置为我的窗口类ui2.setupUi(self),还要声明resized = pyqtSignal(),我将用它来运行readjust函数,该函数可以像这样重置小部件的大小和坐标self.resized.connect(self.readjust).

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).

我希望这会有所帮助!

这篇关于如何通过拖动使PyQt小部件可调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆