Issue
I'm creating a code editor in PyQt5. I'm using a QGridLayout in the center widget and building the UI from there. The problem I'm facing is that the QPlainTextEdit's and the Menu don't fill the available space on the center widget. I tried setting the size policy to expanding and tried adapting the stretch parameters of the QGridLayout.
I'm running the code in python 2.7 and PyQt 5. Using the PyCharm IDE.
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
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):
Solution
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.
i.e 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
layout = QVBoxLayout(self)
self.setFixedWidth(60)
self.setStyleSheet("background-color: '#5cb50d';")
Answered By - user3419537
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.