【问题标题】:QDialog with scrollbars visible only when content is bigger than the size仅当内容大于大小时,带有滚动条的 QDialog 才可见
【发布时间】:2022-07-12 15:24:44
【问题描述】:

如何使 QDialog 根据内容调整大小,如果内容大于屏幕大小,将对话框调整为屏幕大小并显示滚动条(水平和垂直)?

提供一些上下文:

我创建了继承自 QDialog 的类,用作通用对话框(从中创建自定义错误、警告和信息对话框),并且我想添加一个 QScrollArea,因此当内容大于屏幕大小时,对话框调整为屏幕大小(无论是宽度、高度还是两者都扩展)。

为了使对话框灵活适应内容,我使用了adjustSize()。但是当我的内容大于屏幕尺寸时,即使我将 sizeHint 覆盖为 self.screen().size(),QScrollArea 也不会占用屏幕尺寸。

我将创建一个小样本并在明天早上更新帖子(但希望将其写下来以防有人知道如何做)。

【问题讨论】:

    标签: qt qdialog qscrollarea


    【解决方案1】:

    这就是我修复它的方法,遵循其他帖子Cannot automatically resize a QScrollArea中提到的方法

    import sys
    import os
    
    from PySide2.QtCore import QFile, QIODevice, QTextStream, QSize, QTimer
    from PySide2.QtWidgets import QWidget, QHBoxLayout, QLabel, QDialog, QScrollArea, QFrame, QApplication, QStyle
    
    _SAMPLE_FILE_PATH: str = 'sample.txt'
    
    
    def get_text_from_file(file_path: str) -> str:
        text: str = ""
        file_path = os.path.join(os.path.dirname(__file__), file_path)
        if QFile.exists(file_path):
            file = QFile(file_path)
            if file.open(QIODevice.ReadOnly | QIODevice.Text):
                stream = QTextStream(file)
                text = stream.readAll()
            file.close()
        else:
            print('file does not exist')
        return text
    
    
    class ScrollBaseDialog(QDialog):
        def __init__(
                self,
                title: str,
                content: QWidget):
            super(ScrollBaseDialog, self).__init__()
    
            self.setWindowTitle(title)
            self._scroll_area = QScrollArea(self)
            self._scroll_area.setWidget(content)
            self._content = content
    
            self._scroll_area.setWidgetResizable(True)
            self._scroll_area.setFrameShape(QFrame.NoFrame)
    
            layout = QHBoxLayout()
            layout.setContentsMargins(0, 0, 0, 0)
            layout.addWidget(self._scroll_area)
            self.setLayout(layout)
            self.adjustSize()
            QTimer.singleShot(0, self._adjust_geometry)
    
        def _adjust_geometry(self) -> None:
            header_height = self.style().pixelMetric(QStyle.PM_TitleBarHeight)
            scroll_width = self.style().pixelMetric(QStyle.PM_ScrollBarExtent)
    
            max_size = self.screen().availableSize() - QSize(scroll_width, header_height)  # screen_size - header
            width = min(max_size.width(), self._content.width()) + scroll_width  # scrollbar width
            height = min(max_size.height(), self._content.height())
    
            x: int = self.screen().geometry().x() + scroll_width/2 + (max_size.width() - width) / 2
            y: int = self.screen().geometry().y() + header_height + (max_size.height() - height) / 2
            self.setGeometry(x, y, width, height)
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        dialog = ScrollBaseDialog(title='Sample Dialog', content=QLabel(get_text_from_file(_SAMPLE_FILE_PATH)))
        dialog.show()
        sys.exit(app.exec_())
    

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      相关资源
      最近更新 更多