2016-03-27 6 views
1

Ich habe eine Python-Datei und eine QML-Datei.FileDialog zeigt eine andere Schnittstelle mit Python3 vs QML

Es gibt eine Schaltfläche in der qml-Datei, um einen FileDialog zu laden. Wenn ich direkt qmlscene test.qml verwende, ist der FileDialog in Ordnung. Aber wenn ich python3 main.py benutze, ist der FileDialog seltsam, und ich kann keine Datei damit auswählen. Bitte sag mir, wie ich es beheben kann.

Dies ist der normale Datei-Dialog:

enter image description here

Und das ist der seltsame Datei-Dialog:

enter image description here

Der Code ist der folgende:

Test .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() 

Antwort

1

Der "seltsame" Datei-Dialog ist eine Standardimplementierung, die vollständig in QML geschrieben wurde. Qt wird use this as a fallback, wenn es weder das native Dialogfeld der Plattform noch das integrierte QFileDialog erstellen kann.

Der Grund, warum Ihr Beispiel das qml-Fallback verwendet, liegt darin, dass Sie QGuiApplication verwenden, das nicht auf einem Widget basiert. Wenn Sie zu QApplication wechseln, wird Ihr Beispiel wie erwartet funktionieren:

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