ComboBox为什么不显示

ComboBox为什么不显示

本文介绍了ComboBox为什么不显示“选定项目”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在QML中的功能:

Here's what I've in my 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 我在弹出列表中看到了项目,但所选项目没有出现在框中!

When I click on the ComboBox I see items in the popup list BUT the selected item doesn't appear in the box!

如果我设置了 textRole : name ,它仅在框中显示 name 属性,但我希望在 ItemDelegate ,在框中。

If I set textRole: "name", it shows only the name property in the box but I want the whole formatted text, defined in ItemDelegate, in the box.

delegate contentItem c>:

Here in the illustration they've one more contentItem beside the one in delegate:

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

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

It still doesn't show the formatted text in the box even if I add the additional contentItem in my QML.

编辑

这是ViewModel .h

Here's the 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

for the Q_PROPERTY I've a macro PROPERTY with the following content:

#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


推荐答案

为此,我必须在 ItemDelegate 之外使用其他 contentItem ,像这样:

For that I've to use the additional contentItem outside the ItemDelegate like this:

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

,我必须给 ComboBox 一个ID,在此示例中为 control 是ID

and I've to give the ComboBox an id, here in the example control is the id

这篇关于ComboBox为什么不显示“选定项目”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:48