QWidget作为查看项目(Qt模型视图控制器)

时间:2013-02-07 02:49:58

标签: qt design-patterns qt4 qwidget model-view

所以我想要的是一个显示可选窗口小部件的qlistview(显示按钮图像和文本的标签)(窗口小部件是一个基于qwidget的窗口小部件,其水平布局带有QLabel和{{1} }))。模型应存储每个项目的图像路径和按钮文本(这似乎不是问题)。我成功创建了一个QListView派生小部件,但它只显示第一个列表项(即自定义小部件),并且它不可选。我创建了一个自定义模型,视图和委托,但我无法弄清楚如何在所有列表项上显示小部件,而不仅仅是第一个。 以下是完整的源代码链接:SOURCE CODE LINK

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

列表中的5个小部件: Widget compiled and ran with 5 widget items defined in the loop

列表中的1个小部件: Widget compiled and ran with 1 widget item defined in the loop

正如您所看到的,阴影中存在差异。

这是代码的另一个副本:

  

Delegate.h   以下是QPushButton的代码:

delegate
  

Delegate.cpp

#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;

};
  

Model.h

#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());
}
  

Model.cpp

#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
  

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

#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.cpp

#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
  

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

#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.cpp

#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
  

View.h

#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();
}
  

View.cpp

#ifndef VIEW_H
#define VIEW_H

#include <QListView>

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

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

#endif // VIEW_H

任何人都可以帮助我用想要的小部件填充所有列表项或告诉我我做错了什么或一些提示?是否有可能在qt中将小部件作为此MVC样式中的列表/表项?我找不到任何在任何地方实现这一点的参考。使用Qt进行C ++ GUI编程,使用Qt 4进行C ++设计模式简介以及在互联网上更多地方进行搜索,但找不到与#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()); } 相关的任何内容,我认为这是添加的方法小部件作为列表视图项。

谢谢!

1 个答案:

答案 0 :(得分:1)