我想在QML中使用QAbstractListModel派生模型.将模型绑定到视图已经很有效.
我想要实现的下一件事是能够访问特定项目及其角色,就像使用QML ListModel一样
grid.model.get(index).DisplayRole
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何在我的QAbstractListModel派生模型中实现这个get方法.
任何提示?
我总是把我的#include
后 #ifdef
/ #define
包含卫队.现在我的IDE(Qt Creator)的重构机制将它放在Include-Guard之前,例如
#include "AnotherHeader.h"
#ifndef MYHEADER_H
#define MYHEADER_H
Run Code Online (Sandbox Code Playgroud)
这会导致任何问题,还是可以这样离开?
Column
文档解释了如何实现这一目标:
但是,我想实现这个目标:
也就是说,其中的所有项目Column
都应该与中心水平对齐.anchors.horizontalCenter: parent.Center
不起作用.如何获得所需的结果?
我正在开发一个应该在Windows和Linux下运行的(Qt)应用程序.所以,我希望能够在Windows和Linux下开发.
因此,我将我的项目(作为git repo)存储在我在Linux下安装的NTFS分区(Ubuntu 13.10).为了避免编译的可执行文件的权限问题,我将阴影构建目录设置为Linux下的主文件夹.
到目前为止,这种方法的工作很好.但有一些影响让我担心:如果我暂存一些已更改的文件(使用smartgit),那么smartgit并不反映我已经将它们暂存.它们仍然显示为未分级
提交时类似的事情:提交后,提交的更改仍然显示为不提交.但是在日志中我可以看到它们被提交了.关闭重新开放回购"解决"这个问题或至少是一个解决方法.
但我担心在Linux下的NTFS分区上使用它来破坏我的仓库.或者,有一天我的回购公司没有被这样破坏的风险吗?
我已经想出如何将从QAbstractListModel派生的模型绑定到QML视图.
但是我厌倦的下一件事是行不通的.如果将新项添加到模型中,则QML视图将不会更新.这是为什么?
DataObject.h
class DataObject {
public:
DataObject(const QString &firstName,
const QString &lastName):
first(firstName),
last(lastName) {}
QString first;
QString last;
};
Run Code Online (Sandbox Code Playgroud)
SimpleListModel.h
class SimpleListModel : public QAbstractListModel
{
Q_OBJECT
enum /*class*/ Roles {
FIRST_NAME = Qt::UserRole,
LAST_NAME
};
public:
SimpleListModel(QObject *parent=0);
QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const;
QHash<int, QByteArray> roleNames() const;
void addName(QString firstName, QString lastName);
private:
Q_DISABLE_COPY(SimpleListModel);
QList<DataObject*> m_items;
};
Run Code Online (Sandbox Code Playgroud)
SimpleListModel.cpp
SimpleListModel::SimpleListModel(QObject *parent) :
QAbstractListModel(parent)
{
DataObject *first = new …
Run Code Online (Sandbox Code Playgroud) 我有一个带有 RowLayout 的 ColumnLayout。RowLayout 按预期定位。如果正在调整窗口大小,情况也是如此。即使窗口比整个 ColumnLayout 小(见第二张图片)
但是,如果我用(水平)ListView 替换 RowLayout,则此 ListView 不会按我的预期定位。我希望它的行为类似于 RowLayout 的示例,但 ListView 的位置更高:
如果窗口变得“变小”,则与第一个示例不同,蓝色矩形“移入”ListView:
如何使用 ListView 实现第一个示例的行为?
来源
import QtQuick 2.0
import QtQuick.Layouts 1.1
Rectangle {
width: 360
height: 360
ColumnLayout {
anchors.fill: parent
anchors.margins: 20
Item {
Layout.fillHeight: true
}
/*
ListView {
Layout.fillHeight: true
Layout.fillWidth: true
orientation: ListView.Horizontal
spacing: 5
model: 3
delegate: Rectangle {
width: 50
height: 50
color: "red"
}
}
*/
RowLayout {
Layout.fillHeight: true
Layout.fillWidth: true
Rectangle {
width: 50
height: …
Run Code Online (Sandbox Code Playgroud) 我发现了一段视频,其中显示了视频中称为"实时节目"的内容.
这可以在这里看到.
我认为这个想法是在应用程序运行时编辑QML文件.保存更改后,应用程序会立即显示此更改.
这消除了重新启动应用程序以查看更改的需要.
我认为这非常好,因为它加快了工作流程.
但我真的不知道它是如何工作的.我需要做些什么来制作这样的东西才能在Windows环境中运行?
我已经在文档中阅读了术语系统接受测试和用户接受测试。
但是我真的无法弄清楚这两者之间有什么区别。
谁能解释其中的区别?
testing acceptance-testing manual-testing user-acceptance-testing
我有一个图像提供程序派生自QQuickImageProvider
实现requestPixmap
.
此图像提供程序适用于Image
组件.
现在我想以同样的方式提供imageSource
a Button
.但没有图像出现.可能是什么问题?
QML代码
Image {
anchors.fill: parent
anchors.margins: 10
source: "image://provider/" + model.DisplayRole
fillMode: Image.PreserveAspectFit
}
Button {
Layout.fillWidth: true
Layout.preferredHeight: width
iconSource: "image://provider/" + model.DisplayRole
onClicked: appCore.switch(model.DisplayRole)
}
Run Code Online (Sandbox Code Playgroud)
C++代码
QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
QModelIndex index;
bool foundId = false;
for(int row = 0; row < m_myModel->rowCount(); row++)
{
index = m_myModel->index(row, 0);
QString name = QVariant(m_myModel->data(index, Qt::DisplayRole)).toString();
if(name == id)
{
foundId …
Run Code Online (Sandbox Code Playgroud) 我有一个自定义Footer
Component
,我想在 QML 应用程序的不同位置重用它:
Rectangle {
color: "gold"
height: 50
anchors {
bottom: parent.bottom
left: parent.left
right: parent.right
}
RowLayout {
anchors.fill: parent
anchors.margins: 10
Button {
text: "quit"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个的使用很简单:
Window {
visible: true
Footer {
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我想在一个视图中添加一个“ButtonA” RowLayout
,Footer
在另一个视图中添加一个“ButtonB”。
我怎样才能做到这一点?