简体   繁体   English

PyQt5 QWidget未填充父项

[英]PyQt5 QWidget not filling parent

I'm creating a code editor in PyQt5. 我正在PyQt5中创建代码编辑器。 I'm using a QGridLayout in the center widget and building the UI from there. 我在中心小部件中使用QGridLayout并从那里构建UI。 The problem I'm facing is that the QPlainTextEdit's and the Menu don't fill the available space on the center widget. 我面临的问题是QPlainTextEdit和菜单不能填充中心小部件上的可用空间。 I tried setting the size policy to expanding and tried adapting the stretch parameters of the QGridLayout. 我尝试将大小策略设置为扩展,并尝试调整QGridLayout的拉伸参数。

I'm running the code in python 2.7 and PyQt 5. Using the PyCharm IDE. 我正在python 2.7和PyQt 5中运行代码。使用PyCharm IDE。

Main.py Main.py

import os
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QAction

from PyQt5.QtGui import QIcon

import UI

versao = '3.0.0'


class Principal(QMainWindow):

    def __init__(self):
        super(Principal, self).__init__()

        self.acao_novo = 0
        self.acao_abrir = 0
        self.acao_exemplos = 0
        self.acao_sair = 0
        self.acao_salvar = 0
        self.acao_salvar_como = 0
        self.acao_comentar_linha = 0
        self.acao_achar = 0
        self.acao_achar_e_substituir = 0
        self.acao_ir_para_linha = 0
        self.acao_placa = 0
        self.acao_porta = 0
        self.acao_lingua = 0
        self.acao_monitor_serial = 0
        self.acao_verificar = 0
        self.acao_verificar_e_carregar = 0

        self.init_ui()

    def init_ui(self):

        self.setCentralWidget(UI.Centro())

        self.setGeometry(300, 300, 500, 550)
        self.setWindowTitle('Br.ino ' + versao)
        self.setWindowIcon(QIcon(os.path.join('recursos', 'logo.png')))
        self.show()

def main():
    app = QApplication(sys.argv)
    principal = Principal()
    app.setStyleSheet("""QMainWindow {
                             background: '#252525';
                         }
                         QMenu{
                             background: '#252525';
                         }
                         QMenu::item {
                             background: '#252525';
                         } 
                         QMenu::item:selected{
                             background: '#101010';
                         }
                         QMenuBar {
                             background: '#252525';
                         }
                         QMenuBar::item {
                             background: '#252525';
                         } 
                         QMenuBar::item:selected{
                             background: '#101010';
                         }
                         QPlainTextEdit{
                             background: '#252525';
                             border: None;
                             border-radius: 6px;
                             color: '#efefef';
                             selection-background-color: '#454545';
                         }""")
    sys.exit(app.exec_())


if __name__ == '__main__':
    main() 

UI.py UI.py

from PyQt5.QtWidgets import (QWidget, QGridLayout, QPlainTextEdit, QVBoxLayout, QSpacerItem, QSizePolicy, QAbstractButton)
import os
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtCore import QSize


class Centro(QWidget):

    def __init__(self):
        super(Centro, self).__init__()
        self.layout = 0

        self.init_ui()

    def init_ui(self):
        layout = QGridLayout(self)
        layout.setRowStretch(0, 7.5)
        layout.setRowStretch(1, 2.5)
        layout.setColumnStretch(0, 1)
        layout.setColumnStretch(1, 5)
        menu = Menu.Menu()
        layout.addWidget(menu, 0, 0, 1, 0)
        layout.setSpacing(10)
        layout.setContentsMargins(0, 0, 0, 0)

        container = QWidget(self)
        container.setStyleSheet("background:#252525")
        editor = QPlainTextEdit(container)
        layout.addWidget(container, 0, 1, 9, 9)

        container_log = QWidget(self)
        log = QPlainTextEdit(container_log)
        log.setStyleSheet("background:#000000")
        log.setDisabled(True)
        layout.addWidget(container_log, 1, 1, 1, 8)

        self.show()


class Menu(QWidget):

    def __init__(self):
        super(Menu, self).__init__()
        self.layout = 0

        self.init_ui()

    def init_ui(self):
        container = QWidget(self)
        container.setFixedWidth(60)
        layout = QVBoxLayout(container)
        container.setStyleSheet("background-color: '#5cb50d';")

        btn_compilar = botaoImagem(QPixmap(os.path.join('recursos', 'compilarFoco.png')), self)
        btn_compilar_e_carregar = botaoImagem(QPixmap(os.path.join('recursos', 'carregarFoco.png')), self)
        btn_novo = botaoImagem(QPixmap(os.path.join('recursos', 'novoArquivoFoco.png')), self)
        btn_abrir = botaoImagem(QPixmap(os.path.join('recursos', 'abrirPastaFoco.png')), self)
        btn_salvar = botaoImagem(QPixmap(os.path.join('recursos', 'salvarFoco.png')), self)
        btn_monitor_serial = botaoImagem(QPixmap(os.path.join('recursos', 'monitorSerialFoco.png')), self)
        btn_monitor_serial.setFixedSize(50, 50)

        layout.setContentsMargins(5, 5, 5, 0)
        espacador_vertical = QSpacerItem(0, 500000000, QSizePolicy.Minimum, QSizePolicy.Expanding)
        layout.addWidget(btn_compilar)
        layout.addWidget(btn_compilar_e_carregar)
        layout.addWidget(btn_novo)
        layout.addWidget(btn_abrir)
        layout.addWidget(btn_salvar)
        layout.addWidget(btn_monitor_serial)
        layout.addItem(espacador_vertical)

        self.show()


class botaoImagem(QAbstractButton):
    def __init__(self, pixmap, parent=None):
        super(botaoImagem, self).__init__(parent)
        self.pixmap = pixmap

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(event.rect(), self.pixmap)

    def sizeHint(self):
        return QSize(50, 63)

The expected result is as follow(this screenshot is a previous version, developed in Java, thus it includes a few features not implemented yet): 预期结果如下(此屏幕快照是使用Java开发的先前版本,因此包含一些尚未实现的功能): 截图

Don't create "container" widgets, they aren't needed. 不要创建“容器”小部件,不需要它们。 Add the widgets directly to your layouts. 将小部件直接添加到布局中。

Your problem is that although you are setting up layouts, you are adding the container widgets to the parent without a layout, and thus the container won't scale. 您的问题是,尽管您正在设置布局,但是您是将容器小部件添加到没有布局的父级中,因此容器无法缩放。

ie unstead of 即与

container = QWidget(self)
container.setStyleSheet("background:#252525")
editor = QPlainTextEdit(container)
layout.addWidget(container, 0, 1, 9, 9)

write

editor = QPlainTextEdit(self)
editor.setStyleSheet("background:#252525")
layout.addWidget(editor, 0, 1, 9, 9)

Update: Here's how to do it for Menu 更新:这是Menu

layout = QVBoxLayout(self)
self.setFixedWidth(60)
self.setStyleSheet("background-color: '#5cb50d';")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM