QLineEdit setCornerWidget width - python

I'm adding a QLineEdit to the menu bar with setCornerWidget. However, when setting a size with either setFixedWidth or setMinimumWidth, the QLineEdit is clipping through the window:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = self.menuBar()
search = QLineEdit(self)
search.setFixedWidth(200)
menu.setCornerWidget(search)

It seems to be a bug, a workaround is to use a QWidget as a container:
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
menu = self.menuBar()
search = QLineEdit()
search.setFixedWidth(200)
container = QWidget()
layout = QVBoxLayout(container)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(search)
menu.setCornerWidget(container)

Related

Mutual resizing of widgets

I'm implementing an application, where a side widget can expand/shrink, so an another widget must shrink/expand. (Or the side widget can be shown over that widget, it doesn't matter, I accept both implementations). It can looks like that:
Here is a part of my code:
class AppView:
def __init__(self):
self._mainWindow = QDialog(None)
self._schedule = ScheduleView(self._mainWindow)
self._schedule.setMinimumWidth(25)
self._schedule.setMaximumWidth(250)
self._tutorial = TutorialView(self._mainWindow)
self._schedule.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
self._tutorial.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
layout = QHBoxLayout()
layout.addWidget(self._schedule)
layout.addWidget(self._tutorial)
layout.setSpacing(0)
layout.setContentsMargins(0, 0, 0, 1)
self._mainWindow.setLayout(layout)
class TutorialView(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self._presenter = TutorialPresenter(self)
self.reqReprSections.connect(self.setModel)
self.reqReprTopics.connect(self.setModel)
self._widget = QQuickWidget(self)
self._widget.rootContext().setContextProperty('tutorialView', self)
self._widget.setSource(QUrl('modules/manual/manualForm/TutorialForm.qml'))
class ScheduleView(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self._presenter = SchedulePresenter(self)
self._widget = QQuickWidget(self)
self._widget.setResizeMode(QQuickWidget.SizeViewToRootObject)
self._widget.rootContext().setContextProperty('scheduleView', self)
self._widget.rootContext().setContextProperty('groupsModel', self)
self._widget.setSource(QUrl('modules/schedule/scheduleForm/ScheduleForm.qml'))
How can I do such resizes in code?
To get that behavior you can use a QHBoxLayout by embedding a rotated button in the middle of the side widgets. You must change the left widget's size policy so that it does not expand.
To implement the rotated button you must override the paintEvent method, in addition to modifying the size policy so that it expands vertically and not horizontally.
class ShrinkExpandButton(QPushButton):
def __init__(self, *args, **kwargs):
QPushButton.__init__(self, *args, **kwargs)
self.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
self.setFixedWidth(2*self.fontMetrics().height())
def paintEvent(self, event):
painter = QStylePainter(self)
painter.rotate(-90)
painter.translate(-self.height(), 0)
option = QStyleOptionButton()
self.initStyleOption(option)
size = option.rect.size()
size.transpose()
option.rect.setSize(size)
painter.drawControl(QStyle.CE_PushButton, option)
class ShrinkExpandWidget(QWidget):
def __init__(self, leftWidget, rightWiget, text, parent=None):
QWidget.__init__(self, parent)
button = ShrinkExpandButton(text, self)
self.setLayout(QHBoxLayout())
self.layout().setSpacing(0)
self.layout().addWidget(leftWidget)
self.layout().addWidget(button)
self.layout().addWidget(rightWiget)
leftWidget.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Expanding)
button.clicked.connect(lambda: leftWidget.setVisible(not leftWidget.isVisible()))
Example:
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
listWidget = QListWidget()
for i in range(20):
listWidget.addItem("{}".format(i))
tableWidget = QTableWidget()
tableWidget.setColumnCount(10)
tableWidget.setRowCount(20)
for i in range(tableWidget.rowCount()):
for j in range(tableWidget.columnCount()):
tableWidget.setItem(i, j, QTableWidgetItem("({}, {})".format(i, j)))
listWidget.setFixedWidth(240)
w = ShrinkExpandWidget(listWidget, tableWidget, "Shrink - Expand")
w.resize(720, 480)
w.show()
sys.exit(app.exec_())
Output:

How to uncheck all radio buttons from another class

I want to uncheck some radio buttons in class A by pushing a button in class B.
My example codes are as below:
import sys, os
import PyQt4
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Widget1(QWidget):
def __init__(self, bridge, parent=None):
super().__init__()
self.bridge = bridge
self.grid = QGridLayout()
self.radiobtn = QRadioButton()
self.grid.addWidget(self.radiobtn, 0, 0)
class Widget2(QWidget):
def __init__(self, parent = None):
super().__init__()
self.grid = QGridLayout()
self.pushbtn = QPushButton()
self.grid.addWidget(self.pushbtn,0, 0)
class Tab1Layout(QWidget):
def __init__(self, parent = None):
super().__init__()
self.grid = QGridLayout()
self.group2 = Widget2()
self.group1 = Widget1(self.group2, self)
self.grid.addWidget(self.group1, 0, 0)
self.grid.addWidget(self.group2, 1, 0)
class Tabs(QTabWidget):
def __init__(self, parent = None):
super().__init__()
self.tab1 = Tab1Layout()
self.addTab(self.tab1, 'Tab1')
self.show()
def main():
app = QApplication(sys.argv)
main = Tabs()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
As my code is now, it has two problems:
The radio button and the push button don't show up
Let's say the radio button and the push button finally show up: I want that when I click the push button in Widget2, the radio button in Widget1 should be unchecked if it is already checked.
Please note that I'm already linking Widget1 and Widget2 by putting a second argument bridge in Widget1. The reason I did this is because there's some other functions in my original project. So please don't alter this part if possible.
The buttons don't show up because you have not added the grid layouts to any of the widgets. So for all three grid layouts, do either this:
self.grid = QGridLayout()
self.setLayout(self.grid)
or this:
self.grid = QGridLayout(self)
To check/uncheck the radio button using a button click, do this:
class Tab1Layout(QWidget):
def __init__(self, parent = None):
...
self.group2.pushbtn.clicked.connect(self.group1.radiobtn.toggle)

Wrong layout in QMainWindow?

I am getting wrong layout in PyQT5. What am I doing wrong? Is there some predefined small field size or similar? I created main window as QMainWindow and inside it a widget as central widget. This is how it looks like:
class Main(QWidget):
"""The main widget with label and LineEdit"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize the UI of the main widget"""
self.mySourceLabel = QLabel("Select your file:")
self.mySourceLine = QLineEdit()
self.mySourceLine.setPlaceholderText("File name here")
# Set layout
grid = QGridLayout()
#grid.setSpacing(5)
grid.addWidget(self.mySourceLabel, 0, 0)
grid.addWidget(self.mySourceLine, 1, 0)
self.setLayout(grid)
class MyApp(QMainWindow):
"""Main application class"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize UI of an application"""
# main window size, title
self.setGeometry(400, 300, 400, 300)
self.setWindowTitle("Version upgrade ")
# create instance of a class Main
self.main = Main(self)
# create central widget, create grid layout
centralWidget = QWidget()
centralLayout = QGridLayout()
centralWidget.setLayout(centralLayout)
When you pass the parent to a QWidget this will locate a position with respect to its parent and generate widgets like the ones you have obtained, to solve this, layouts are used, QMainWindow is a special QWidget since it has predefined elements, so it already has a layout:
In QMainWindow the widget must be added to the centralwidget with the setCentralWidget function, in your case:
class MyApp(QMainWindow):
"""Main application class"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
[...]
centralWidget = Main(self)
self.setCentralWidget(centralWidget)
complete code:
class Main(QWidget):
"""The main widget with label and LineEdit"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize the UI of the main widget"""
self.mySourceLabel = QLabel("Select your file:")
self.mySourceLine = QLineEdit()
self.mySourceLine.setPlaceholderText("File name here")
# Set layout
grid = QGridLayout()
#grid.setSpacing(5)
grid.addWidget(self.mySourceLabel, 0, 0)
grid.addWidget(self.mySourceLine, 1, 0)
self.setLayout(grid)
class MyApp(QMainWindow):
"""Main application class"""
def __init__(self, parent=None):
super().__init__(parent)
self.initUi()
def initUi(self):
"""Initialize UI of an application"""
# main window size, title
self.setGeometry(400, 300, 400, 300)
self.setWindowTitle("Version upgrade ")
# create central widget, create grid layout
centralWidget = Main(self)
self.setCentralWidget(centralWidget)
Screenshot:

Widget on corner QTabBar is not in corner

I have a pushbutton to add a tab in a QTabWidget. But when I change the button's size, it's not in the corner anymore. So how can I bring it to the corner like in the first picture?
Not change size:
Change size:
Here is my code:
from PyQt4 import QtGui, QtCore
class PlaylistTable(QtGui.QWidget):
def __init__(self):
super(PlaylistTable, self).__init__()
self.playlistTable = QtGui.QTableWidget(self)
self.playlistTable.setFrameShape(QtGui.QFrame.NoFrame)
self.playlistTable.setFrameShadow(QtGui.QFrame.Sunken)
self.playlistTable.setLineWidth(0)
self.playlistTable.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.playlistTable.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
self.playlistTable.setHorizontalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
self.playlistTable.setShowGrid(True)
self.playlistTable.setGridStyle(QtCore.Qt.SolidLine)
self.playlistTable.setWordWrap(True)
self.playlistTable.setCornerButtonEnabled(True)
self.playlistTable.verticalHeader().setVisible(False)
class CustomTabWidget(QtGui.QTabWidget):
"""Tab Widget that that can have new tabs easily added to it."""
def __init__(self, parent=None):
super(CustomTabWidget, self).__init__(parent)
# QtGui.QTabWidget.__init__(self, parent)
# Tab Bar
self.tab = QtGui.QTabBar()
self.setTabBar(self.tab)
# Properties
self.setMovable(True)
self.setTabsClosable(True)
self.plusButton = QtGui.QPushButton("+")
self.plusButton.setFixedSize(QtCore.QSize(22, 22))
self.setCornerWidget(self.plusButton)
# Signals
self.connect(self.plusButton, QtCore.SIGNAL('clicked()'), self.addTab)
# self.tab.plusClicked.connect(self.addTab)
self.tab.tabMoved.connect(self.tab.moveTab)
self.tabCloseRequested.connect(self.removeTab)
def addTab(self):
string = QtCore.QString.fromUtf8("Playlist")
tab = PlaylistTable()
super(CustomTabWidget, self).addTab(tab, string)
class AppDemo(QtGui.QMainWindow):
def __init__(self):
super(AppDemo, self).__init__()
self.centralwidget = QtGui.QWidget(self)
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setContentsMargins(0, -1, 0, -1)
self.playlist_manager = CustomTabWidget(self.centralwidget)
self.horizontalLayout.addWidget(self.playlist_manager)
self.playlist_manager.addTab()
self.setCentralWidget(self.centralwidget)
self.show()
# end class AppDemo
def main():
import sys
app = QtGui.QApplication(sys.argv)
w = AppDemo()
w.setWindowTitle('AppDemo')
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I'm guessing the reason is this, from QTabWidget Documentation:
The geometry of the widget is determined
based on the widget's sizeHint() and the style().
If you print the sizeHint() of the QPushButton you will see that width never goes below a certain value.
I've found an alternative is to use a QToolButton which can do everything (even more) a QPushButton does.
self.plusButton = QtGui.QToolButton(self)
self.plusButton.setText("+")

QScroll area within QStackedLayout

I have a QMainWindow with a QStackedWidget as the central widget, and having been switching between layouts by changing the current widget of this central widget.
This has been working fine, but now I am trying to make one of these possible layouts scrollable and this is the result:
Code for MainWindow class:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
#-- On load open main menu
self.showMenu()
# Go to the menu screen
def showMenu(self):
widget_menu = WidgetMain(self)
widget_menu.btnConfigu.clicked.connect(self.showConfig)
self.central_widget.addWidget(widget_menu)
self.central_widget.setCurrentWidget(widget_menu)
self.resize(420,350)
# Go to the config screen
def showConfigu(self):
widget_configu = WidgetOptions(self)
self.central_widget.addWidget(widget_configu)
self.central_widget.setCurrentWidget(widget_configu)
Code for WidgetOptions class:
class WidgetOptions(QtGui.QWidget):
def __init__(self, parent=None):
super(WidgetOptions, self).__init__(parent)
#Container Widget
widget = QtGui.QWidget()
layoutRightContainer = QtGui.QVBoxLayout()
for _ in range(11):
btn = QtGui.QPushButton("test")
layoutRightContainer.addWidget(btn)
widget.setLayout(layoutRightContainer)
widget
self.setFixedHeight(300)
#Scroll Area Properties
scroll = QtGui.QScrollArea()
scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
scroll.setWidgetResizable(True)
scroll.setWidget(widget)
layoutMain = QtGui.QHBoxLayout()
layoutMain.addWidget(widget)
I've tried numerous tweaks to the set sizes, and making different containers non-/resizable. What works outside of a StackedWidget doesn't seem to work within one. There also doesn't seem to be any questions on SO with such a situation.
self.setLayout(layoutMain)
I solved this by making the WidgetOptions class itself an extension of QScrollArea rather than a QWidget containing the QScrollArea
Code for WidgetOptions class:
class WidgetOptions(QtGui.QScrollArea):
def __init__(self, parent=None):
super(WidgetOptions, self).__init__(parent)
layoutLeft = QtGui.QVBoxLayout()
self.btnVariables = QtGui.QPushButton("Variables")
self.btnGroups = QtGui.QPushButton("Groups")
layoutLeft.addWidget(self.btnVariables)
layoutLeft.addWidget(self.btnGroups)
#Container Widget
widget = QtGui.QWidget()
layoutRightContainer = QtGui.QVBoxLayout()
for _ in range(11):
btn = QtGui.QPushButton("test")
layoutRightContainer.addWidget(btn)
widget.setLayout(layoutRightContainer)
#Scroll Area Properties
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setWidgetResizable(True)
self.setWidget(widget)
self.setWidget(widget)

Categories