Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在QTabWidget中使用QQuickWidget在中心显示QML?_Python_Pyqt_Qml_Pyqt5 - Fatal编程技术网

Python 如何在QTabWidget中使用QQuickWidget在中心显示QML?

Python 如何在QTabWidget中使用QQuickWidget在中心显示QML?,python,pyqt,qml,pyqt5,Python,Pyqt,Qml,Pyqt5,我正在尝试使用QQuickWidget显示自定义listView。 因为我需要两个listView:一个用于搜索结果,一个用于队列, 我使用QTabWidget在这两个QQuickWidget之间切换 我还没有完成listView的制作(因为我是这方面的初学者) 所以我临时编写了QML来测试它是否像我上面键入的那样工作。 当我运行我的应用程序时,QTabWidget中的QML意外显示 它应该像下面的QQuickWidget一样显示一个黄色矩形和文本,但它显示的文本在左上角对齐,没有黄色矩形 当应

我正在尝试使用QQuickWidget显示自定义listView。 因为我需要两个listView:一个用于搜索结果,一个用于队列, 我使用QTabWidget在这两个QQuickWidget之间切换

我还没有完成listView的制作(因为我是这方面的初学者) 所以我临时编写了QML来测试它是否像我上面键入的那样工作。 当我运行我的应用程序时,QTabWidget中的QML意外显示

它应该像下面的QQuickWidget一样显示一个黄色矩形和文本,但它显示的文本在左上角对齐,没有黄色矩形

当应用程序被执行时

[我尝试过的事情] 我认为这是因为我将QQuickWidget放在每个选项卡的QWidget中,所以我将这些QWidget升级为具有QQuickWidget但没有任何更改的自定义页面

main.py

导入系统 从PyQt5.QtCore导入* 从PyQt5.QtWidgets导入* 从PyQt5导入uic 从PyQt5.QtQuickWidgets导入QQuickWidget 从myresulttab导入myresulttab 从myqueuetab导入myqueuetab #加载用户界面文件 form_class=uic.loadUiType(“mainWindowUI.ui”)[0] 类窗口类(QMainWindow,form_类): 定义初始化(自): super()。\uuuu init\uuuuu() self.setupUi(self) self.testQuickWidget.setSource(QUrl.fromLocalFile(“resultTabQml.qml”)) 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) myWindow=WindowClass() myWindow.show() app.exec() mainWindowUI.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>555</width>
    <height>821</height>
   </rect>
  </property>
  <property name="font">
   <font>
    <family>NanumGothic</family>
    <pointsize>10</pointsize>
    <weight>50</weight>
    <italic>false</italic>
    <bold>false</bold>
   </font>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <property name="styleSheet">
   <string notr="true"/>
  </property>
  <widget class="QWidget" name="centralwidget">
   <property name="styleSheet">
    <string notr="true"/>
   </property>
   <layout class="QGridLayout" name="gridLayout" rowstretch="50,5,45,0" columnstretch="70,0">
    <property name="verticalSpacing">
     <number>15</number>
    </property>
    <item row="2" column="0" colspan="2">
     <widget class="QQuickWidget" name="testQuickWidget">
      <property name="resizeMode">
       <enum>QQuickWidget::SizeRootObjectToView</enum>
      </property>
     </widget>
    </item>
    <item row="0" column="0" colspan="2">
     <widget class="QTabWidget" name="tabWidget">
      <property name="currentIndex">
       <number>0</number>
      </property>
      <property name="elideMode">
       <enum>Qt::ElideNone</enum>
      </property>
      <widget class="myResultTab" name="resultTab">
       <property name="font">
        <font>
         <family>NanumGothic</family>
         <pointsize>10</pointsize>
         <weight>50</weight>
         <italic>false</italic>
         <bold>false</bold>
        </font>
       </property>
       <attribute name="title">
        <string>검색창</string>
       </attribute>
      </widget>
      <widget class="myQueueTab" name="queueTab">
       <property name="autoFillBackground">
        <bool>false</bool>
       </property>
       <attribute name="title">
        <string>QUEUE</string>
       </attribute>
      </widget>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QLabel" name="console">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Minimum">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="font">
       <font>
        <family>NanumGothic</family>
        <pointsize>10</pointsize>
        <weight>50</weight>
        <italic>false</italic>
        <bold>false</bold>
       </font>
      </property>
      <property name="toolTip">
       <string extracomment="프로그램이 당신에게 보내는 편지입니다. 다정하게 대해주세요 :)"/>
      </property>
      <property name="text">
       <string>resultTabQML.qml should be displayed like this!</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>555</width>
     <height>20</height>
    </rect>
   </property>
   <widget class="QMenu" name="menu_F">
    <property name="title">
     <string>파일(&amp;F)</string>
    </property>
   </widget>
   <widget class="QMenu" name="menu_E">
    <property name="title">
     <string>편집(&amp;E)</string>
    </property>
    <addaction name="separator"/>
    <addaction name="separator"/>
   </widget>
   <addaction name="menu_F"/>
   <addaction name="menu_E"/>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>QQuickWidget</class>
   <extends>QWidget</extends>
   <header location="global">QtQuickWidgets/QQuickWidget</header>
  </customwidget>
  <customwidget>
   <class>myResultTab</class>
   <extends>QWidget</extends>
   <header>myresulttab.h</header>
   <container>1</container>
  </customwidget>
  <customwidget>
   <class>myQueueTab</class>
   <extends>QWidget</extends>
   <header>myqueuetab.h</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
 <slots>
  <slot>inquireDataBtnPressed()</slot>
  <slot>insertDataBtnPressed()</slot>
  <slot>deleteDataBtnPressed()</slot>
  <slot>updateDataBtnPressed()</slot>
  <slot>getDataFromApiBtnPressed()</slot>
 </slots>
</ui>
myQueueTab.py

从PyQt5.QtCore导入*
从PyQt5.QtWidgets导入*
从PyQt5.QtQuickWidgets导入QQuickWidget
类myQueueTab(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
布局=QVBoxLayout(自身)
queueQuickWidget=QQuickWidget(qrl.fromLocalFile(“queueTabQml.qml”))
layout.addWidget(queueQuickWidget)
self.setLayout(布局)
resultabqml.qml

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Item{
    Rectangle{
        id:rect1
        color:"yellow"
        anchors.fill: parent
        Text {
            id: sampleText1
            text: qsTr("sampleText1")
            font.family: "Arial"
            font.pointSize: 50
            anchors.rightMargin: 0
            anchors.bottomMargin: 0
            anchors.fill: parent
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
        }
    }

}
/*##^##
Designer {
    D{i:0;autoSize:true;height:480;width:640}
}
##^##*/

您必须将属性设置为
SizeRootObjectToView
,以便根根据QQuickWidget的大小展开:

import os

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QVBoxLayout, QWidget
from PyQt5.QtQuickWidgets import QQuickWidget

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class myResultTab(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        filename = os.path.join(CURRENT_DIR, "resultTabQml.qml")
        resultQuickWidget = QQuickWidget(QUrl.fromLocalFile(filename))
        resultQuickWidget.setResizeMode(QQuickWidget.SizeRootObjectToView)
        layout.addWidget(resultQuickWidget)
导入操作系统
从PyQt5.QtCore导入QUrl
从PyQt5.qtwidts导入QVBoxLayout,QWidget
从PyQt5.QtQuickWidgets导入QQuickWidget
CURRENT_DIR=os.path.dirname(os.path.realpath(uu文件_uu))
类MyResultTable(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
布局=QVBoxLayout(自身)
filename=os.path.join(当前目录,“resultTabQml.qml”)
resultQuickWidget=QQuickWidget(qrl.fromLocalFile(文件名))
resultQuickWidget.setResizeMode(QQuickWidget.SizerootObject视图)
layout.addWidget(resultQuickWidget)


如果不进行设置,则resizeMode将使用默认的
SizeViewToRooObject
,该项将不考虑视图的大小,因此该项的大小为0x0,因此黄色矩形的大小也将相同,并且文本将居中于位置(0,0)这在OP提供的图像中显示。

请提供一个我很抱歉,但您希望我提供代码,以便您可以重现错误?如果是这样的话,我该怎么做呢?确切地说,我需要所有重现问题所需的代码,这样我就可以分析问题所在,并可能为您提供解决方案。我的建议是,您制作一份项目副本,删除不必要的代码:regex、数据库等,但即使您的代码也是可行的,然后编辑您的问题并添加所有由上述结果产生的.qml、.ui和.py。我添加了、ui文件并删除了不必要的代码。看看吧:)