寻找固定大小策略以及scrollArea在选项卡中的显示方式

时间:2019-06-12 21:25:14

标签: python pyqt pyqt5 qscrollarea qsizepolicy

所以我试图使用PyQt5在标签中放置滚动区域。只是在玩一个读取串口的系统。

我遇到的问题是,尽管我设置了QVBoxLayout,但其大小似乎并没有适应我想要的大小。它似乎正在使用其最小尺寸并且未进行调整。

我尝试查看提供的网站上有关QSizePolicy的文档,但不幸的是,这些文档都标记为TODO。

https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qsizepolicy.html

我想知道是否有人对此有经验?

import sys
from PyQt5.QtWidgets import QMainWindow, QSizePolicy, QLabel, QGridLayout, QToolTip, QPlainTextEdit, QScrollArea, QApplication, QPushButton, QWidget, QAction, QTabWidget, QHBoxLayout, QVBoxLayout
from PyQt5.QtGui import *
from PyQt5.QtCore import pyqtSlot, QDateTime, Qt, pyqtSignal, QObject, QSize
import datetime
import serial
import serial.tools.list_ports

import threading


class FDSerial(QMainWindow):

    def __init__(self):

        super().__init__()

        self.initUI()

    def initUI(self):

        self.connected = False
        self.fd_line = ""

        newAct = QAction(QIcon('icn/001-file.png'), 'New', self)
        newAct.triggered.connect(self.init_sc)

        self.toolbar = self.addToolBar('New')
        self.toolbar.addAction(newAct)

        openAct = QAction(QIcon('icn/002-folder.png'), 'Open', self)
        self.toolbar.addAction(openAct)

        connectAct = QAction(QIcon('icn/003-pendrive.png'), 'Connect', self)
        connectAct.triggered.connect(self.find_port)
        self.toolbar.addAction(connectAct)

        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        editMenu = menubar.addMenu('&Settings')
        toolsMenu = menubar.addMenu('&Tools')
        sessionMenu = menubar.addMenu('&Session')
        helpMenu = menubar.addMenu('&Help')

        self.statusBar().showMessage('Ready')

        self.table_widget = LayoutWidgets(self)
        self.setCentralWidget(self.table_widget)

        self.setGeometry(400, 400, 800, 600)
        self.setWindowTitle('FD Serial Demo')
        self.show()

        self.c = Communicate()
        self.c.serialStuff.connect(lambda: self.table_widget.add_row(self.fd_line))

    def init_sc(self):
        self.ser = serial.Serial()
        self.ser.baudrate = 115200
        self.is_connected = False
        self.tests_run = 0
        self.port_found = False

    def find_port(self):
        if self.is_connected is False:
            self.is_connected = True
        else:
            self.is_connected = False
        for port in serial.tools.list_ports.comports():
            if port.vid == 5824 and port.pid == 1155:
                self.ser.port = str(port.device)
                self.port_found = True
                print("Found")
        if self.port_found is False:
            print("Not found")
        x = threading.Thread(target=self.talk_module)
        x.start()

    def talk_module(self):
        self.ser.open()
        while self.is_connected is True:
            self.fd_line = self.ser.readline().decode()
            print(self.fd_line)
            self.c.serialStuff.emit()
        self.ser.close()


class Communicate(QObject):

    serialStuff = pyqtSignal()


class LayoutWidgets(QWidget):

    def __init__(self, parent):
        super(QWidget, self).__init__(parent)

        self.layout = QVBoxLayout(self)
        self.thisthat = 0

        self.mySizePolicy = QSizePolicy()
        self.mySizePolicy.setHorizontalStretch(1)
        self.mySizePolicy.setVerticalStretch(1)
        # self.mySizePolicy.setHeightForWidth(False)
        # self.mySizePolicy.setHorizontalPolicy(QSizePolicy.Maximum)
        # self.mySizePolicy.setVerticalPolicy(QSizePolicy.Maximum)

        self.tabs = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QWidget()

        self.tabs.addTab(self.tab1, "Serial CANFD Interface")
        self.tabs.addTab(self.tab2, "Data Visualizer")
        self.tab1.layout = QVBoxLayout()
        self.tab2.layout = QVBoxLayout()

        self.scrollArea = QScrollArea(self.tab1)

        self.scrollArea.setWidgetResizable(True)

        # self.widget = QWidget()
        # self.scrollArea.setWidget(self.widget)

        self.layout_SArea = QVBoxLayout(self.scrollArea)
        self.layout_SArea.setSpacing(0)

        self.tab1.layout.addWidget(self.scrollArea)

        self.scrollArea.setSizePolicy(self.mySizePolicy)
        self.scrollArea.setStyleSheet("background-color:'#d3f3c8'")

        self.layout.addWidget(self.tabs)

        self.setLayout(self.layout)

        self.qtextbig = QPlainTextEdit()

        self.qtextbig.setSizePolicy(self.mySizePolicy)

        self.qtextbig.setReadOnly(False)
        self.layout_SArea.addWidget(self.qtextbig)

    def add_row(self, row):
        self.this = str(row)
        self.this2 = str(datetime.datetime.now().time())
        self.thisthat = self.thisthat + 1
        self.qtextbig.appendPlainText(self.this)
        self.qtextbig.appendPlainText(self.this2)
        self.qtextbig.appendPlainText(str(self.thisthat))


if __name__ == '__main__':

    app = QApplication(sys.argv)

    ex = FDSerial()

    sys.exit(app.exec_())

Here is how the GUI looks right now.  The scroll are is way too small!  It is in green.

这是GUI的外观。滚动条太小了!它是绿色的。

我正在寻找滚动区域以容纳整个选项卡的大小,然后在调整窗口大小时进行调整。不胜感激任何指针。

谢谢!

1 个答案:

答案 0 :(得分:2)

问题与QSizePolicy无关,它们并不是必需的。问题是您没有使用布局。我认为使用以下指令:

self.tab1.layout = QVBoxLayout()
self.tab2.layout = QVBoxLayout()

向每个选项卡添加布局,但仅指出存在一个名为layout的新属性,该属性采用QVBoxLayout的值,并删除对该选项卡的布局方法的引用,即仅删除访问权限到tab1.layout()方法,这样做是一种不好的做法。

考虑到上述情况,我使用了以下代码:

# ...
class LayoutWidgets(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)

        layout = QVBoxLayout(self)
        self.thisthat = 0

        self.tabs = QTabWidget()
        layout.addWidget(self.tabs)

        self.tab1 = QWidget()
        self.tab2 = QWidget()

        self.tabs.addTab(self.tab1, "Serial CANFD Interface")
        self.tabs.addTab(self.tab2, "Data Visualizer")

        lay = QVBoxLayout(self.tab1)
        self.scrollArea = QScrollArea(widgetResizable=True)
        self.scrollArea.setStyleSheet("background-color:'#d3f3c8'")
        lay.addWidget(self.scrollArea)

        layout_SArea = QVBoxLayout(self.scrollArea)
        self.qtextbig = QPlainTextEdit(readOnly=False)
        layout_SArea.addWidget(self.qtextbig)

    def add_row(self, row):
        self.this = str(row)
        self.this2 = str(datetime.datetime.now().time())
        self.thisthat = self.thisthat + 1
        self.qtextbig.appendPlainText(self.this)
        self.qtextbig.appendPlainText(self.this2)
        self.qtextbig.appendPlainText(str(self.thisthat))
# ...

enter image description here