gpt4 book ai didi

qt - QWidget 作为 View 项(Qt Model View Controller )

转载 作者:行者123 更新时间:2023-12-04 13:29:33 26 4
gpt4 key购买 nike

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

};

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

Model.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

Model.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 Widget containing the list view:


#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

Prefs.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 Widget that has to be used as list item


#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

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

View.h


#ifndef VIEW_H
#define VIEW_H

#include <QListView>

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

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

#endif // VIEW_H

View.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 相关的任何内容我认为这是将小部件添加为 ListView 项的方法。

谢谢!

最佳答案

关于qt - QWidget 作为 View 项(Qt Model View Controller ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com