Qt는 우리에게 모델 및 뷰 프레임 워크를 제공하며 매우 유연합니다. 당신은 "보기" 하여 "모델"의 데이터를 표시, "모델"로 데이터를 저장하고 "위임"C의
코드 ++하여 데이터를 재생하는 방법을 결정할 수하기 때문에, 조금 장황 나는, 당신은 다른 QML 파일로 창이나 목록보기 분리 할 수있는 아이디어를
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
}
}
을 표현, QML과 자바 스크립트, C++의 힘을 결합하는 문서에서 QML 사용합니다. 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();
}
이 당신의 필요에 의해 그들을 향상, C++ 버전은 위임을 지원합니다. 사용자가 사용자가 색인을 클릭하면 QListView를 캡슐화하고 새 창을 열 수 있습니다 (선택한 항목을 항목으로 찾으려면 QItemSelectionModel이 필요함). 고도로 사용자 정의 할 수있는 UI를 디자인 할 수 있으려면 모델을 많이 사용해야합니다. Qt의 프레임 워크를 볼 수 있습니다. 귀하의 케이스 은 꽤 간단하기 때문에 기본 QListView와 QStandardItemModel이면 충분합니다.
보충 : 선택한 색인을 검색하는 방법은 무엇입니까?
//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
}
어떻게하면 QML을 사용하지 않고도이 작업을 수행 할 수 있습니까? –
편집 됨, 나는 당신의 숙제를하지 않기를 바란다. – StereoMatching
사실, 나는 이미 QListWidget을 사용하고 QListWidgetItems를 루프에 추가함으로써 조금 다르게 해결했다. 스타일링을 위해 저는 커스텀 델리게이트를 사용했습니다. 그런 방법은 괜찮습니까? 저는 처음부터 올바른 방법으로 일을하곤했기 때문입니다. –