class NewReminderWindow(QWidget): def __init__(self): QWidget.__init__(self) self.resize(500, 250) self.setWindowTitle('Add New Reminder') self.new_reminder_text_input = QPlainTextEdit() self.new_reminder_text_input.setFixedHeight(90) self.new_reminder_text_input.setFixedWidth(500) self.cancel_button = QPushButton('Cancel') self.cancel_button.setFixedWidth( self.cancel_button.minimumSizeHint().width()) self.cancel_button.clicked.connect(self.on_cancel_clicked) self.save_button = QPushButton('Save') self.save_button.setFixedWidth( (self.save_button.minimumSizeHint().width())) self.v_box = QVBoxLayout() self.h_box1 = QHBoxLayout() self.h_box1.addWidget(self.new_reminder_text_input, alignment=Qt.AlignHCenter) self.h_box2 = QHBoxLayout() self.h_box2.addWidget(self.cancel_button) self.h_box2.addWidget(self.save_button) self.v_box.addLayout(self.h_box1) self.v_box.addLayout(self.h_box2) self.setLayout(self.v_box) @Slot() def on_cancel_clicked(self): self.hide() self.new_reminder_text_input.clear()
def setPlainTextEditHeight(ptxt: QtWidgets.QPlainTextEdit, nRows: int): """ Sets the height of a plain text edit box to a set number of rows. """ pdoc = ptxt.document() fm = QtGui.QFontMetrics(pdoc.defaultFont()) margins = ptxt.contentsMargins() rowHeight = fm.lineSpacing() height = (rowHeight * nRows + 2 * (pdoc.documentMargin() + ptxt.frameWidth()) + margins.top() + margins.bottom()) ptxt.setFixedHeight(height)
class QFileBrowse(QWidget): def __init__(self, subfolder_name='QR Codes', *args, **kwargs): super().__init__(*args, **kwargs) self.layout = QGridLayout() self.button = QPushButton("Change save location") self.subfolder = QCheckBox("Create subfolder") self.subfolder.stateChanged.connect(self.update_display) self.button.clicked.connect(self.browse) self.subfolder_name = subfolder_name self.dialog = QFileDialog() self._base_path = pathlib.Path(".").expanduser().resolve() # output path display self.path_display = QPlainTextEdit() # make bold font = self.path_display.font() font.setWeight(QFont.Bold) self.path_display.setFont(font) # configure height self.path_display.setFixedHeight(QFontMetrics(font).height() * 2) self.path_display.setLineWrapMode(QPlainTextEdit.NoWrap) # styles self.path_display.setReadOnly(True) self.path_display.setFrameStyle(QFrame.Box | QFrame.Sunken) self.path_display.setTextInteractionFlags(Qt.TextSelectableByMouse) self.path_display.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed) self.path_display.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.path_display.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # add widgets to layout self.layout.addWidget(self.button, 0, 0) self.layout.addWidget(self.subfolder, 0, 1) self.layout.addWidget(self.path_display, 1, 0, 1, 2) self.layout.setMargin(0) self.setLayout(self.layout) self.update_display() @property def save_path(self): if self.subfolder.isChecked(): return self._base_path / self.subfolder_name return self._base_path def browse(self): path = self.dialog.getExistingDirectory() if path != '': self._base_path = pathlib.Path(path).expanduser().resolve() self.update_display() def update_display(self): scrollbar = self.path_display.horizontalScrollBar() old_scroll = scrollbar.value() scrolled_to_end = old_scroll == scrollbar.maximum() self.path_display.setPlainText(str(self.save_path)) if scrolled_to_end: scrollbar.setValue(scrollbar.maximum()) else: scrollbar.setValue(old_scroll)