我正在尝试用Qt制作一个小程序.我有main.cpp
以下代码:
#include <QtGui/QApplication>
#include "qmlapplicationviewer.h"
Q_DECL_EXPORT int main(int argc, char *argv[])
{
QScopedPointer<QApplication> app(createApplication(argc, argv));
QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
viewer.setMainQmlFile(QLatin1String("qml/tw_looptijden_berekenen/main.qml"));
viewer.showExpanded();
return app->exec();
}
int reken_tijden_uit(){
return true;
}
Run Code Online (Sandbox Code Playgroud)
我有一个.qml
档案:
import QtQuick 1.1
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我点击时MouseArea
,程序退出.我想的是,它调用函数reken_tijden_uit
中main.cpp
的文件.
我已经google了很多,并在这个网站上搜索.我找到了几个答案,但我没有找到一个工作.
那么我放在哪里代码所以我可以reken_tijden_uit
在C++中调用该函数?
提前致谢.
头文件如下所示:
#ifndef EIGEN_FUNCTION_HEADER_H
#define EIGEN_FUNCTION_HEADER_H
class MyObject : …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个QML对象,其作用类似于其他对象的包装器.这是我的QML文件(Container.qml):
Item {
property string label
property Item control
Row {
Label {
text: label
}
// Not sure how to display the control assigned to the control property
}
}
Run Code Online (Sandbox Code Playgroud)
我想做什么(在我的QML中使用这个组件)是这样的:
Container {
label: "My Label"
control: Textbox {
text: "My Value"
}
}
Run Code Online (Sandbox Code Playgroud)
当输入QML时,结果(在界面中)应该类似于此QML的输出:
Item {
Row {
Label {
text: "My Label"
}
Textbox {
text: "My Value"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?当我尝试这样做时,在将一个Item分配给control属性时,我得到"无法将对象分配给属性".我搜索了Qt论坛并无情地搜索了这个,但没有成功.如果有人知道答案,我将不胜感激.
谢谢
插口
我是Qt-Quick的初学者.我不知道Qt是QML的基础.而且我也不熟悉C++,它也是Qt和QML的主要支持语言.我知道JS可以在QML层中做很多逻辑.但是如果我们需要与OS进行交互,那么我们必须使用一些基本语言.由于我对Python感到满意,我正在计划" QML-JS-Python "组合.
所以,我的问题:
抱歉,如果我愚蠢地提出这些问题.但我想提出建议和意见.
编辑:此组合QML-JS-Python的任何限制
提前致谢
有没有办法在QML和Qt 5.1中列出所有对象成员/属性?
如:
var obj=myQObject;
console.log(obj)
// expected output:
// obj { x:123..... }
Run Code Online (Sandbox Code Playgroud)
这对调试非常有帮助.
我创建了一个Publisher
定期发出QImage
对象的类.
但是,我很难将其绘制QImage
到QML元素中.似乎Image
和Canvas
QML组件需要一个QUrl
而不是一个QImage
,但我不知道如何将我转换QImage
成一个QUrl
.编辑4:当我说QUrl时,我并不是说我正在尝试将图像转换为URL.那是胡说八道.我的意思是我要生成对此图像的引用,该引用不在磁盘上,并且QML组件要求的数据类型是URL.
我做了一些研究,发现它QQuickImageProvider
提供了一个解决方案,但我没有找到任何文档解释如何将我的QImage
信号转换为QUrl
我可用于绘图的信号.任何示例代码或参考文档将不胜感激.
谢谢你的帮助!
EDIT1:
我在这里看了一下:http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html我没看到我如何将QImage传递给快速图像提供程序并从中创建一个QUrl .
EDIT2.这是标题.实施不应该很重要.
class Publisher
{
Q_OBJECT
public:
Publisher(QObject* parent = 0);
virtual ~Publisher(void);
Q_SIGNALS:
void newImage(const QImage& newImage);
};
Run Code Online (Sandbox Code Playgroud)
编辑3.这是我的QML代码,但我不知道如何绘制我的QImage,所以这段代码是没有意义的.
我的main.cpp文件:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
qmlRegisterType<Publisher>("Components", 1, 0, "Publisher");
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/QQuickViewExample/main.qml"));
viewer.showExpanded();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
我的main.qml文件:
import QtQuick …
Run Code Online (Sandbox Code Playgroud) 如何使ServerItem增长以适应内容?现在ServerItems只是相互重叠.
main.qml
import Qt 4.7
import "Teeworlds" as Teeworlds
Item {
Column {
Teeworlds.ServerItem {
serverName: "InstaGib, lost [xyz]"
}
Teeworlds.ServerItem {
serverName: "Arena.sbor (rus)"
}
}
}
Run Code Online (Sandbox Code Playgroud)
ServerItem.qml
import QtQuick 1.0
BorderImage {
id: serverItem
property string serverName: "unnamed server"
property string gameType: "DM"
property int numPlayers: 0
property int maxPlayers: 8
property int ping: 60
Text {
id: title
text: parent.serverName
}
Grid {
id: grid
anchors.top: title.bottom
columns: 2
rows: 3
Text { text: "Gametype: " } …
Run Code Online (Sandbox Code Playgroud) 我真的很喜欢QML.我喜欢如何定义组件(与类相比)及其属性,并从其他地方(与对象相比)实例化它们.
我可以说,一个按钮,有一些外观和感觉,上面有标签文字.例如,可以使用此组件定义(Button.qml
)来完成此操作:
Item {
id: button
property string label
anchors.fill: parent
Rectangle {
anchors.fill: parent
radius: 10
color: "gray"
Text {
anchors.centerIn: parent
font.pixelSize: 20
text: button.label
color: "white"
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在此主文件(main.qml
)中实例化:
Rectangle {
width: 300
height: 200
Button {
anchors.centerIn: parent
anchors.margins: 50
label: "Hello button!"
}
}
Run Code Online (Sandbox Code Playgroud)
但我看到以下限制:我只能定义一个带有一些属性的按钮模板,而不是一些占位符.实例中定义的所有子项都是直接子项,至少是默认值,我想更改此行为.
假设我想在按钮中放置一个项目(让我们说一个图像,但我不想告诉Button
它将是一个图像的定义).我想象这样的事情:
Item {
id: button
property Item contents <-- the client can set the placeholder content here
anchors.fill: parent …
Run Code Online (Sandbox Code Playgroud) 如果我在C++中有一个列表,我该如何将它暴露给QML(在Qt5/QtQuick 2中)?似乎QML只能理解 - QObject
派生类,这是一个问题,因为QObject
s不能放入QList
或复制.我该怎么做呢:
struct Thing
{
int size;
QString name;
};
class ThingManager : public QObject
{
Q_OBJECT
// These macros support QtQuick, in case we one day want to use it to make a slick
// interface (when QML desktop components are released).
Q_PROPERTY(QList<Thing> things READ things NOTIFY thingssChanged)
public:
// ...
QList<Thing> things() const;
// ...
};
Run Code Online (Sandbox Code Playgroud)
所以我可以在QML中做这样的事情:?
var a = thingManager.things[0].name;
Run Code Online (Sandbox Code Playgroud) 我想运行QML组件列表并选择一种类型:
for (var i = 0; i < controls.children.length; ++i) {
if ( typeof (controls.children[i].height) == "QDeclarativeRectangle")
{
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?