开发者

Connecting QPushButton's sizes

开发者 https://www.devze.com 2023-03-06 08:30 出处:网络
开发者_开发技巧I have a grid-like view with QPushButtons. The buttons content isn\'t static, so the column sizes expands dynamically. And I want same column size for each column for aesthetical reason
开发者_开发技巧

I have a grid-like view with QPushButtons. The buttons content isn't static, so the column sizes expands dynamically. And I want same column size for each column for aesthetical reasons.

Now I'm using an adjust_sizes function after showing the widget, because I noticed If I call it before showing the widget, nothing changes. But my solutions seems ugly to me. Is there a better way for that?

My function:

def adjust_sizes(self):
    max_width=0
    for i in self.buttons:
        if i.geometry().width() > max_width : 
            max_width=i.geometry().width()
    for i in self.buttons: i.setMinimumSize(max_width,0)

And thats how I use it:

g=launcher()
g.show()
g.adjust_sizes()

Is there a better way for doing that?

Edit: As for clarify here is the desired screenshots(Look at Raceyman's answer):

Desired Look:

Connecting QPushButton's sizes

Here is what I get:

Connecting QPushButton's sizes

My System:

Arch Linux x86_64, Python 3.2 or 2.7.1, PyQt 4.8.4, Qt 4.7.3


My apologies if this isn't really an answer (it's a bit long for comments), but I'm wondering if the version of PyQt you're using might be part of the cause. I'm using 4.8.3 (I think), and when I build a dialog this way using QGridLayout the columns, by default, are all equal sizes. Here is a (very, very, very crude) example:

from PyQt4 import QtGui, QtCore
import sys

class launcher(QtGui.QDialog):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        mainlayout = QtGui.QGridLayout()
        buttons = [['a', 'aaaa', 'aaaaaaa', 'a'], 
                   ['b', 'bbbbbbbbbb', 'bbbbb', 'b'], 
                   ['c','c','c', 'c']]
        for i, buttonrow in enumerate(buttons):
            for j, button in enumerate(buttonrow):                
                mainlayout.addWidget(QtGui.QPushButton(button), i, j)
        self.setLayout(mainlayout)
        self.exec_()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    g = launcher()
    g.show()

Here is a screenshot of the resultant dialog:

Connecting QPushButton's sizes

Hopefully this is similar to what you are trying to do.

0

精彩评论

暂无评论...
验证码 换一张
取 消