简体   繁体   English

如何通过拖动使 PyQt 小部件可调整大小?

[英]How can I make a PyQt widget resizable by dragging?

I have a QScrollArea containing a widget with a QVBoxLayout.我有一个 QScrollArea,其中包含一个带有 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.我从使用 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.这个想法是保持小部件坐标和大小相对。

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) .所以我必须创建一个class Ui_MainWindow并将它设置为我的窗口类ui2.setupUi(self)并声明resized = pyqtSignal()我将使用它来运行重新调整函数,该函数重置小部件的大小和坐标,例如所以self.resized.connect(self.readjust)

i hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PyQt制作可调整大小的窗口和可调整大小的背景图像? - How do I make a resizable window and a resizable background image using PyQt? PyQt - 在可调整大小的GridLayout中居中小部件 - PyQt - Centering Widget in resizable GridLayout 在 Pyqt 中,如何通过空的小部件空间启用拖动/移动窗口? - In Pyqt, how do I enable dragging/moving windows by the empty widget space? 我可以让一个小部件跨越 pyQt 中的多列吗 - Can I make a widget span more than one column in pyQt 如何让 FigureCanvas 在嵌入在 pyqt GUI 中的 matplotlib 小部件中填充整个图形? - How can I make the FigureCanvas fill the entire Figure in a matplotlib widget embedded in a pyqt GUI? 如何使 QGridLayout() 在水平和垂直方向上都可以调整大小? - how can I make QGridLayout() resizable in both horizontal and vertical directions? 如何在PyQt中获取之前激活的小部件? - How can I get the previous activated widget in PyQt? 如何将可变视频渲染到 PyQt 小部件? - How can I render a mutable video to a PyQt widget? 如何在 pyqt5 中将 QRadioButton 小部件添加到 QTableView - How can I add QRadioButton Widget to QTableView in pyqt5 Pyqt5:如何创建可手动编辑的 QDateEdit 小部件? - Pyqt5: How can I create a manually editable QDateEdit widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM