Why doesn't ComboBox show the Selected Item?

送分小仙女□ 提交于 2020-07-14 12:04:32

问题


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" }
        }
    }
}

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

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.

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

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

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

EDIT

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

the .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();
}

and the 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();
}

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

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

回答1:


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
    }
}

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



来源:https://stackoverflow.com/questions/62622656/why-doesnt-combobox-show-the-selected-item

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!