我想在 Qml Quick Controls 2 中显示数字SpinBox而不使用数字格式:
SpinBox {
    inputMethodHints: Qt.ImhDigitsOnly
    from: 1000
    to: 10000
}
我尝试设置不同的区域设置,但每次数字都显示为“1.000”或“1,000”(正确的是“1000”)。有没有办法强制未格式化的输出?
我正在使用 QFile 读取 Qt 5.12 上的文件。我尝试从计算机中读取文件,但是当我使用从 FileDialog 读取的目录时,其前缀为“file:///”。谁能告诉我为什么这是错误的以及如何使用从 FileDialog 获取的 URL?
谢谢!
QFile file("C:/Users/HuuChinhPC/Desktop/my_txt.txt"); // this work
//QFile file("file:///C:/Users/HuuChinhPC/Desktop/my_txt.txt"); //didn't work
QString fileContent;
if (file.open(QIODevice::ReadOnly) ) {
    QString line;
    QTextStream t( &file );
    do {
        line = t.readLine();
        fileContent += line;
     } while (!line.isNull());
    file.close();
} else {
    emit error("Unable to open the file");
    return QString();
}
我想通过按按钮向 QML 表视图添加一定数量的行。用户界面如下所示:
按“更新列表模型”后,TableView 中应出现一个新行。
我的代码看起来像这样(如下)。我想该addPerson方法必须发出dataChanged事件才能使其正常工作。我怎样才能做到这一点?或者是否有更好的解决方案来同步 QML 表视图与 C++ 模型?
主程序
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "MainWindow.h"
int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);
    qmlRegisterType<TableModel>("TableModel", 0, 1, "TableModel");
    QQmlApplicationEngine engine;
    MainWindow mainWindow;
    return app.exec();
}
主窗口.h
#pragma once
#include <QQmlApplicationEngine>
#include <QtQuick>
#include "TableModel.h"
class MainWindow : public QObject {
    Q_OBJECT;
public:
    explicit MainWindow() {
        engine_.load(QUrl(QStringLiteral("qrc:/main.qml")));
        QObject *rootObject = engine_.rootObjects().first();
        QObject::connect(rootObject, SIGNAL(on_ButtonUpdateListModel_click()), this, SLOT(on_ButtonUpdateListModel_click()));
    }
public slots:
    void on_ButtonUpdateListModel_click() {         
        QQuickView view;
        QQmlContext *ctxt …我正在使用忙碌指示器来显示中间进度,并且我想更改圆圈的颜色。我提到了自定义忙碌指示器链接,但这正在更改默认动画和其他内容。此外,它很难理解,因为没有对此进行解释。
我只想更改颜色,动画应该与默认值相同。请建议。
我遇到了这个:
ListView {
    id: listView
    model: ["Lorem","Ipsum"]
    delegate: Item {
        height: 20
        Text {
            z: 2
            text: modelData
            anchors.fill: parent
        }
        Rectangle {
            z: 1
            color: "red"
            // this does not work:
            anchors.fill: parent
            // this works, but I have mixed feelings about it:
            // height: 20; width: listView.width
        }
    }
}
因此,显然,anchors不在委托的子项目中工作(在这种情况下,Rectangle根本不显示).我想了解这背后的机制.另外,我想问一下处理这种情况的首选方法是什么?谢谢!
我的用户界面包含一个带有
  horizontalAlignment: Text.AlignJustify
  maximumLineCount: 5
  wrapMode: TextEdit.WordWrap
  elide: Text.ElideRight
当文本不适合时,最后一行应以“ ... MORE”结尾,其中“ MORE”应可突出显示。这可能是一个单独的“文本”字段,在该字段中,可见性由是否忽略文本来控制。
但是,如何检测何时删除文本?
我开始在Qt Creator中编写一个NumberAnimation声明,并在自动完成框中得到了一些东西.其中一个是"NumberAnimation with target".这是否意味着有这样的语法:
NumberAnimation with foo {
    // ...
}
我使用Qt Quick Controls 2时出现弹出窗口大小行为的问题.当我将ListView作为弹出窗口的contentItem时,弹出窗口大小为零.一些重现问题的示例代码:
import QtQuick 2.4
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
    id: window
    visible: true
    width: 800
    height: 600
    Button {
        text: "open popup"
        onClicked: popup.open()
    }
    Popup {
        id: popup
        x: (window.width - width) / 2
        y: window.height / 6
        width: contentWidth
        height: contentHeight
        contentItem: ListView {
            width: contentWidth
            height: contentHeight
            model: ListModel {
                ListElement {
                    name: "Apple"
                    cost: 2.45
                }
                ListElement {
                    name: "Orange"
                    cost: 3.25
                }
                ListElement {
                    name: "Banana"
                    cost: 1.95
                } …我正在用QML创建一个绘图组件.我的QML组件的结构如下所示:
Rectangle {
  id: canvas
  objectName: "myPlot"
  Rectangle {
    id: plot
    PlotArea {
      id: pa
    } // this is my c++ QQuickItem
    MouseArea {} // for handling interaction with the plot, like zooming
    XAxis{}
    YAXis{}
  }
}
这PlotArea是我的c ++课程.我需要从C++与这个QML组件进行交互,更准确地说,我需要调用一个成员函数PlotArea来向绘图中添加数据.通常的方法是使用findChild<QObject*>("objectName"),但由于组件将被重用,我不能给PlotArea和对象名称.
PlotArea如果我有指针,我该如何访问"myPlot"?我试过了
QObject *plot = rootObjects.value(0)->findChild<QObject*>("plotObject");
PlotArea * myplot = (plot->findChild<PlotArea*>("pa"));
但这不起作用.但是,如果我这样做,那么上述方法是有效的
PlotArea {
  id: pa
  objectName: "pa"
} // this is my c++ QQuickItem
但是我想知道这是否安全,因为在我的应用程序中会有服务PlotAreas,并且所有这些都有"pa"这个名字.