为什么我的视图没有显示标题?

时间:2021-01-22 02:23:56

This is a class copied from the Qt tutorials:

这是从Qt教程复制的类:

class Window : public QWidget
{
    Q_OBJECT

public:
    Window();

    void setSourceModel();

private slots:
    void filterRegExpChanged();
    void filterColumnChanged();
    void sortChanged();
    void addMail();

private:
    QSortFilterProxyModel *proxyModel;
    QStandardItemModel *model;

    QGroupBox *sourceGroupBox;
    QGroupBox *proxyGroupBox;

    QTreeView *sourceView;
    QTreeView *proxyView;
    QCheckBox *filterCaseSensitivityCheckBox;
    QCheckBox *sortCaseSensitivityCheckBox;
    QLabel *filterPatternLabel;
    QLabel *filterSyntaxLabel;
    QLabel *filterColumnLabel;
    QLineEdit *filterPatternLineEdit;
    QComboBox *filterSyntaxComboBox;
    QComboBox *filterColumnComboBox;
    QPushButton* button;
};  

This is a def of a ctor:

这是一个ctor的定义:

Window::Window()
{
    model = new QStandardItemModel(this);  
//HEADERS ARE NOT DISPLAYED EVEN THOUGH I'M SETTING THEM HERE
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Subject"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Sender"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Date"));

    proxyModel = new QSortFilterProxyModel;
    proxyModel->setDynamicSortFilter(true);

    sourceView = new QTreeView;
    sourceView->setRootIsDecorated(false);
    sourceView->setAlternatingRowColors(true);

    proxyView = new QTreeView;
    proxyView->setRootIsDecorated(false);
    proxyView->setAlternatingRowColors(true);
    proxyView->setModel(proxyModel);
    proxyView->setSortingEnabled(true);

connect(button,SIGNAL(clicked()),this,SLOT(addMail()));//THIS SEEMS NOT TO MAKE ANY DIFFERENCE - NOTHING IS ADDED
};  

void Window::addMail()
{
    model->insertRow(0);
    model->setData(model->index(0, 0), "subject");
    model->setData(model->index(0, 1), "sender");
    model->setData(model->index(0, 2), "date");
}  
void Window::setSourceModel()
{
    proxyModel->setSourceModel(model);
    sourceView->setModel(model);
}  
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Window window;
    window.setSourceModel();
    window.show();

    return app.exec();
}

Neither headers nor data added via addMail are displayed. Why?

既不显示标题,也不显示通过addMail添加的数据。为什么?

2 个解决方案

#1


5  

QAbstractItemModel::setHeaderData is meant to handle user edits of the header, not setting them programmatically. The model then must implement setHeaderData to handle those edit requests. QStandardItemModel apparently doesn't create header items if they weren't set initially, so nothing happens.

QAbstractItemModel :: setHeaderData用于处理标头的用户编辑,而不是以编程方式设置它们。然后,模型必须实现setHeaderData来处理这些编辑请求。 QStandardItemModel显然不会创建标题项,如果它们最初没有设置,所以没有任何反应。

As you're using QStandardItemModel, you should use setHorizontalHeaderItem or setHorizontalHeaderLabels (Vertical variants are also available) to set up the header.

当您使用QStandardItemModel时,您应该使用setHorizo​​ntalHeaderItem或setHorizo​​ntalHeaderLabels(也可以使用垂直变体)来设置标头。

#2


-1  

If you look at the documentation, you will take notice, that setHeaderData takes four parameters, with the last one being the role. The role defaults to Qt::EditRole, but you probably want Qt::DisplayRole.

如果你查看文档,你会注意到,setHeaderData有四个参数,最后一个是角色。该角色默认为Qt :: EditRole,但您可能需要Qt :: DisplayRole。

#1


5  

QAbstractItemModel::setHeaderData is meant to handle user edits of the header, not setting them programmatically. The model then must implement setHeaderData to handle those edit requests. QStandardItemModel apparently doesn't create header items if they weren't set initially, so nothing happens.

QAbstractItemModel :: setHeaderData用于处理标头的用户编辑,而不是以编程方式设置它们。然后,模型必须实现setHeaderData来处理这些编辑请求。 QStandardItemModel显然不会创建标题项,如果它们最初没有设置,所以没有任何反应。

As you're using QStandardItemModel, you should use setHorizontalHeaderItem or setHorizontalHeaderLabels (Vertical variants are also available) to set up the header.

当您使用QStandardItemModel时,您应该使用setHorizo​​ntalHeaderItem或setHorizo​​ntalHeaderLabels(也可以使用垂直变体)来设置标头。

#2


-1  

If you look at the documentation, you will take notice, that setHeaderData takes four parameters, with the last one being the role. The role defaults to Qt::EditRole, but you probably want Qt::DisplayRole.

如果你查看文档,你会注意到,setHeaderData有四个参数,最后一个是角色。该角色默认为Qt :: EditRole,但您可能需要Qt :: DisplayRole。