4

所以我想要的是一个显示可选小部件的 qlistview(显示按钮的图像和文本的标签(小部件是一个基于 qwidget 的小部件,它具有带有 aQLabel和 a的水平布局QPushButton))。该模型应该为每个项目存储图像路径和按钮文本(这似乎不是问题)。我成功创建了一个 QListView 派生小部件,但它只显示第一个列表项(这是自定义小部件)并且它是不可选择的。我创建了一个自定义模型、视图和委托,但我不知道该怎么做才能在所有列表项上显示小部件,而不仅仅是首先。这是完整的源代码链接:SOURCE CODE LINK

我用 5 个小部件项目的列表和 1 个小部件项目的列表分别运行应用程序。而且我认为它添加了小部件,但它在第一个上重叠了所有小部件(5 个项目构建在按钮上有更密集的阴影):

列表中的 5 个小部件: 使用循环中定义的 5 个小部件项编译和运行小部件

列表中的 1 个小部件: 使用循环中定义的 1 个小部件项目编译和运行小部件

如您所见,阴影有所不同。

这是代码的另一个副本:

Delegate.h 下面是代码delegate

#include <QtGui>
#include <QAbstractItemDelegate>

class WidgetDelegate : public QAbstractItemDelegate
{
public:
    WidgetDelegate(QObject *parent = 0);

    void paint(QPainter *painter,
               const QStyleOptionViewItem &option,
               const QModelIndex &index) const;

    QSize sizeHint(const QStyleOptionViewItem &option,
                  const QModelIndex &index) const;

};

委托.cpp

#include <QtGui>

#include "Delegate.h"
#include "Profile.h"

WidgetDelegate::WidgetDelegate(QObject *parent)
    : QAbstractItemDelegate(parent)
{ }

void WidgetDelegate::paint(QPainter */*painter*/,
                           const QStyleOptionViewItem &/*option*/,
                           const QModelIndex &/*index*/) const
{
}

QSize WidgetDelegate::sizeHint(const QStyleOptionViewItem &/*option*/,
                              const QModelIndex &/*index*/) const
{
    return QSize(ProfileItem().geometry().width(), ProfileItem().geometry().height());
}

模型.h

#ifndef MODEL_H
#define MODEL_H

#include <QStringList>
#include <QAbstractListModel>
#include <QList>
#include "Profile.h"

class StringListModel : public QAbstractListModel
{
    Q_OBJECT

public:
    StringListModel(const QStringList &strings, QObject *parent = 0)
        : QAbstractListModel(parent), stringList(strings) {}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const;

private:
    QStringList stringList;
};

#endif // MODEL_H

模型.cpp

#include "Model.h"
#include <QVariant>

int StringListModel::rowCount(const QModelIndex &/*parent*/) const
{
    return stringList.count();
}

QVariant StringListModel::data(const QModelIndex &/*index*/,
                               int /*role*/) const
{
}

QVariant StringListModel::headerData(int /*section*/,
                                     Qt::Orientation /*orientation*/,
                                     int /*role*/) const
{
}

Prefs.h 包含列表视图的小部件:

#ifndef PREFERENCES_H
#define PREFERENCES_H

#include "Model.h"
#include <QDialog>

class QPushButton;
class ProfileItem;
class QVBoxLayout;
class View;
class StringListModel;

class Preferences : public QDialog
{
public:
    Preferences(QWidget *parent = 0);

private:
    QVBoxLayout *m_pVerticalLayout;

    View *myList;
    QPushButton *button;
    ProfileItem *item;
    StringListModel *customModel;
};

#endif // PREFERENCES_H

首选项.cpp

#include "Profile.h"

#include <QPixmap>
#include <QHBoxLayout>
#include <QBitmap>
#include <QMessageBox>

ProfileItem::ProfileItem(QWidget *parent) :
    QWidget(parent)
{
    pixmap = QPixmap(":/avatar");

    m_avatarImageLabel.setPixmap(pixmap);
    m_avatarImageLabel.setMask(pixmap.mask());
    m_avatarTextButton.setText("Test");
    connect(&m_avatarTextButton, SIGNAL(clicked()), this, SLOT(buttonPushed()));

    m_pHorizontalLayout = new QHBoxLayout;

    m_pHorizontalLayout->addWidget(&m_avatarImageLabel);
    m_pHorizontalLayout->addWidget(&m_avatarTextButton);

    setLayout(m_pHorizontalLayout);
}

void ProfileItem::setAvatarImage(const QString &avatarImage)
{
    pixmap = QPixmap(avatarImage);
    m_avatarImageLabel.setPixmap(pixmap);
    m_avatarImageLabel.setMask(pixmap.mask());
}

void ProfileItem::setAvatarName(const QString &avatarName)
{
    m_avatarTextButton.setText(avatarName);
}

void ProfileItem::buttonPushed()
{
    QMessageBox msg;
    msg.setText("Button was pushed!");
    msg.exec();
}

Profile.h 必须用作列表项的小部件

#ifndef PROFILEITEM_H
#define PROFILEITEM_H

#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QPixmap>

class QHBoxLayout;

class ProfileItem : public QWidget
{
    Q_OBJECT

public:
    explicit ProfileItem(QWidget *parent = 0);

public slots:
    void setAvatarImage(const QString &avatarImage);
    void setAvatarName(const QString &avatarName);
    void buttonPushed();

private:
    QPixmap pixmap;
    QLabel m_avatarImageLabel;
    QPushButton m_avatarTextButton;

    QHBoxLayout *m_pHorizontalLayout;

};

#endif // PROFILEITEM_H

配置文件.cpp

#include "Profile.h"

#include <QPixmap>
#include <QHBoxLayout>
#include <QBitmap>
#include <QMessageBox>

ProfileItem::ProfileItem(QWidget *parent) :
    QWidget(parent)
{
    pixmap = QPixmap(":/avatar");

    m_avatarImageLabel.setPixmap(pixmap);
    m_avatarImageLabel.setMask(pixmap.mask());
    m_avatarTextButton.setText("Test");
    connect(&m_avatarTextButton, SIGNAL(clicked()), this, SLOT(buttonPushed()));

    m_pHorizontalLayout = new QHBoxLayout;

    m_pHorizontalLayout->addWidget(&m_avatarImageLabel);
    m_pHorizontalLayout->addWidget(&m_avatarTextButton);

    setLayout(m_pHorizontalLayout);
}

void ProfileItem::setAvatarImage(const QString &avatarImage)
{
    pixmap = QPixmap(avatarImage);
    m_avatarImageLabel.setPixmap(pixmap);
    m_avatarImageLabel.setMask(pixmap.mask());
}

void ProfileItem::setAvatarName(const QString &avatarName)
{
    m_avatarTextButton.setText(avatarName);
}

void ProfileItem::buttonPushed()
{
    QMessageBox msg;
    msg.setText("Button was pushed!");
    msg.exec();
}

查看.h

#ifndef VIEW_H
#define VIEW_H

#include <QListView>

class View : public QListView
{
public:
    View();

    void setModel(QAbstractItemModel *model);
    QSize sizeHint();
};

#endif // VIEW_H

查看.cpp

#include "View.h"
#include "Profile.h"

View::View()
{
    viewport()->setAutoFillBackground(false);
    setSelectionMode(QAbstractItemView::SingleSelection);
}

void View::setModel(QAbstractItemModel* model)
{
    QListView::setModel(model);

    for (int i = 0; i < 5; ++i)
    {
        QModelIndex index = model->index(i, 0);

        ProfileItem* widget = new ProfileItem();
        setIndexWidget(index, widget);
    }
}

QSize View::sizeHint()
{
    return QSize(ProfileItem().width(), ProfileItem().height());
}

谁能帮我用想要的小部件填充所有列表项或告诉我我做错了什么或一些提示?在 qt 中是否可以将小部件作为这种 MVC 样式的列表/表格项?我在任何地方都找不到任何参考来实现这一目标。在 C++ GUI Programming with Qt、Advanced Qt Programming、Introduction to Design Patterns in C++ with Qt 4 和 Internet 上的其他几个地方搜索,但找不到任何相关QAbstractItemView::setIndexWidget内容,我认为这是将小部件添加为列表的方法查看项目。

谢谢!

4

1 回答 1

1

我认为小部件不是用 setIndexWidget 添加的,而是在委托中创建的。

查看一些示例:
http ://doc.qt.digia.com/stable/itemviews-spinboxdelegate.html
http://www.qtforum.org/article/24195/qpushbutton-as-delegate-in-a-qtableview.html
在 Qt 中,如何正确实现委托?

于 2013-02-17T09:30:33.787 回答