2016-03-29 40 views
1

我最近一直在使用QTCreator,而且我爱上了ATM。不幸的是,我想使用它与Python,但我一直在遇到问题。我遇到的最大问题是找到我的应用程序上下文的子项返回None。无法在QObject上调用findChild(QObject,'child')。退货无

main.py

import sys 

from PyQt5.QtCore import QUrl, QSize, QObject 
from PyQt5.QtGui import QGuiApplication 
from PyQt5.QtQuick import QQuickView 


LOGIN_SCREEN_SIZE = QSize(640, 350) 


def main(): 
    application = QGuiApplication(sys.argv) 

    login_window = QQuickView() 
    login_window.setSource(QUrl('loginscreen.qml')) 

    login_window.setMinimumSize(LOGIN_SCREEN_SIZE) 
    login_window.setMaximumSize(LOGIN_SCREEN_SIZE) 

    login_window.show() 

    ## vvvv Returns None ## 
    print(login_window.findChild(QObject, 'white_background')) 

    # login_window.rootObject().open_main_app.connect(app) 

    sys.exit(application.exec_()) 


def app(username, password): 
    print("YAY") 

    main_window = QQuickView() 
    main_window.show() 


if __name__ == '__main__': 
    main() 

loadingscreen.qml

import QtQuick 2.4 
import QtQuick.Controls 1.5 

Item { 
    id: window 
    x: 0 
    height: 350 
    visible: true 
    transformOrigin: Item.Center 
    rotation: 0 

    signal open_main_app(string username, string password) 

    Rectangle { 
     id: white_background 
     x: 0 
     y: 0 
     width: 640 
     height: 350 
     color: "#ffffff" 
     border.width: 0 


     Rectangle { 
      id: green_background 
      x: 0 
      y: 0 
      width: 640 
      height: 138 
      color: "#30945d" 
     } 

     Text { 
      id: login_text 
      x: 0 
      y: 0 
      width: 640 
      height: 124 
      color: "#b3d756" 
      text: qsTr("LOGIN") 
      styleColor: "#f60000" 
      style: Text.Normal 
      font.bold: true 
      verticalAlignment: Text.AlignVCenter 
      horizontalAlignment: Text.AlignHCenter 
      font.pixelSize: 112 
     } 


     TextField { 
      id: username_box 
      x: 149 
      y: 160 
      width: 384 
      height: 30 
      placeholderText: qsTr("Steve") 
     } 


     TextField { 
      id: password_box 
      x: 149 
      y: 215 
      width: 384 
      height: 30 
      echoMode: 2 
      placeholderText: qsTr("qwerty123") 
     } 


     Label { 
      id: label1 
      x: 40 
      y: 166 
      text: qsTr("Username") 
     } 


     Label { 
      id: label2 
      x: 40 
      y: 221 
      text: qsTr("Password") 
     } 


     Button { 
      id: login_button 
      x: 548 
      y: 308 
      text: qsTr("Go") 

      onClicked: 
      { 
       window.open_main_window(username_box.text, password_box.text) 

       //Qt.quit() << Only works when launching from C++ file 
      } 
     } 

    } 

} 

white_background不是唯一一个我测试过,他们都没有工作。我无法理解问题是什么。作为一个侧面说明,我所拥有的信号也不会发送。我想尝试手动连接,但我不能,因为findChild不起作用。

回答

1

这是因为findChild不寻找id的元素。相反,您应该在qml文件中使用objectName

重写你的white_background矩形为:

Rectangle { 
     id: white_background 
     objectName: "white_background" 
     x: 0 
     y: 0 
     width: 640 
     height: 350 
     color: "#ffffff" 
     border.width: 0 
     ... 
} 
+0

你好,谢谢你的回复。我已经移动了,但是当我还在尝试时,我确实尝试了objectName,并且这也不起作用。我发誓我说,我在这个问题上尝试过,但我想我没有,这太长了。 – 0BobTheJanitor

0

碰到这个问题,而试图找出如何从PyQt的访问TextInput元素的值。按照ismael的建议尝试设置objectName并且它工作正常。所以我想ismael是对的。

这是我尝试过的工作。

@pyqtSlot(result = str) 
def saveEdits(self): 
    comments = appLabel.findChild(QObject, "commentsTextArea").property("text") 
    print(comments) 
    return "save edits success" 

其中appLabel = QQuickView()commentsTextAreaTextInput元件的objectName

相关问题