我有一个4x4网格,我想将箭头键按下与网格中项目的移动相关联.怎么做到这一点?
这是一个QML示例:
import QtQuick 1.1
Rectangle {
id: main;
width: 500; height: 500;
color: "darkgreen";
property int emptyBlock: 16;
Grid {
id: grid16;
x: 5; y: 5;
width: 490; height: 490;
rows: 4; columns: 4; spacing: 5;
Repeater {
model: 1;
Rectangle {
width: 118; height: 118; color: "darkblue";
}
}
}
Keys.onRightPressed: pressRight();
function pressRight() {
console.log("Left key pressed");
}
focus: true;
}
Run Code Online (Sandbox Code Playgroud)
更新1:感谢sebasgo和alexisdm的答案.如果在网格内移动并不那么容易,为什么我们有move过渡属性[http://qt-project.org/doc/qt-4.8/qml-grid.html#move-prop]
我有一个QML TextEdit元素,我计划附加一些文本并将光标放在最后.我的方法:
import QtQuick 1.1
Rectangle {
color: "black"
anchors.fill: parent
focus: false
TextEdit {
id: txtCommands
color: "lightGreen"
anchors.fill: parent
textFormat: TextEdit.RichText
wrapMode: TextEdit.WordWrap
font.family: "Consolas"
font.pixelSize: 15
focus: true
MouseArea {
anchors.fill: parent
focus: false
}
Keys.onPressed: {
console.log(event.text)
switch (event.key) {
case 16777234: // LEFT
case 16777235: // UP
case 16777237: // DOWN
case 16777236: // RIGHT
event.accepted = true
break;
case 16777220: // Enter
txtCommands.text = txtCommands.text + ">: "
txtCommands.selectAll()
txtCommands.cursorPosition = txtCommands.text.length
break; …Run Code Online (Sandbox Code Playgroud) 我发现了一段视频,其中显示了视频中称为"实时节目"的内容.
这可以在这里看到.
我认为这个想法是在应用程序运行时编辑QML文件.保存更改后,应用程序会立即显示此更改.
这消除了重新启动应用程序以查看更改的需要.
我认为这非常好,因为它加快了工作流程.
但我真的不知道它是如何工作的.我需要做些什么来制作这样的东西才能在Windows环境中运行?
我是Qt的新手,也是我在qt-project.org和其他地方读过的内容.QtQuick似乎是一个很有吸引力的选择,因为它能够在指针和触摸设备上工作.我的问题是让它与c ++一起使用.
在"Hello World"之后,我决定写一个Conway的生命游戏变体作为下一步.关于如何获得"板" - 一个[高度] [宽度] [字节每像素] char数组 - 我已经完全神秘化了 - 集成到场景图中.
基本上,过程是"LifeBoard"遍历其规则并更新char*/image.我有这个简单的QML:
:::QML
ApplicationWindow {
id: life_app_window
visible: true
title: qsTr("Life")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Quit")
onTriggered: Qt.quit();
}
}
}
toolBar: ToolBar {
id: lifeToolBar;
ToolButton {
id: toolButtonQuit
text: qsTr("Quit")
onClicked: Qt.quit()
}
ToolButton {
id: toolButtonStop
text: qsTr("Stop")
enabled: false
//onClicked:
}
ToolButton {
id: toolButtonStart
text: qsTr("Start")
enabled: true
//onClicked: //Start life.
}
ToolButton {
id: …Run Code Online (Sandbox Code Playgroud) 如何处理QML中的64位整数?我知道,无用的Javascript无法正常处理它们,因为它对所有内容都使用double。如果我尝试在信号中使用它们,则所有内容都设置为undefined。有没有解决的办法?也许我必须使用a QVariant或其他东西?
在我的应用程序中,我有一个注册为singleton的类QML.我的目的是在QML关联数组中收集值并将此数组传递给C++.这是该类的简化版本:
class Config : public QObject
{
Q_OBJECT
private:
Config(QObject *parent = 0);
public:
static Config *instance();
~Config();
Q_INVOKABLE void sendValue (const QVariantMap &map) {
qWarning() << map.size();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我将该类的实例注册为singleton:
qmlRegisterSingletonType<Config>("myNS", 1, 0, "Config", config_singletontype_provider);
Run Code Online (Sandbox Code Playgroud)
在QML文件的某个地方,我尝试将javascript数组传递回c ++;
function sendValue() {
var arr = [];
arr["key"] = "value";
Config.sendValue(arr);
}
Run Code Online (Sandbox Code Playgroud)
但没有通过.将map.size()在C++返回0.也许我需要一些额外的转换?
我使用Qt 5.1.1在Windows上开发了一个项目,并在QML文件中使用了QtQuick 2.0导入.最近我发现我的应用程序需要OpenGL 2.0或更高版本,因此无法在具有OpenGL 1.0的系统上运行.我搜索并发现这是因为QtQuick 2.0导入.
该问题是:我不能修改导入从QtQuick 2.0至1.0 QtQuick因为我得到的错误:
QtQuick 1.0 module verison is not installed
Run Code Online (Sandbox Code Playgroud)
那我怎么才能安装QtQuick 1.0呢?我还安装了QtQuile 4.7附带的Qt 4.7,但是从Qt 5.1.1移植到Qt 4.7恰好是相当困难的工作.它并不那么简单,所以我想尽量保持简单,并在Qt 5.1.1上安装QtQuick 1.0.
注意:我在QML文件中没有做奇怪的事情; QML代码应该可以正常使用QtQuick 1.0.
rootContext()->findChild()从C++代码中的任何位置检索根对象(下面的代码返回0,也返回0)(类是注册的QML类型,类组件定义是根的直接子类的类方法,请参阅objectName )并在运行时添加生成的QQuickItem?Button使用最底层的代码将QML添加到场景中?在main.qml(片段)中
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
objectName: root
visible: true
width: 360
height: 100
// ...
}
Run Code Online (Sandbox Code Playgroud)
myclass.cpp
void myclass::add_hip()
{
setProperty("hip", 87);
QQmlEngine *engine = QtQml::qmlEngine(this);
// QObject *root = engine->rootContext()->findChild<QObject*>("womanObj");
QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
QObject *wobj = window->findChild<QObject *>("womanObj");
// 1. Define a QML component.
QQmlComponent *readHipComp = new QQmlComponent(engine);
readHipComp->setData("import QtQuick.Controls 1.2\n\
Button {\n\
anchors.top: addHipBtn.top\n\
anchors.left: addHipBtn.left\n\
anchors.margins: 3\n\
text: \"Hip value\"\n\
onClicked: {\n\
msgDlg.text …Run Code Online (Sandbox Code Playgroud) 我正在编写一个小程序,它使用Qt5 QML作为GUI层,使用Python3-PyQt5来实现数据模型.
我现在想要ComboBox在QML中显示并将其模型设置为枚举列表.我如何将枚举作为python类的属性导出,以便我可以在QML中引用它?
我最好用QML写这个:
ComboBox {
model: mymodel.car_manufacturers
onCurrentIndexChanged: mymodel.selected_manufacturer = currentIndex
}
Run Code Online (Sandbox Code Playgroud)