简体   繁体   English

从单独的ui文件加载到主窗口的停靠小部件

[英]dock widget loaded from separate ui file to main window

I'm building an application using PySide and would like the ability to load widgets from separate .ui files. 我正在使用PySide构建应用程序,并且希望能够从单独的.ui文件加载窗口小部件。 below is some code I've tried out but it wont let me dock the dock widget loaded separately from the main window to the main window. 下面是一些我已经尝试过的代码,但是它不能让我将分别从主窗口加载的停靠小部件停靠在主窗口中。 The main window ui file contains a simple main window and a few items already docked into it, the dock_widget ui contains a dock widget and a few buttons inside. 主窗口ui文件包含一个简单的主窗口和一些已经停靠在其中的项目,dock_widget ui包含一个停靠小部件和内部的一些按钮。 When I double click the dock widget's bar once its loaded it seems to 'dock' to nothing and when i drag it over the main window it wont dock. 当我双击停靠小部件的栏一旦加载,它似乎“停靠”为零,当我将其拖动到主窗口上时,它不会停靠。 The main window definitely accepts dock widgets as there are some defined inside it that behave normally. 主窗口肯定会接受停靠窗口小部件,因为其中定义了一些正常运行的窗口小部件。

#!/usr/bin/python

# Import PySide classes
import sys
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtUiTools import QUiLoader


def load_ui(ui_file, parent=None):
    loader = QUiLoader()
    file = QFile(ui_file)
    file.open(QFile.ReadOnly)
    myWidget = loader.load(file, None)
    myWidget.show()
    file.close()
    myWidget.show()
    return myWidget

# Create a Qt application
app = QApplication(sys.argv)


# Create a Label and show it
main_window = load_ui("ui/main_window.ui")
dock_widget = load_ui("ui/console.ui", main_window)

# Enter Qt application main loop
app.exec_()
sys.exit()

console.ui console.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>DockWidget</class>
 <widget class="QDockWidget" name="DockWidget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>542</width>
    <height>261</height>
   </rect>
  </property>
  <property name="contextMenuPolicy">
   <enum>Qt::DefaultContextMenu</enum>
  </property>
  <property name="windowTitle">
   <string>DockWidget</string>
  </property>
  <widget class="QWidget" name="dockWidgetContents">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QTextEdit" name="textEdit_2"/>
    </item>
    <item>
     <widget class="QTextEdit" name="textEdit"/>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Any thoughts on what I should do differently 关于我应该做些什么的任何想法

You should not call QDockWidget::show . 您不应该调用QDockWidget::show This forces it to act like an usual QWidget. 这迫使它像通常的QWidget一样工作。 Use QMainWindow::addDockWidget instead. 使用QMainWindow::addDockWidget代替。

def load_ui(ui_file, parent=None):
    loader = QUiLoader()
    file = QFile(ui_file)
    file.open(QFile.ReadOnly)
    myWidget = loader.load(file, None)
    file.close()
    return myWidget

main_window = load_ui("ui/main_window.ui")
dock_widget = load_ui("ui/console.ui", main_window)
main_window.show()
main_window.addDockWidget(Qt.LeftDockWidgetArea, dock_widget)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM