复杂模型和显示数据 我刚刚开始学习C++和QT框架,我已经有问题了。问题是如何创建和显示数据,这些数据不仅是字符串,而且是对象,我可以访问和显示哪些属性。例如,我有一个员工列表,我想显示一个如下所示的列表: --------------------- John Smith Salary: 50,230 --------------------- Max Mustermann Salary: 67,000 ---------------------
目标是列表中的每个项目都可以单击,并打开一个包含详细信息的新窗口。另外,重要的一点是,我可以对属性进行不同的样式设置。Qt为我们提供了模型和视图框架,它非常灵活。 您可以通过“模型”保存数据,通过“视图”显示“模型”的数据 并确定如何通过“委派”播放数据 <> C++代码有点冗长,所以我用QML从文档中表达了概念复杂模型和显示数据 我刚刚开始学习C++和QT框架,我已经有问题了。问题是如何创建和显示数据,这些数据不仅是字符串,而且是对象,我可以访问和显示哪些属性。例如,我有一个员工列表,我想显示一个如下所示的列表: --------------------- John Smith Salary: 50,230 --------------------- Max Mustermann Salary: 67,000 ---------------------,c++,qt,C++,Qt,目标是列表中的每个项目都可以单击,并打开一个包含详细信息的新窗口。另外,重要的一点是,我可以对属性进行不同的样式设置。Qt为我们提供了模型和视图框架,它非常灵活。 您可以通过“模型”保存数据,通过“视图”显示“模型”的数据 并确定如何通过“委派”播放数据 C++代码有点冗长,所以我用QML从文档中表达了概念 import QtQuick 2.1 import QtQuick.Window 2.1 import QtQuick.Controls 1.0 Rectangle {
import QtQuick 2.1
import QtQuick.Window 2.1
import QtQuick.Controls 1.0
Rectangle {
width: 640; height: 480
//the new window
Window{
id: newWindow
width: 480; height:240
property string name: ""
property string salaryOne: ""
property string salaryTwo: ""
Rectangle{
anchors.fill: parent
Text{
id: theText
width:width; height: contentHeight
text: newWindow.name + "\nSalaryOne : " + newWindow.salaryOne + "\nSalaryTwo : " + newWindow.salaryTwo
}
Button {
id: closeWindowButton
anchors.centerIn: parent
text:"Close"
width: 98
tooltip:"Press me, to close this window again"
onClicked: newWindow.visible = false
}
}
}
ListModel {
id: salaryModel
ListElement {
name: "John Smith"
SalaryOne: 50
SalaryTwo: 230
}
ListElement {
name: "Max Mustermann"
SalaryOne: 67
SalaryTwo: 0
}
}
//this is the delegate, determine the way you want to show the data
Component {
id: salaryDelegate
Item {
width: 180; height: 40
Column {
Text { text: name }
Text { text: "Salary : " + SalaryOne + ", " + SalaryTwo }
}
MouseArea{
anchors.fill: parent
//set the value of the window and make it visible
onClicked: {
newWindow.name = model.name
newWindow.salaryOne = model.SalaryOne
newWindow.salaryTwo = model.SalaryTwo
newWindow.visible = true
view.currentIndex = index
}
}
}
}
ListView {
id: view
anchors.fill: parent
model: salaryModel
delegate: salaryDelegate
}
}
<> P>可以将窗口或ListVIEW划分为不同的QML文件,结合C++、QML和JavaScript的功能。像qml这样的声明性语言在处理UI方面非常好
c++版本
#include <memory>
#include <QApplication>
#include <QListView>
#include <QSplitter>
#include <QStandardItemModel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel model(2, 1);
model.appendRow(new QStandardItem(QString("John Smith\nSalary: %1, %2\n").arg(50).arg(230)));
model.appendRow(new QStandardItem(QString("Max Mustermann\nSalary: %1, ").arg(67) + QString("000\n")));
QSplitter splitter;
QListView *list = new QListView(&splitter);
list->setModel(&model);
splitter.addWidget(list);
splitter.show();
return a.exec();
}
您是否可以演示如何在不使用QML的情况下实现这一点?实际上,我已经通过使用QListWidget并在循环中向其添加QListWidgetItems解决了这一问题。对于样式设置,我使用了自定义委托。这样的方法行吗,因为我从一开始就习惯了以正确的方式做事?不,这不是家庭作业,我只是很想学点新东西。祝贺你。只要它符合你的需要,它就是一个好方法(以我的拙见)。
//the type of model_selected is QItemSelectionModel*
model_selected = list->selectionModel();
connect(model_selected, SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
this, SLOT(selection_changed(QItemSelection, QItemSelection)));
void imageWindow::selection_changed(QItemSelection, QItemSelection)
{
//do what you want
}