c++ - 复杂模型和显示数据

标签 c++ qt

我刚刚开始学习 C++ 和 Qt Framework,我已经遇到了问题。问题是我如何创建和显示数据,它不仅仅是一个字符串,而是一个对象,我可以访问和显示哪些属性。例如,我有一个员工列表,我想显示一个如下所示的列表:

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

目标是列表中的每个项目都是可点击的,并打开一个包含详细信息的新窗口。此外,重要的是我可以为属性设置不同的样式。

最佳答案

Qt 为我们提供了模型和 View 框架,非常灵活。 您可以通过“模型”保存数据,通过“ View ”显示“模型”的数据 并通过“委托(delegate)”确定如何播放您的数据

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

您可以将窗口或 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();
}

根据需要增强它们,c++ 版本也支持委托(delegate)。 您可以封装 QListView 并在 用户点击索引(你需要 QItemSelectionModel 来检测哪个 您选择的项目)。在您可以设计高度自定义的 UI 之前,您有 研究了很多Qt的模型和 View 框架。既然你的情况 非常简单,默认的 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
}

关于c++ - 复杂模型和显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848221/

相关文章:

c++ - 您最喜欢的跨平台开发方式是什么?

c++ - 关于获取函数结果类型的各种方法

c++ - 为什么没有声明 putenv()?

c++ - QT:QXmlStreamReader 总是返回 "Premature End of Document"错误

c++ - 带有字符串可选参数的QT C++函数

c++ - 图像卷积与高斯模糊,加速可能吗?

c++ - 为 boost::property_tree::ptree 移动构造函数

c++ - 如何将 sprite 从 method 返回到 main 然后绘制它?

c++ - 如何在下拉状态下设置 QComboBox 背景颜色?

c++ - QT 中的 SQLite CREATE 查询