如何实现 Qt Designer 中定义的信号/槽

发布于 2024-12-24 19:35:51 字数 863 浏览 2 评论 0原文

我正在尝试将按钮的 click() 信号连接到我自己的函数。该按钮位于我使用 QT Designer 创建的小部件中。我使用 QUiLoader 加载 .ui 文件,如下所示:

class MyWidget(QtGui.QMainWindow):
    def __init__(self, *args):  
        QtGui.QMainWindow.__init__(self, *args)

        loader = QtUiTools.QUiLoader()
        file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
        file.open(QtCore.QFile.ReadOnly)
        self.myWidget = loader.load(file, self)
        file.close()

        self.setCentralWidget(self.myWidget)

        btn = self.myWidget.findChild(QtGui.QPushButton, "HelloWorldButton")
        btn.clicked.connect(self.slot1)        
    
    def slot1(self):
        print "Received"

这是连接到按钮 clicked() 信号的正确方法吗?我发现我可以直接在 Qt Designer 中连接信号和插槽,但是如何在代码中准备并进行此类连接? 附带问题:上面的代码可以工作,但主窗口显示的大小错误。如何确保它以正确的尺寸显示?我应该在最小高度/宽度限制的情况下执行此操作吗?

I am trying to connect the click() signal of a button to my own function. The button is in a widget that I created with QT Designer. I load the .ui file with QUiLoader like so:

class MyWidget(QtGui.QMainWindow):
    def __init__(self, *args):  
        QtGui.QMainWindow.__init__(self, *args)

        loader = QtUiTools.QUiLoader()
        file = QtCore.QFile("pyside_ui_qtdesigner_form_test.ui")
        file.open(QtCore.QFile.ReadOnly)
        self.myWidget = loader.load(file, self)
        file.close()

        self.setCentralWidget(self.myWidget)

        btn = self.myWidget.findChild(QtGui.QPushButton, "HelloWorldButton")
        btn.clicked.connect(self.slot1)        
    
    def slot1(self):
        print "Received"

Is this the correct way to connect to button clicked() signal? I see that I can wire up signals and slots directly in Qt Designer, but how do I prepare and get to such wire-ups in the code?
Side question: The code above works, but the main window shows in the wrong size. How do I ensure that it appears in the right size? Should I do this with minimum height/width constraints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-12-31 19:35:51

使用信号和槽编辑模式直接连接预定义的Qt信号到预定义的 Qt 插槽。

因此,对于简单对话框上的“关闭”按钮,您只需将连接从按钮拖到对话框中,选择 clicked() 信号和 reject() 槽,单击“确定”,然后就没有什么可做的了。

对于您想要自己定义的信号和/或槽,您可以直接在代码中建立连接,也可以通过 Qt Designer 间接建立连接。您的示例已经很好地演示了第一个选项,但是连接可以更加简单和干净地完成,如下所示:

self.myWidget.HelloWorldButton.clicked.connect(self.slot1)

在 Qt Designer 中进行连接更加复杂。首先,您必须切换到信号/槽编辑模式(通过按F4或单击工具栏按钮),然后在发送器/接收器对象之间拖动连接。这将打开“配置连接”对话框,您可以在其中单击编辑按钮之一打开信号/槽编辑器,然后单击加号按钮手动添加您将在自己的代码中定义的任何信号/槽的签名(请参阅截图如下)。完成此操作后,您可以关闭第二个对话框并在第一个对话框中建立相关连接。但需要明确的是:这不会在 Qt Designer 本身内创建任何信号和槽 - 它所做的只是允许您指定代码名称以后可以用。此外,只能手动将信号/槽添加到自定义类,例如顶级表单和推广的小部件

screenshot


至于您的主窗口具有“错误的大小”:很难从您显示的代码中看出,但是这可能是因为您没有在正在加载的小部件中设置布局。

顺便说一句:您使用QUiLoader有什么具体原因吗?使用pyuic4编译Python模块要灵活得多,并且您可以从生成的代码中学到很多东西。

编辑

对我来说,在主窗体上设置布局可以解决您所说的调整大小问题。

如果您不知道该怎么做:在设计器中,右键单击主窗体的空白部分,然后从菜单中选择布局/网格中的布局(还有一个按钮在工具栏上)。

完成此操作后,调整表单大小将自动拉伸它以适合所包含的小部件。

Use Signals and Slots Editing Mode for connecting predefined Qt signals directly to predefined Qt slots.

So for "Close" button on a simple dialog, you can just drag a connection from the button to the dialog, select the clicked() signal and the reject() slot, click "OK", and there would be nothing more to do.

For signals and/or slots you want to define yourself, you can either make the connections directly in code, or indirectly via Qt Designer. Your example already demonstrates the first option fairly well, but the connection could be done much more simply and cleanly, like this:

self.myWidget.HelloWorldButton.clicked.connect(self.slot1)

Making the connection in Qt Designer is more complicated. Firstly, you must switch to Signal/Slot editing mode (by pressing F4 or clicking the toolbar button) and then drag a connection between the sender/receiver objects. This will open the Configure Connection dialog where you can click one of the Edit buttons to open the Signal/Slot Editor, and then click the plus buttons to manually add the signatures of any signals/slots you will define in your own code (see the screenshot below). Once this is done, you can close the second dialog and make the relevant connection(s) in the first dialog. But to be clear: this will not create any signals and slots within Qt Designer itself - all it does is allow you to specify names your code can use later. Also, it's only possible to manually add signals/slots to custom classes, such as the top-level form and promoted widgets.

screenshot


As for your main window having the "wrong size": it's difficult to tell from the code you've shown, but it may be because you did not set a layout in the widget that you are loading.

BTW: is there a specific reason you're using QUiLoader? Compiling python modules using pyuic4 is much more flexible, and you can learn a lot from the code that is generated.

EDIT

For me, setting a layout on the main form cures the resizing problem you are talking about.

If you don't know how to do that: in Designer, right-click on a blank part of the main form, and then select Layout/Layout in a Grid from the menu (there's also a button on the toolbar for this).

Once you've done that, resizing the form will automatically stretch it to fit the contained widgets.

睡美人的小仙女 2024-12-31 19:35:51

我编辑了 .ui 文件:

  1. 关闭 QT 设计器
  2. 在插槽/信号区域编辑 .ui
  3. 运行它

I edited the .ui file:

  1. Close QT designer
  2. Edit the .ui in the slot/signal area
  3. Run it
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文