为什么 ComboBox 不显示所选项目?
Why doesn't ComboBox show the Selected Item?
这是我的 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 在插图中,除了 delegate
:
中的那个,他们还有一个 contentItem
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
为此,我必须在 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
这是我的 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 在插图中,除了 delegate
:
contentItem
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
为此,我必须在 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