2013-05-31 2 views
0

저는 C++과 Qt 프레임 워크를 배우기 시작했습니다. 저는 이미 문제가 있습니다. 문제는 단지 문자열이 아니라 액세스하고 표시 할 수있는 객체 인 데이터를 만들고 표시하는 방법입니다. 예컨대 나는 직원의 목록을하고 난 다음과 같다 목록 표시 할 :복잡한 모델과 데이터 표시

--------------------- 
John Smith 
Salary: 50,230 
--------------------- 
Max Mustermann 
Salary: 67,000 
--------------------- 

목표는 목록의 각 항목을 클릭하고 세부 사항 새 창을 여는입니다. 또한 중요한 부분은 속성을 다르게 스타일을 지정할 수 있다는 것입니다.

답변

1

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

어떻게하면 QML을 사용하지 않고도이 작업을 수행 할 수 있습니까? –

+0

편집 됨, 나는 당신의 숙제를하지 않기를 바란다. – StereoMatching

+0

사실, 나는 이미 QListWidget을 사용하고 QListWidgetItems를 루프에 추가함으로써 조금 다르게 해결했다. 스타일링을 위해 저는 커스텀 델리게이트를 사용했습니다. 그런 방법은 괜찮습니까? 저는 처음부터 올바른 방법으로 일을하곤했기 때문입니다. –