作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 MouseArea
中的 QML 中定义了一个按钮(实际上它是一个 ApplicationWindow
) .我设法从 PyQt5 连接到它的 clicked 事件。现在我试图显示一个保存文件对话框,但出现错误:
QWidget: Cannot create a QWidget without QApplication
我的代码如下所示:
from PyQt5.QtCore import QUrl, QObject # pylint: disable-msg=E0611
from PyQt5.QtGui import QGuiApplication, QIcon # pylint: disable-msg=E0611
from PyQt5.QtQml import QQmlApplicationEngine # pylint: disable-msg=E0611
from PyQt5.QtWidgets import QFileDialog # pylint: disable-msg=E0611
def openFile():
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
filename = QFileDialog.getOpenFileName(None,"QFileDialog.getOpenFileName()", "","All Files (*);;Python Files (*.py)", options=options)
print(filename)
def run():
app = QGuiApplication(sys.argv)
app.setWindowIcon(QIcon(resource_path("assets\\images\\icon.ico")))
engine = QQmlApplicationEngine()
engine.load(resource_path("qml\\Window.qml"))
engine.quit.connect(app.quit)
if not engine.rootObjects():
return -1
button = engine.rootObjects()[0].findChild(QObject, "openButton")
button.clicked.connect(openFile)
return app.exec_()
if __name__ == '__main__':
sys.exit(run())
我也试图通过
ApplicationWindow
而不是
None
,但随后我收到一个类型错误:
TypeError: getOpenFileName(parent: QWidget = None, caption: str = '', directory: str = '', filter: str = '', initialFilter: str = '', options: Union[QFileDialog.Options, QFileDialog.Option] = 0): argument 1 has unexpected type 'QWindow'
在我看来这没有多大意义,因为
QMainWindow
继承
QWidget
.
openFile()
显示对话框功能?
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import QtQuick 2.3
ApplicationWindow {
id: mainWindow
visible: true
width: 600
height: 400
Item {
anchors.top: titleBar.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
property bool isMainButtonFocused: false
objectName: "openButton"
signal clicked()
Label {
padding: 5
text: "<b><font color='#fefefe'>Hello World</font></b>"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
font.family: "Helvetica"
font.pointSize: 9
background: Rectangle {
color: mouseAreaOpenFolderButton.containsMouse ? "#777777" : "#333333"
border.width: isMainButtonFocused ? 2 : 1
border.color: "#ffffff"
radius: 5
}
MouseArea {
id: mouseAreaOpenFolderButton
anchors.fill: parent
hoverEnabled: true
onClicked: {
isMainButtonFocused = true
parent.parent.clicked()
}
}
}
}
}
最佳答案
错误非常明显:
# ...
class Helper(QObject):
@pyqtSlot(QUrl)
def read_file(self, url):
filename = url.toLocalFile()
print(filename)
def run():
app = QGuiApplication(sys.argv)
app.setWindowIcon(QIcon(resource_path("assets\\images\\icon.ico")))
engine = QQmlApplicationEngine()
helper = Helper()
engine.rootContext().setContextProperty("helper", helper)
# ...
import QtQuick.Window 2.2
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import QtQuick 2.3
import QtQuick.Dialogs 1.3
ApplicationWindow {
id: mainWindow
visible: true
width: 600
height: 400
Item {
anchors.top: titleBar.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
property bool isMainButtonFocused: false
Label {
padding: 5
text: "<b><font color='#fefefe'>Hello World</font></b>"
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
font.family: "Helvetica"
font.pointSize: 9
background: Rectangle {
color: mouseAreaOpenFolderButton.containsMouse ? "#777777" : "#333333"
border.width: isMainButtonFocused ? 2 : 1
border.color: "#ffffff"
radius: 5
}
MouseArea {
id: mouseAreaOpenFolderButton
anchors.fill: parent
hoverEnabled: true
onClicked: {
// isMainButtonFocused = true
fileDialog.visible = true
}
}
}
}
FileDialog {
id: fileDialog
title: "Please choose a file"
selectedNameFilter: "All Files (*);;Python Files (*.py)"
onAccepted: {
helper.read_file(fileDialog.fileUrls[0])
}
}
}
关于python - 从连接到 QML 的 PyQt5 打开文件对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62530031/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!