This is my code
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class Window(Gtk.Window):
def __init__(self):
super().__init__(title="GUi")
self.ui()
def ui(self):
self.connect("destroy", Gtk.main_quit)
button1 = Gtk.Button(label="Button 1")
button2 = Gtk.Button(label="Button 2")
grid = Gtk.Grid()
self.add(grid)
grid.attach(button1, 0, 0, 1, 1)
grid.attach(button2, 0, 3, 2, 2)
self.show_all()
window = Window()
Gtk.main()
and this is my output but I have mentioned the button 2 to appear in the 3rd column and gave it row and column span of 2 but both of these are not working as expected what am I doing wrong?
I am getting the impression that you are try trying to place the buttons beside each other with a space between them. check out this link where it mentions grid to get a better idea of what arguments for grid.attach(a,b,c,d,e) are.
(a) The child parameter is the Gtk.Widget to add.
(b) left is the column number to attach the left side of child to.
(c) top indicates the row number to attach the top side of child to.
(d, e) width and height indicate the number of columns that the child will span, and the number of rows that the child will span, respectively.
https://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html
you wont be able to put a space without using something like.
grid.set_row_spacing(20)
grid.set_column_spacing(20)
or maybe cell padding or button alignment. for more on that look at this link
https://athenajc.gitbooks.io/python-gtk-3-api/content/gtk-group/gtkgrid.html
keep in mind when thinking about sizing and positioning are inherited from classes. Positioning is tricky because you are can't really make a space between.
Related
I'm trying to make a widget which contains many other widgets and I keep having problems with resizing the window: the widget keeps expanding even if I "tell" it not to. Here is my minimal example:
from PyQt5 import QtWidgets, QtGui, QtCore
class CustomWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.layout = QtWidgets.QGridLayout()
self.button1 = QtWidgets.QPushButton("Button A")
self.button2 = QtWidgets.QPushButton("Button B")
self.label1 = QtWidgets.QLabel("Long label that can span multiple columns")
self.layout.addWidget(self.button1, 0, 0)
self.layout.addWidget(self.button2, 0, 1)
self.layout.addWidget(self.label1, 1, 0, 1, 2)
self.setLayout(self.layout)
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.cw = CustomWidget()
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.cw)
self.setLayout(self.layout)
self.show()
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
app = QtWidgets.QApplication([])
win = App()
status = app.exec_()
This code does work however if I resize the window then all the buttons and labels get spread out over the screen which is not what I want.
I've tried:
Setting a fixed size: doesn't work because the label text can be different lengths and I want the widget to resize accordingly (but stay as small as possible)
self.setFixedSize(self.size()) doesn't do anything and sometimes makes it worse
self.setSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum) or any other size policy seems to do nothing
TL;DR I want my widget to shrink even if there's empty space but I don't want to set a fixed size.
EDIT:
I have partially solved the problem by passing in alignment=QtCore.Qt.AlignLeft to all the self.layout.addWidget calls. It doesn't totally get rid of the problem however it may be a step in the right direction.
If you want to keep those widgets as small as possible, you can add a row and column stretch to the layout, set for a row/column index greater than the most bottom-right layout coordinate used.
In your case, you have two rows and two columns, so it's enough to set the stretch for the third row and column:
self.layout.setColumnStretch(2, 1)
self.layout.setRowStretch(2, 1)
Obviously, you can set it for a very high index, so that if you have to add more widgets you don't have to care about it.
If you want to keep those widgets in the center, just add them starting from the second row and column and set the row/column stretch for the first row/column too:
self.layout.setColumnStretch(0, 1)
self.layout.setRowStretch(0, 1)
self.layout.addWidget(self.button1, 1, 1)
self.layout.addWidget(self.button2, 1, 2)
self.layout.addWidget(self.label1, 2, 1, 1, 2)
self.layout.setColumnStretch(3, 1)
self.layout.setRowStretch(3, 1)
Note that you can also use the size policy, but you have to use Maximum, not Minimum, and you also need to add widgets with the correct alignment.
I got same issue. I resolved the issue by setting the values of minimum and maximum size to equal like both to 100
If you want to shrink the widget set minimum to 0 and set maximum value to the size you want start the display. It will not expand but shrink
From what you are describing it seems you just don't want to use a layout.
Make a widget with a layout to hold the buttons and label. Place this widget in the main window without using a layout, instead place with setGeometry.
Something like this,
from PyQt5 import QtWidgets, QtGui, QtCore
class CustomWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.resize(400, 100)
# Create a widget to hold your buttons
self.fixwidget = QtWidgets.QWidget(self)
self.layout = QtWidgets.QGridLayout()
self.button1 = QtWidgets.QPushButton("Button A")
self.button2 = QtWidgets.QPushButton("Button B")
self.label1 = QtWidgets.QLabel("Long label that can span multiple columns")
self.layout.addWidget(self.button1, 0, 0)
self.layout.addWidget(self.button2, 0, 1)
self.layout.addWidget(self.label1, 1, 0, 1, 2)
# set the layout inside the widget
self.fixwidget.setLayout(self.layout)
# place the widget with setGeometry
height = self.fixwidget.sizeHint().height()
width = self.fixwidget.sizeHint().width()
xpos, ypos = 5, 5
self.fixwidget.setGeometry(QtCore.QRect(xpos, ypos, width, height))
QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create("Fusion"))
app = QtWidgets.QApplication([])
win = CustomWidget()
win.show()
status = app.exec_()
Notice that not using a layout is, in most cases, not advised as it makes managing the window harder.
I've googled around but I'm not able to find a solution to my problem.
I have a QTableWidget with 2 columns and what I'm trying to do is to make them visible to the whole widget without the horizontal scrollbar to appear.
With a picture it should be all clear:
I have used Qt Designer to create the UI and some code to fill all the widgets and other stuff.
So, first I resized th2 2 columns to the content with:
self.statTable.resizeColumnToContents(0)
self.statTable.resizeColumnToContents(1)
and it works, but then the Widget is not resizing to the 2 columns width.
This has a very easy solution in PyQt5. All you need to do is set the size adjust policy on the table when initialising the UI, and it will automatically resize to fit the contents. This can either be done via Qt Designer (in the QAbstractScrollArea section of the Property Editor), or programmatically, like this:
self.statTable.setSizeAdjustPolicy(
QtWidgets.QAbstractScrollArea.AdjustToContents)
You then just need to do:
self.statTable.resizeColumnsToContents()
whenever the table is re-populated.
For PyQt4, everything has to be calculated manually, and a few hacks are also required to get completely consistent results. The demo script below works okay for me, but YMMV:
import random
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.table = QtGui.QTableWidget(5, 2, self)
self.button = QtGui.QPushButton('Populate', self)
self.button.clicked.connect(self.populate)
layout = QtGui.QGridLayout(self)
layout.addWidget(self.table, 0, 0)
layout.addWidget(self.button, 1, 0)
layout.setColumnStretch(1, 1)
def populate(self):
words = 'Red Green Blue Yellow Black White Purple'.split()
length = random.randint(2, len(words))
self.table.setRowCount(random.randint(3, 30))
for column in range(self.table.columnCount()):
for row in range(self.table.rowCount()):
item = QtGui.QTableWidgetItem(' '.join(
random.sample(words, random.randint(1, length))))
self.table.setItem(row, column, item)
self.table.setVisible(False)
self.table.verticalScrollBar().setValue(0)
self.table.resizeColumnsToContents()
self.table.setVisible(True)
self.setTableWidth()
def setTableWidth(self):
width = self.table.verticalHeader().width()
width += self.table.horizontalHeader().length()
if self.table.verticalScrollBar().isVisible():
width += self.table.verticalScrollBar().width()
width += self.table.frameWidth() * 2
self.table.setFixedWidth(width)
def resizeEvent(self, event):
self.setTableWidth()
super(Window, self).resizeEvent(event)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(700, 150, 800, 400)
window.show()
sys.exit(app.exec_())
For the autoadjust settings on the table widget in Qt Designer, you canlook in the object inspector for the table widget you can drill down to it as shown below.
(PyQt5)
The issue for me is that my cells in the right-most column are multi-line (QPlainTextEdit), and I wanted word-wrapping... but I also wanted this right-most column to extend to fill the parent container.
It seems you can do everything you need in PyQt5 designer, in the Property editor for your QTableView:
in the "QTableView" section check "wordWrap"
in the "QAbstractScroll" section check "AdjustToContents" (as mentioned by Crap Phone)
in the "Header" section check "horizontalHeaderStretchLastSection"
This then generates the following sort of code:
self.history_table_view.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContentsOnFirstShow)
self.history_table_view.horizontalHeader().setStretchLastSection(True )
"word wrap = True" appears to be the default setting, so nothing is shown, but it would be this:
self.history_table_view.setWordWrap(True)
Use your_tablewidget.resizeColumnsToContents() every single time after you call your_tablewidget.setItem(). You don't need any other setting.
you know just try, tableWidget.resize(1200, 600) *you can change resolution but it is your answer...
My goal is to get a ULC that has a column of TextCtrls that will dynamically resize the row as the user types, so if there is a much better way, say so.
Here is what I've tried:
The ExpandoTextCtrl is exactly what I want. I have a working example, so I know I'm able to implement it correctly, I'm after a column of these:
import wx
import sys
import wx.lib.expando as ex
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
text = "\"I\'ll Be Missing You\" is a song recorded by American rapper Puff Daddy and American singer Faith Evans, featuring R&B group 112, in memory of fellow Bad Boy Records artist Christopher \"The Notorious B.I.G.\" Wallace, who was gunned down on March 9, 1997. --Wikipedia"
self.edit_text = ex.ExpandoTextCtrl(self, value = text, size = (200,50))
self.edit_text.SetMaxHeight(sys.maxint)
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="expando Demo")
panel = TestPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = TestFrame()
app.MainLoop()
As you add or remove lines it adjusts the height on the fly, for any character (not just the number of newlines or something simple like that).
If I add it to a cell in an UltimateListCtrl, it has a static size which is approximately column width and 2 lines of text visible, so it won't even instantiate showing the entire text, but it also won't resize either.
I have the same problem with other types of Windows is the list. I wrote a button that changes its size when you click it. This code of mine (I can post if you want but it feels redundant) runs perfectly in a panel of its own or in a Sizer with other widgets, etc, but in the ULC it will only instantiate at the original size and never changes with calls to button.SetSize().
I have researched bug reports for the ULC but haven't seen anything relevant and not fixed. I have tried calling the ULC's Refresh(), Update(), and Show(False/True) methods, and all of the above on the parent Panel and the Frame with no success.
Here is the code, based around a common example of the ULC online:
import wx
import sys
from wx.lib.agw import ultimatelistctrl as ULC
import wx.lib.expando as ex
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.sizes = self.size_gen()
font = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
boldfont = wx.SystemSettings_GetFont(wx.SYS_DEFAULT_GUI_FONT)
boldfont.SetWeight(wx.BOLD)
boldfont.SetPointSize(12)
self.ultimateList = ULC.UltimateListCtrl(self, agwStyle = wx.LC_REPORT
| wx.LC_VRULES
| wx.LC_HRULES
| ULC.ULC_HAS_VARIABLE_ROW_HEIGHT)
info = ULC.UltimateListItem()
info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_CHECK
info._image = []
info._format = 0
info._kind = 1
info._text = "Artist Name"
self.ultimateList.InsertColumnInfo(0, info)
info = ULC.UltimateListItem()
info._format = wx.LIST_FORMAT_RIGHT
info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT | ULC.ULC_MASK_FONT
info._image = []
info._text = "Title"
info._font = boldfont
self.ultimateList.InsertColumnInfo(1, info)
info = ULC.UltimateListItem()
info._mask = wx.LIST_MASK_TEXT | wx.LIST_MASK_IMAGE | wx.LIST_MASK_FORMAT
info._format = 0
info._text = "Genre"
info._font = font
info._image = []
self.ultimateList.InsertColumnInfo(2, info)
self.ultimateList.InsertStringItem(0, "Newsboys")
self.ultimateList.SetStringItem(0, 1, "Go")
self.ultimateList.SetStringItem(0, 2, "Rock")
text = "\"I\'ll Be Missing You\" is a song recorded by American rapper Puff Daddy and American singer Faith Evans, featuring R&B group 112, in memory of fellow Bad Boy Records artist Christopher \"The Notorious B.I.G.\" Wallace, who was gunned down on March 9, 1997. --Wikipedia"
self.ultimateList.InsertStringItem(1, "Puffy")
edit_text = ex.ExpandoTextCtrl(self.ultimateList, value = text, size=(200,50))
edit_text.SetMaxHeight(sys.maxint)
self.ultimateList.SetItemWindow(1, col=1, wnd=edit_text, expand=True)
self.ultimateList.SetStringItem(1, 2, "Pop")
self.ultimateList.InsertStringItem(2, "Family Force 5")
self.button = wx.Button(self.ultimateList, label='button', size =(200,200))
self.button.Bind(wx.EVT_BUTTON, self.on_button)
self.ultimateList.SetItemWindow(2, 1, self.button, expand=True)
#self.ultimateList.SetStringItem(2, 1, "III")
self.ultimateList.SetStringItem(2, 2, "Crunk")
self.ultimateList.SetColumnWidth(0, 150)
self.ultimateList.SetColumnWidth(1, 200)
self.ultimateList.SetColumnWidth(2, 100)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.ultimateList, 1, wx.EXPAND)
self.SetSizer(sizer)
def on_button(self, event):
self.button.SetSize(self.sizes.next())
def size_gen(self):
sizes = [(150,200),(200,200),(80,80)]
index = 0
while True:
yield sizes[index]
index = index + 1
if index > 2:
index = 0
########################################################################
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="MvP UltimateListCtrl Demo")
panel = TestPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.App(False)
frame = TestFrame()
app.MainLoop()
EDIT
I've tried several more approaches now. I got rid of all the column formatting and replaced those 3 blocks with simplelist.InsertColumn(index, label) calls. The most useful thing I did was remove the expand=True from the ULC.SetItemWindow() call. This seems to have returned control of the button's width (but not height) to the button. Since it starts at 150Wx200H, when I call next and it changes to 200x200, the button overflows into the next cell to the right. When I call next again, commanding an 80x80 size, the button shrinks to 150x200, its unable to be set smaller than its initial size.
If I initialize the button smaller than I ever need, say 50x30, then I can set all the sizes (80,80;200,200) correctly in both dimensions, but the button then overflows into its neighbours to the right and below.
You can see that the list is not refreshing any of the rows.
You can also see that the widget maintains its original upper left corner position (maybe this is correct, but I don't think so)
If I add list.Refresh/DoLayout/Layout/Update to the on_button, it has no effect.
Dragging or repositioning the window (top level Frame) has no effect.
ULC.SendSizeEvent has no effect.
Next Idea
I also tried deleting the entire row and inserting a new button of new size, like so:
def on_button(self,event):
new_size = self.sizes.next()
print new_size
l = self.ultimateList
label = l.GetItemWindow(1, 1).GetLabel()
l.DeleteItem(1)
self.button = wx.Button(self.ultimateList, label=label, size=new_size)
self.button.Bind(wx.EVT_BUTTON, self.on_button)
l.InsertStringItem(1, 'Family Farce 5')
l.SetItemWindow(1,1,self.button)
l.SetStringItem(1,2,'Crunk')
I don't think this is an ok strategy, as destroying and rebuilding an edit_text on every keystroke sounds like it would have a whole lot of problems, but to be fair I haven't tried it yet.
Anyway, with the button I can call it and resizes correctly. The problem is that the ULC doesn't redraw subsequent rows based on the new height, or even the initial height, but the default height of a row. It does draw the recreated row with the button the correct height.
This time (deleting the line and adding a new line) I noticed resizing the window (top level Frame) forced the redraw, so I added SendSizeEvent to on_button and now the button works perfectly.
Apparently I've worked out how to replace a row with one of a new size.
So the question is still about Windows in a ULC, I can dynamically resize a widget, but how can I force the ULC to redraw itself after sizing a widget?
Another way to say it: Why does SendSizeEvent force a refresh after inserting a new item into the list, but not after modifying an existing item? I could subclass the ULC and extend a particular method or property if I knew which one to do. I've tried looking at the source but I can't make heads or tails of it.
First: ULC creates the line heights only once and if the height is already there when painting a line, it wont do it again.
Furthermore: When you add a new window, the size gets written into the line object. This will never be done again and cannot be triggered by any means manually.
However:
If you adjust UltimateListCtrl like this it will work.
Function GetWindowSize of class UltimateListItemData (line 2863):
def GetWindowSize(self):
""" Returns the associated window size. """
wnd = self._wnd
if wnd.GetSizer(): # the window is a complex one hold by a sizer
size = wnd.GetBestSize()
else: # simple window, without sizers
size = wnd.GetSize()
print(size)
return size
In your code you need to invalidate the old line heights, so it gets recalculated:
def on_button(self, event):
self.button.SetSize(self.sizes.next())
self.ultimateList._mainWin.ResetLineDimensions(True)
self.ultimateList._mainWin.RecalculatePositions()
Please note, that this modifies ULC and accesses private variables.
Lokla
Using following code, I have the problems, that the buttons "Log Leeren" and "Auto Scroll" change their height, when I resize the window. They should be exactly one text-line high and the rest of the viewport should be used by the scrolledWindow
What do I need to change:
class ConsoleLogWindow(Gtk.Window):
def __init__(self, server):
self.log = server["log"];
Gtk.Window.__init__(self, title="Meteor Log von %s" % server["name"])
# self.set_icon_from_file("filename")
self.set_size_request(800,500)
table = Gtk.Table(3, 2, False)
self.add(table)
# Should be only one line of thext high
self.button_clear = Gtk.Button(label="Log Leeren")
self.button_scroll = Gtk.Button(label="Auto Scroll")
table.attach(self.button_clear, 2, 3, 1, 2)
table.attach(self.button_scroll, 0, 1, 1, 2)
# should take as much space as is available.
scrollWindow = Gtk.ScrolledWindow()
scrollWindow.set_hexpand(False)
scrollWindow.set_vexpand(True)
self.content_table = Gtk.Table(len(self.log)+1, 4, False)
# self.content_table is filled here.
scrollWindow.add(self.content_table)
table.attach(scrollWindow, 0, 3, 0, 1)
the window class is called in a function like:
def show_errors_menu(self, widget):
print ("Showing Error Menu")
win = ConsoleLogWindow(widget.get_node());
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Don't use Gtk.Table; it does not always respect the expand and align properties of its child widgets. Its replacement since GTK 3.0 has been Gtk.Grid. Using that, you only have to make sure that expand is set to true on the scrolled window and false on the buttons.
In the Qt side, at least, the frameworks consider for every widget a size attribute, a minimumSize attribute and a maximumSize attribute. Setting these three attributes to the same value for height and width makes them unresizable. Java's widgets have a explicit resizable attribute. I don't know how GTK3 prevents resizing, but doing some web search I came across this recipe which makes unresizable windows by extending and adding self.set_resizable(False) which is said to work. Give it a try and let me know!
I have a QHBoxLayout which I add widgets to, one by one.
I alternate by adding a Custom Widget, than a QLabel (repeating).
The QHBoxLayout is owned by a QGroupBox.
However, I notice that the spacing between the Custom Widget and the QLabel is 'irregular' when there are few widgets added to the Layout.
I intend for the QLabels to be centered in the gap between the custom widgets, but the QLabels only approach the centre as more widgets are added.
These screenshots show the two cases.
The desired case (only occuring with lots of widgets in the layout),
and the undesired case (occuring when little widgets are in the layout).
As you can see in the bottom case, the QLabels are not centered between the Custom Widgets. Instead, they are very far to the right!
What is causing this behaviour??
I believe the QGroupBox has a centered (horizontal) alignment and the QLabels have a fixed width of 10 (or 20 for the '->' QLabel) pixels (to avoid ugly overlap).
Any help at all would be greatly appreciated!
Thanks!
Specs:
python 2.7.1
PyQt4
Windows 7
The instantiation of the QHBoxLayout is absolutely normal and similiar to all examples.
Here is the code for the filling of the layout.
for i in range (0,len(Reactants)):
self.WidgetHouse.Reaction_Element_Layout.addWidget(eval('self.OverallContainer_Reactants.Reactant_'+str(i)))
# self.WidgetHouse.Reaction_Element_Layout is the QHBoxLayout
# self.OverallContainer_Reactants.Reactant_'+str(i) is a Custom Widget
if i != (len(Reactants)-1):
tmp = QtGui.QLabel('+')
tmp.setFixedWidth(10)
tmp.setAlignment(QtCore.Qt.AlignCenter)
self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)
else:
tmp = QtGui.QLabel('->')
tmp.setFixedWidth(20)
tmp.setAlignment(QtCore.Qt.AlignCenter)
self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)
EDIT:
Setting a fixed width of the QLabels (tmp.setFixedWidth(10)) is the source of the 'right anchoring'.
However, not setting a fixed width results in the same space being dedicated to QLabels and Custom Widgets in the layout, leading to an overlap of QLabels and Custom Widgets.
Identical to the code above, discluding 'tmp.setFixedWidt(10)'
What can I do to prevent this that's not completely horrible?
Can I move the Labels 'back' from the front?
(Calling .raise_() on all the custom widgets after everything was added to the layout did not work)
(Or will I have to do something like manually calculate the appropriate width of the labels based off the amount of widgets in the layout? Yuck!)
ANOTHER EDIT:
Progress:
I do not change the maximum/minimum (or fixed) width of the QLabels, but I do set their alignment to center.
Instead, I set a minimum width of the custom widget.
This fixes the apparent 'overlapping' (which wasn't really the case) and makes the Labels appear 'more centered'.
However, as you can see, the QLabels still aren't perfectly centered - too far right.
If I don't set a center alignment on the QLabels they are too far left.
What could be the problem now??
(I do not set a maximum width on the labels)
Thanks for all the help so far guys!
Here is a simple example script which is a reasonable approximation of the UI in the question, but without any of the layout issues:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.groupBox = QtGui.QGroupBox(self)
hbox = QtGui.QHBoxLayout(self.groupBox)
length = 3
for index in range(length):
hbox.addWidget(Widget(u'H\u2082O', self))
if index < length - 1:
hbox.addWidget(Label(u'+', self))
else:
hbox.addWidget(Label(u'\u2192', self))
hbox.addWidget(Widget(u'4 H\u2082O', self))
hbox.addWidget(Label(u'+', self))
hbox.addWidget(Widget(u'H\u2084O\u2082', self))
vbox = QtGui.QVBoxLayout(self)
vbox.addWidget(self.groupBox)
vbox.addStretch()
class Label(QtGui.QLabel):
def __init__(self, label, parent=None):
QtGui.QLabel.__init__(self, label, parent)
self.setAlignment(QtCore.Qt.AlignCenter)
class Widget(QtGui.QWidget):
def __init__(self, label, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setMaximumWidth(100)
layout = QtGui.QGridLayout(self)
self.label = QtGui.QLabel(label, self)
self.label.setAlignment(QtCore.Qt.AlignCenter)
layout.addWidget(self.label, 0, 0, 1, 2)
self.lineEdit = QtGui.QLineEdit(self)
layout.addWidget(self.lineEdit, 1, 0, 1, 2)
self.toolButton = QtGui.QToolButton(self)
layout.addWidget(self.toolButton, 2, 0, 1, 1)
self.comboBox = QtGui.QComboBox(self)
layout.addWidget(self.comboBox, 2, 1, 1, 1)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Solution to all problems:
Give the custom widgets a fixed width.
Results in perfectly centered, not 'overlapping' widgets.
:)
Thanks for all the help guys!