2016-03-27 30 views
1

vs Python3 farklı arayüz Bir piton dosyası ve QML dosyasına sahip gösterir.FileDialog QML

bir FileDialog yüklemek için QML dosyasında bir düğmesi vardır. Doğrudan qmlscene test.qml kullandığımda, FileDialog tamam. Ama ben python3 main.py kullandığınızda, FileDialog garip ve ben onun tarafından bir dosya seçemiyorum. Lütfen bana nasıl düzeltileceğini söyle.

Bu normaldir dosya iletişim kutusudur:

enter image description here

Ve bu garip dosya iletişim kutusudur:

testi:

enter image description here

kod şudur .qml

import QtQuick 2.4 
import QtQuick.Dialogs 1.2 
import QtQuick.Controls 1.3 
import QtQuick.Controls.Styles 1.3 
import QtQuick.Layouts 1.1 

Rectangle { 
     width: 400 
     height:30 



     Button { 
       id: save 
       text: "save" 
       onClicked: { 
         fileDialogLoader.item.open() 
        } 
      } 
     Loader { 

       id: fileDialogLoader 
       sourceComponent: fileDialog_com 
      } 

     Component{ 
       id: fileDialog_com 


       FileDialog { 
         id: fileDialog 
         title: "select a file" 
         nameFilters: ["pdf files(*.pdf)"] 
         selectExisting: false 

         onAccepted: { 
           console.log(" you choose: "+ fileDialog.fileUrls) 
          } 
        } 
      } 
    } 

main.py

#!/usr/bin/env python 
# encoding: utf-8 

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

class MyMain(QObject): 
    pass 


if __name__ == '__main__': 
    path = 'test.qml' 
    app = QGuiApplication([]) 
    view = QQuickView() 
    con = MyMain() 
    context = view.rootContext() 
    context.setContextProperty("con",con) 
    view.engine().quit.connect(app.quit) 
    view.setSource(QUrl(path)) 
    view.show() 
    app.exec() 

cevap

1

"garip" dosya iletişim QML tamamen yazılmış olan bir varsayılan uygulamasıdır. Qt o platformun doğal iletişim ya ya yaratamaz use this as a fallback ne zaman yerleşik QFileDialog. widget tabanlı olmadığını QGuiApplication, kullandığınız çünkü

senin örneğin QML yedek özelliğini kullanır nedeni vardır. QApplication'a geçerseniz, örneğiniz beklendiği gibi çalışır:

# from PyQt5.QtGui import QGuiApplication 
from PyQt5.QtWidgets import QApplication 
... 
# app = QGuiApplication([]) 
app = QApplication([])