【问题标题】:Why doesn't ComboBox show the Selected Item?为什么 ComboBox 不显示所选项目?
【发布时间】:2020-10-18 16:52:33
【问题描述】:

这是我的 QML 中的内容:

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3

Window {
    visible: true
    width: 640
    height: 480
    title: "Test Window"

    ComboBox{
        width: 300
        model: testContext.List
        delegate: ItemDelegate{
            width: parent.width
            contentItem: RowLayout{
                Text{ text: modelData.name }
                Text{
                    text: " | " + modelData.age
                    Layout.alignment: Text.AlignRight
                }
            }
            background: Rectangle{ color: hovered? "green" : "white" }
        }
    }
}

当我点击ComboBox 时,我在弹出列表中看到项目,但所选项目没有出现在框中!

如果我设置textRole: "name",它只会在框中显示name 属性,但我希望在框中显示ItemDelegate 中定义的整个格式化文本。

Here 在插图中他们还有一个contentItemdelegate 中的一个旁边:

contentItem: Text {
    ...
    text: control.displayText
    ...
}

即使我在 QML 中添加了额外的 contentItem,它仍然不会在框中显示格式化文本。

编辑

这是 ViewModel .h

#ifndef TEST_H
#define TEST_H

#include <QObject>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QVector>
#include "aclass.h"
#include "Property.h"

class Test : public QObject
{
    Q_OBJECT
    PROPERTY(QVector<AClass*>, List)

public:
    explicit Test(QObject *parent = nullptr);

private:
     QQmlApplicationEngine engine;

};

#endif // TEST_H

.cpp:

#include "test.h"

Test::Test(QObject *parent) : QObject(parent)
{
    engine.rootContext()->setContextProperty("testContext", this);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    for(int i = 0; i < 10; i++){
        auto a = new AClass();
        a->setname("Item " + QString::number(i));
        a->setage(i + 10);
        m_List.push_back(a);
    }
    emit ListChanged();
}

还有main.cpp:

#include <QGuiApplication>
#include "test.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    Test test;
    return app.exec();
}

对于Q_PROPERTY,我有一个宏PROPERTY,其内容如下:

#ifndef PROPERTY_H
#define PROPERTY_H

#define PROPERTY(QType, name) \
    Q_PROPERTY(QType name READ name WRITE set##name NOTIFY name##Changed) \
    public: \
    QType name(){return m_##name;} \
    void set##name(QType value){m_##name = value; emit name##Changed();} \
    Q_SIGNAL void name##Changed(); \
    private: \
    QType m_##name;
   
#endif // PROPERTY_H

这里是AClass

#ifndef ACLASS_H
#define ACLASS_H

#include <QObject>
#include "Property.h"

class AClass : public QObject
{
    Q_OBJECT
    PROPERTY(QString, name)
    PROPERTY(int, age)   
};

#endif // ACLASS_H

【问题讨论】:

  • 请澄清“选定”项目“没有出现在框中”怎么可能?什么是选定项目?实际上我看到你没有选择任何领带
  • @folibis,它根本就没有textRole,而且我还没有找到将格式化文本放入textRole的方法!
  • 你必须提供minimal reproducible example,包括你的模型定义和创建
  • @folibis,我已经添加了模型定义,并且在我的 main.qml 目前我只有组合框
  • @folibis,你测试了吗?

标签: qt combobox qml


【解决方案1】:

为此,我必须像这样在ItemDelegate 之外使用额外的contentItem

contentItem: RowLayout{
    Text{ text: model[control.currentIndex].name }
    Text{
        text: " | " + model[control.currentIndex].age
        horizontalAlignment: Text.AlignRight
        Layout.fillWidth: true
    }
}

我必须给ComboBox 一个id,在这个例子中control 是id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-24
    • 2010-11-30
    • 1970-01-01
    • 2010-11-07
    • 2011-04-19
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    相关资源
    最近更新 更多