我有一个要求,我有 QAbstractListModel ,它正在不断更新。QAbstractListModel的数据类型是整数类型。
我想以特定的时间间隔将整个数据复制到向量中,以便向量不断更新,我可以进一步使用它。
知道如何通过索引迭代 QAbstractListModel 并将其复制到向量中。
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QDebug>
#include <QObject>
class MyClass : public QObject
{
public:
MyClass();
public slots:
void buttonClicked();
void buttonClicked(QString &in);
};
#endif // MYCLASS_H
Run Code Online (Sandbox Code Playgroud)
myclass.cpp
#include "myclass.h"
MyClass::MyClass()
{
}
void MyClass::buttonClicked()
{
// Do Something
}
void MyClass::buttonClicked(QString &in)
{
qDebug() << in;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <QApplication>
#include <QQmlApplicationEngine>
#include <myclass.h>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
MyClass myClass; // A class containing my functions
// Trying …Run Code Online (Sandbox Code Playgroud) 我需要按红色圆圈下的创建按钮,然后按此按钮我试图填充表格视图的第一行。我是 QT Quick 的新用户,请帮助我。我浪费了很多时间却没有办法。
我还提供了 UI 和代码,请查看。
谢谢 !
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.0
import QtQuick.Dialogs 1.0
import "content"
import "images"
ApplicationWindow {
visible: true
title: "Mask Editor: Subsystem"
width: 720
height: 420
minimumHeight: 400
minimumWidth: 720
statusBar: StatusBar {
id: minstatusbar
RowLayout {
id:row1
spacing: 2
width: parent.width
Button {
text:"Unmask"
}
Item { Layout.fillWidth: true }
Button {
text:"OK"
}
Button {
text:"Cancel"
}
Button {
text:"Help"
}
Button {
text:"Apply"
}
}
} …Run Code Online (Sandbox Code Playgroud) Qt Quick Templates 弹出窗口与 QtQuick Controls 弹出窗口有什么区别?
import QtQuick.Templates 2.0我从vs得到的弹出窗口import QtQuick.Controls 2.0似乎有细微的行为差异。
[1] https://doc-snapshots.qt.io/qt5-5.8/qml-qtquick-controls2-popup.html
我想创建一个类似手风琴的动画元素,单击时会展开。这是它应该如何工作的。
当用户单击红色矩形之一时,作为实际内容的绿色矩形应该展开。我希望这个扩展是动画的。每个红色标题的绿色矩形内容的高度可能不同。
我已经能够实现点击展开行为,但没有动画。这是我目前拥有的代码。
AccordionElement.qml
import QtQuick 2.5
import QtQuick.Layouts 1.1
ColumnLayout {
id: rootElement
property string title: ""
property bool isOpen: false
default property alias accordionContent: contentPlaceholder.data
anchors.left: parent.left; anchors.right: parent.right
// Header element
Rectangle {
id: accordionHeader
color: "red"
anchors.left: parent.left; anchors.right: parent.right
height: 50
MouseArea {
anchors.fill: parent
Text {
text: rootElement.title
anchors.centerIn: parent
}
cursorShape: Qt.PointingHandCursor
onClicked: {
rootElement.isOpen = !rootElement.isOpen
}
}
}
// This will get filled with the content
ColumnLayout {
id: contentPlaceholder
visible: …Run Code Online (Sandbox Code Playgroud) 假设我们有一个非常简单的QML文件,就像这样:
import QtQuick 2.0
Rectangle {
width: 800
height: 600
color: '#000'
Text {
text: qsTr("Hi all")
anchors.centerIn: parent
}
}
Run Code Online (Sandbox Code Playgroud)
QML文件加载了QtQuick2ApplicationViewer助手类,如下所示:
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/MyApp/Login/Window.qml"));
viewer.showFullScreen();
Run Code Online (Sandbox Code Playgroud)
如果我想从C++中将Rectangle的颜色更改为白色,我该怎么办?我的猜测是:
QQuickItem *window = viewer.rootObject();
window->setProperty("color", "#fff");
Run Code Online (Sandbox Code Playgroud)
但所有这一切都是以下编译器错误:
invalid use of incomplete type 'struct QQuickItem'
forward declaration of 'struct QQuickItem'
Run Code Online (Sandbox Code Playgroud) 我试过这篇文章:
http://www.ics.com/blog/combining-qt-widgets-and-qml-qwidgetcreatewindowcontainer#.UlVUExCXcnI
如果我尝试重做这个,我会收到此错误:
15:13:36:项目的运行步骤Test_QMLWidgets01 ... 15:13:36:配置不变,跳过qmake步骤.15:13:36:开始:"C:\ Qt\Qt5\Tools\QtCreator\bin\jom.exe"C:\ Qt\Qt5\Tools\QtCreator\bin\jom.exe -f Makefile.Debug cl -c -nologo -Zm200 -Zc:wchar_t -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc -DUNICODE -DWIN32 -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\ Qt\Qt5\5.1.0\msvc2010\include"-I"C:\ Qt\Qt5\5.1.0\msvc2010\include\QtWidgets"-I"C:\ Qt\Qt5\5.1.0\msvc2010\include\QtGui"-I "C:\ Qt\Qt5\5.1.0\msvc2010\include\QtANGLE"-I"C:\ Qt\Qt5\5.1.0\msvc2010\include\QtCore"-I"debug"-I"." -一世"." -I "C:\ Qt的\ QT5\5.1.0\msvc2010\mkspecs\Win32的msvc2010"
所以我将#include改为#include,但后来我得到链接错误:
15:16:27:项目的运行步骤Test_QMLWidgets01 ... 15:16:27:配置不变,跳过qmake步骤.15:16:27:开始:"C:\ Qt\Qt5\Tools\QtCreator\bin\jom.exe"C:\ Qt\Qt5\Tools\QtCreator\bin\jom.exe -f Makefile.Debug cl -c -nologo -Zm200 -Zc:wchar_t -Zi -MDd -GR -W3 -w34100 -w34189 -EHsc -DUNICODE -DWIN32 -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -I"C:\ Qt\Qt5\5.1.0\msvc2010\include"-I"C:\ Qt\Qt5\5.1.0\msvc2010\include\QtWidgets"-I"C:\ Qt\Qt5\5.1.0\msvc2010\include\QtGui"-I "C:\ Qt\Qt5\5.1.0\msvc2010\include\QtANGLE"-I"C:\ Qt\Qt5\5.1.0\msvc2010\include\QtCore"-I"debug"-I"." -一世"." …
如何让它工作锚定动画,如下所述:
import QtQuick 2.0
Item {
id: container
width: 200; height: 200
Rectangle {
id: myRect
width: 100; height: 100
color: "red"
}
Behavior on anchors.right {
AnchorAnimation { duration: 2000 }
}
Component.onCompleted: {
myRect.anchors.right = container.right
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?更换锚点时没有适当的动画.
此外,如果可以,请提供有关此主题的一些示例.
UPD:提供了更简单的例子.
import QtQuick 1.0
Rectangle
{
width: 100; height: 100
color: "red"
MouseArea
{
anchors.fill: parent
onPressed:
{
NumberAnimation
{
target: parent.x
to: 50;
duration: 1000
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这段代码能够x在按钮按下事件中移动矩形的位置,但这没有任何作用.
我哪里错了?
使用QtQuick,我在转发器中有一排5个图像.我想在悬停时实现一个类似于MacOS停靠动画的动画.这是一张参考图片:
为了进一步分解,这就是我想要实现的目标.这些图像在悬停时应如下:
这是我到目前为止的代码
Row {
spacing: 2
anchors.bottom: parent.bottom
anchors.bottomMargin: 30
anchors.horizontalCenter: parent.horizontalCenter
Repeater {
id: iconRepeater
model: iconColors()
Image {
source: "icons/" + modelData + ".png"
scale: mouseArea.containsMouse ? 1.5 : 1.0
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: endTimer()
}
Behavior on scale {
PropertyAnimation {
duration: 75
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会扩展您悬停的图像,但我似乎也无法影响邻居.任何建议表示赞赏!