如何使用QColumnView显示多列数据

发布于 2024-09-16 17:09:04 字数 716 浏览 1 评论 0原文

我想在 QColumnView 的多列中显示数据。我使用 Qt Creator 和 Qt 4 进行开发。

考虑一个地址簿应用程序,其中您有多个组:组 1、组 2 等。您的联系人可以属于这些组中的任何一个。

Group 1:
    John Smith
    Pocahontas
Group 2:
    Chief Powhatan
Group 3:
    ...

当选择第一列中的组时,第二列将显示该组中的所有联系人,当选择联系人时,其个人信息将显示在第三列中。

我已尝试以下操作(基于 Qt 文档中的示例):

QStringList strList1;
strList1 << "Group 1" << "Group 2" << "Group 3";

strListM1 = new QStringListModel(); // Previously declared as QStringListModel *strListM1
strListM1->setStringList(strList1);
ui->columnView->setModel(strListM1);

但是,我无法弄清楚如何添加更多列,以及如何将联系人姓名添加为第一列中这些组的子级。

我该怎么做?如何动态添加列和行(而不是像上面那样使用 QStringList 或任何其他类似的行方法)?

I want to display data in multiple columns in a QColumnView. I am using Qt Creator and Qt 4 for development.

Consider an address book application where you have multiple groups: Group 1, Group 2, etc. Your contacts can belong to any of those groups.

Group 1:
    John Smith
    Pocahontas
Group 2:
    Chief Powhatan
Group 3:
    ...

When a group in the first column is selected, the second column will show all contacts in that group, and when a contact is selected, their personal information is shown in a third column.

I have tried the following (based on an example from Qt Documentation):

QStringList strList1;
strList1 << "Group 1" << "Group 2" << "Group 3";

strListM1 = new QStringListModel(); // Previously declared as QStringListModel *strListM1
strListM1->setStringList(strList1);
ui->columnView->setModel(strListM1);

However, I have not been able to figure out how to add more columns, and add the contact names as children of those groups in the first column.

How can I do this? How could I add columns and rows dynamically (instead of using the QStringList like above, or any other similar method for rows)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

面如桃花 2024-09-23 17:09:05

您可以依赖QStandardItemQStandardItemModel。下面是一个非常简单且可编译的示例,说明如何将这些类与 QColumnView 一起使用:

#include <QtGui>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QMainWindow win;
    QColumnView *cview = new QColumnView;
    win.setCentralWidget(cview);

    /* Create the data model */
    QStandardItemModel model;

    for (int groupnum = 0; groupnum < 3 ; ++groupnum)
    {
        /* Create the phone groups as QStandardItems */
        QStandardItem *group = new QStandardItem(QString("Group %1").arg(groupnum));

        /* Append to each group 5 person as children */
        for (int personnum = 0; personnum < 5 ; ++personnum)
        {
            QStandardItem *child = new QStandardItem(QString("Person %1 (group %2)").arg(personnum).arg(groupnum));
            /* the appendRow function appends the child as new row */
            group->appendRow(child);
        }
        /* append group as new row to the model. model takes the ownership of the item */
        model.appendRow(group);
    }

    cview->setModel(&model);

    win.show();
    return app.exec();
}

有关 Qt 模型/视图编程的更多详细信息,请参阅 官方文档

You may rely on QStandardItem and QStandardItemModel. Here's a really simple and compilable example on how to use these classes with QColumnView:

#include <QtGui>

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QMainWindow win;
    QColumnView *cview = new QColumnView;
    win.setCentralWidget(cview);

    /* Create the data model */
    QStandardItemModel model;

    for (int groupnum = 0; groupnum < 3 ; ++groupnum)
    {
        /* Create the phone groups as QStandardItems */
        QStandardItem *group = new QStandardItem(QString("Group %1").arg(groupnum));

        /* Append to each group 5 person as children */
        for (int personnum = 0; personnum < 5 ; ++personnum)
        {
            QStandardItem *child = new QStandardItem(QString("Person %1 (group %2)").arg(personnum).arg(groupnum));
            /* the appendRow function appends the child as new row */
            group->appendRow(child);
        }
        /* append group as new row to the model. model takes the ownership of the item */
        model.appendRow(group);
    }

    cview->setModel(&model);

    win.show();
    return app.exec();
}

For more details abut Qt's Model/View Programming, please refer to the official documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文