在另一个 *.ui 文件的框架/小部件中加载整个 *ui 文件 [英] Load whole *ui file in an frame/widget of another *.ui file

查看:47
本文介绍了在另一个 *.ui 文件的框架/小部件中加载整个 *ui 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为家庭项目开发一个小型用户界面.我用 QT-Designer 创建了一个 *.ui 文件.这是我的主窗口,带有一些导航按钮、标签等.现在,当我单击导航按钮时,我正在努力在主窗口的框架或小部件中加载另一个 *.ui(例如带有内容).我正在使用 pyqt4,我必须在树莓派上用 python 实现它.

I am developing a small user interface for a home-project. I created a *.ui file with QT-Designer. This is my main window with some navigations buttons, labels and so on. Now I am struggeling to load another *.ui (for example with content) in the frame or widget of the main window when I click on navgation buttons. I am using pyqt4 and I have to implement it with python on an raspberry pi.

我也使用了搜索,但我没有找到解决我的问题的可行解决方案.也许真的很容易,但对我来说很难.

I also used the search but I did not find an working solution for my problem. Maybe it is really easy but for me it is difficultly.

*重要:我不想重新组织按钮和标签!我想在主窗口中的小部件或框架中加载整个 *.ui 文件!

*Important: I don't want to reorganize buttons and labels! I want to load a whole *.ui file in a widget or frame which is in the main window!

这是我的代码示例:以下是我的main.py

Here my code examples: The following is my main.py

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *

def buttonKlick1close():
start.close()

start = loadUi("start.ui")
start.connect(start.pushButton, SIGNAL("clicked()") , buttonKlick1close)

# there are other "signal-connections" for my navigation buttons but I 
# integreated all exactly the same like above. 
# actually when I click to another navigation button it will open an "def" 
# with "window2.showFullscreen()" and so on.... 

start.showFullScreen()
app.exit(app.exec_())

现在,当我点击导航按钮时,它总是会打开 7-8 个窗口,因此我还需要在每个窗口上使用整个导航按钮.我的目标是创建一个窗口,其中包含我的导航按钮和一个框架/小部件,我可以在其中提升/加载其他 *ui 文件.或者有没有其他没有框架/小部件的好的解决方案?

Now, when I click on my navagtion buttons it will always open 7-8 windows and so I also need my whole navgation buttons on every window. My aim is to create ONE window with my navigation buttons and an frame/widget in it where I can promote/load the other *ui files. Or is there another good solution without frames/widgets?

推荐答案

首先,我的解决方案的基础是推广一个widget,为此我将按照以下方式构建项目:

First of all, the basis of my solution is the promotion of a widget, for this I will structure the project in the following way:

├── main.py
├── pages
│   ├── energypage.py
│   ├── fanpage.py
│   ├── homepage.py
│   └── statuspage.py
└── ui
    ├── energy.ui
    ├── fan.ui
    ├── home.ui
    ├── main.ui
    └── status.ui

页面的 .ui 将基于 Widget 模板,但主要的将使用 MainWindow 模板(MainWindow 允许有 ToolBars、StatusBar、DockWidgets、menuBar 等,所以我选择它作为主要).

the .ui of the pages will be based on the Widget template but the main one will use the MainWindow template (MainWindow allows to have ToolBars, StatusBar, DockWidgets, menuBar, etc so I choose it as main).

由于 .ui 无法自行提升,因此将创建调用设计并遵循类似结构的类,但您可以添加更多功能,例如在 homepage.py 的情况下:

Since the .ui can not be promoted by itself will create classes that invoke the design and follow a similar structure but you can add more functionalities, for example in the case of homepage.py:

import os
from PyQt4 import QtGui, uic


current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/home.ui"))


class HomeWidget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)


if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = HomeWidget()
    w.show()
    sys.exit(app.exec_())

main.ui 左侧有按钮,右侧有 QStackedWidget:

The main.ui has the buttons on the left side and the QStackedWidget on the right side:

通过选择选项插入页面 -> 在通过右键单击 QStackedWidget 打开的菜单的当前页面之后添加每个页面

Each page was added by selecting the option insert page-> after current page of the menu that opens by right clicking on the QStackedWidget

然后它会被提升到使用pages文件夹中的widget:

Then it will be promoted to use the widget that is in the pages folder:

然后在 main 中将按钮与适当的索引相关联:

Then in the main you associate the buttons with the appropriate indexes:

ma​​in.py

import os
from PyQt4 import QtGui, uic
from functools import partial

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

class MainWidget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        buttons = (self.homebutton, self.statusbutton, self.fanbutton, self.energybutton)
        for i, button in enumerate(buttons):
            button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    app.setStyle("fusion")
    w = MainWidget()
    w.show()
    sys.exit(app.exec_())

完整示例在此处.

这篇关于在另一个 *.ui 文件的框架/小部件中加载整个 *ui 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆