在pyqt设计器中创建的对象上的findChild

时间:2017-11-06 15:38:51

标签: python qt pyqt pyqt4

我的pyqt存在以下问题:

假设我在Qt Designer中创建了一个对象并将其保存为.ui文件。然后我使用pyuic将其转换为.py文件。因为我想将一个新模块集成到一个给定的程序中,这是最受欢迎的方式(因为稍后.ui文件将在启动时自动转换为.py文件)。

如果我查看我的.py文件,我会在窗口中看到以下内容:

class Ui_SubWindow(object):
    def setupUi(self, SubWindow):
        SubWindow.setObjectName(_fromUtf8("SubWindow"))
        ....

我有一个RemoteWindow类作为MainWindow,其中SubWindow已启动:

class RemoteWindow(QtGui.QMainWindow):
  def __init__(self, subcore):
    super(RemoteWindow, self).__init__(subcore.core.gui)
    self.subcore = subcore
    self.ui = Ui_SubWindow()

现在我有一个核心计划:

class SubCore(object):
  def __init__(self, core, identity, number):
    ...
    self.gui = RemoteWindow(self)
    self.newController = NewController(self.gui)

和新的控制器类:

class NewController(object):
  def __init__(self, subwindow):
    self.subwindow = subwindow
    self.ui = subwindow.ui
从我的控制器

我想在那个窗口上调用.findChild()

submitFrame = self.ui.findChild(QtGui.QFrame, "frameSubmit")

,但我得到的是:

AttributeError: 'Ui_SubWindow' object has no attribute 'findChild'

我认为这是因为类Ui_SubWindow不是某个QObject的子类而是对象的子类,我是对的吗?

self.uisubwindow.ui相同,其中subwindowRemoteWindow的实例,其.ui类的Ui_SubWindow参数为> public partial class Form1 : Form { public Form1() { InitializeComponent(); this.KeyDown += this.Form1_KeyDown; } Canon playerIcon = new Canon(25, 15, 450, 595); Alien alien = new Alien(30, 30, 0, 0); private void Form1_Load(object sender, EventArgs e) { Timer timer = new Timer(); timer.Interval = 10; timer.Tick += new EventHandler(timer_Tick); timer.Start(); } private void timer_Tick(object sender, EventArgs e) { draw(); } private void draw() { // Set up canvas: Graphics spaceInvanders; spaceInvanders = this.picCanvas.CreateGraphics(); spaceInvanders.Clear(Color.Black); // Draw player icon (canon): SolidBrush playerBrush = new SolidBrush(Color.White); spaceInvanders.FillRectangle(playerBrush, playerIcon.getPosX(), playerIcon.getPosY(), playerIcon.getWidth(), playerIcon.getHeight()); // Draw aliens: SolidBrush alienBrush = new SolidBrush(Color.Green); spaceInvanders.FillRectangle(alienBrush, Convert.ToSingle(alien.getPosX()), Convert.ToSingle(alien.getPosY()), alien.getWidth(), alien.getHeight()); // Allow aliens to move: alien.move(); } // Method used to handle the movement of the canon: private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Left) // Move left { playerIcon.move(1, 0, this.picCanvas.Width); } else if (e.KeyCode == Keys.Right) // Move right { playerIcon.move(2, 0, this.picCanvas.Width); } else if (e.KeyCode == Keys.Up) // Fire weapon { } } } }

有没有机会让pyuic或Qt Designer让这个SubWindow成为QObject的孩子,没有操纵自动生成的.py文件?

3 个答案:

答案 0 :(得分:1)

您根本不需要使用 std::vector<double> v(10'000'007, 0.5); ,因为pyuic会自动在findChild对象中为Qt Designer中定义的所有小部件创建属性。属性名称取自ui。所以你需要的只是:

objectName

答案 1 :(得分:0)

Qt Designer创建了一个设计,即一个用于填充主窗口小部件的类,因此内部窗口小部件不是此类的子窗口,如果要使用findChild,则必须对返回此类的窗口小部件执行此操作。调用方法setupUi。

class RemoteWindow(QtGui.QMainWindow):
  def __init__(self, subcore):
    super(RemoteWindow, self).__init__(subcore.core.gui)
    self.subcore = subcore
    self.ui = Ui_SubWindow()
    self.ui.setupUi(self) # Here the widget is filled
    [...]

然后你应该按照以下方式使用它:

class NewController(object):
  def __init__(self, subwindow):
    self.subwindow = subwindow
    submitFrame = self.subwindow.findChild(QtGui.QFrame, "frameSubmit")
    [...]

答案 2 :(得分:0)

我最近不得不做同样的事情,并留在这里,以防有人看到此链接与其他相关链接。 https://stackoverflow.com/a/62340205/1621381

for name, obj in dict(self.__dict__).items():
        # print(str(name) + str(obj))
        obj_type = str(obj).strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
        # obj_type = str(obj).strip("<").rsplit(" ")[0]
        # print(obj_type)
        # obj_type = obj_str.strip("<PyQt5").rsplit(" ")[0].replace(".", '', 1)
        label_name = "self." + str(name)
        try:
            label_name = self.findChild(eval(obj_type), name)
            print(str(label_name) + ' created')
        except:
            pass
        if not isinstance(obj_type, QObject):
            continue