سؤال

I'm building an application using PySide and would like the ability to load widgets from separate .ui files. 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. 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

<?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. This forces it to act like an usual QWidget. Use QMainWindow::addDockWidget instead.

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top