C++ QListWidget项目一致性定位问题
我不熟悉Qt框架。在新的应用程序中,我想创建带有自定义项目的列表。这些项目非常简单,必须包含标题标签、缩略图和描述标签() 现在我不想玩自定义绘图和所有这些东西,因为我认为使用适当的小部件/布局可以更容易地完成我想要的项目,所以我决定使用QListwidget和子类QAbstractItemModel(KeyframeModel)和QListWidgetItem(TileWidgetItem) 经过一些编码,它看起来像我想要的,但当我添加一些项目时,QListWidget(网格模式)发生了奇怪的事情。在我的例子中,QListWidget是可调整大小的(取决于它嵌入主布局的方式),列数应该取决于列表和项目宽度。物品的大小是固定的(至少目前是这样)。 但是当我调整列表的大小时,其中一个列表宽度的项目没有对齐,我不知道发生了什么。以下是应用程序的te屏幕截图: 调整大小#2比调整大小#1宽几个像素,而调整大小#1很难获得(边框大小写)-宽度少几个像素,我有两列(没关系),但多了一些像素,我就得到了大小写#2。 在所有情况下,列数都可以 有时,在程序立即启动后,最后一项也会出现偏差,如图1所示(在启动后,如图1所示,但尽管列表宽度相同,但可以看到不同的结果)。 我想知道为什么启动后会如此不一致 我错过什么了吗?我必须以不同的方式完成某些部分吗? 或者只是调试模式中的一些小故障 下面我发布了一些代码: 应用程序:C++ QListWidget项目一致性定位问题,c++,qt,qlistwidget,C++,Qt,Qlistwidget,我不熟悉Qt框架。在新的应用程序中,我想创建带有自定义项目的列表。这些项目非常简单,必须包含标题标签、缩略图和描述标签() 现在我不想玩自定义绘图和所有这些东西,因为我认为使用适当的小部件/布局可以更容易地完成我想要的项目,所以我决定使用QListwidget和子类QAbstractItemModel(KeyframeModel)和QListWidgetItem(TileWidgetItem) 经过一些编码,它看起来像我想要的,但当我添加一些项目时,QListWidget(网格模式)发生了奇怪的
// Source file
QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//--------------------------------------------------------------------------------
// Add elements to the list
TileWidgetItem *item = new TileWidgetItem();
item->setData(TileWidgetItem::TileRole::TitleRole, QVariant("Long title"));
item->setData(TileWidgetItem::TileRole::DescriptionRole, QVariant("My long info"));
item->setText("My super text");
qDebug() << "Widget size hint: " << item->sizeHint();
ui.listWidget_moves->addItem(item);
item->updateView();
TileWidgetItem *item1 = new TileWidgetItem();
item1->setData(TileWidgetItem::TileRole::TitleRole, QVariant("Item #2"));
item1->setText("Tile #2");
ui.listWidget_moves->addItem(item1);
item1->updateView();
TileWidgetItem *item2 = new TileWidgetItem();
ui.listWidget_moves->addItem(item2);
item2->updateView();
TileWidgetItem *item3 = new TileWidgetItem();
ui.listWidget_moves->addItem(item3);
item3->updateView();
//--------------------------------------------------------------------------------
// Adjust cell size
QSize cellSize;
for (uint i = 0; i < ui.listWidget_moves->count(); i++)
{
int dim = ui.listWidget_moves->item(i)->sizeHint().height();
if (dim > cellSize.height())
cellSize.setHeight(dim);
dim = ui.listWidget_moves->item(i)->sizeHint().width();
if (dim > cellSize.width())
cellSize.setWidth(dim);
}
ui.listWidget_moves->setGridSize(cellSize);
}
// Source file
constexpr int MAX_THUMB_SIZE = 100;
TileWidgetItem::TileWidgetItem(QListWidget *listview)
: QListWidgetItem(listview, ItemType::UserType)
{
/* Prepare main widget */
QWidget *view = new QWidget();
view->setObjectName("tile");
view->setStyleSheet(
"QWidget#tile { margin: 4 8; background-color: #404040; border: 1 solid rgba(0,0,0,30%); border-radius: 4px }\n"
"QWidget#tile::hover { border: 1 solid #EEE; background-color: #484848 }\n"
"QWidget#tile[selected=true] { background-color: #00F }"
);
//-----------------------------------------------------------
/* Prepare layout */
QVBoxLayout *layout = new QVBoxLayout();
layout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
//-----------------------------------------------------------
/* Prepare title with icon */
QHBoxLayout *titleLayout = new QHBoxLayout();
QLabel *titleIcon = new QLabel();
titleIcon->setObjectName("titleIcon");
titleIcon->setStyleSheet("background-color: black");
titleIcon->setFixedSize(QSize(16, 16));
titleLayout->addWidget(titleIcon);
QLabel *title = new QLabel("Title");
title->setObjectName("title");
title->setMinimumWidth(60);
title->setStyleSheet("background-color: #800;");
titleLayout->addWidget(title);
QWidget *titleWidget = new QWidget();
titleWidget->setStyleSheet("background-color: #080");
titleWidget->setLayout(titleLayout);
layout->addWidget(titleWidget);
//-----------------------------------------------------------
/* Prepare thumbnail */
QLabel *thumbnail = new QLabel();
thumbnail->setObjectName("thumbnail");
thumbnail->setStyleSheet("background-color: black; border: 1 solid #F00");
thumbnail->setFixedSize(QSize(MAX_THUMB_SIZE, MAX_THUMB_SIZE * 0.7f));
thumbnail->setPixmap(QPixmap("Resources/moto.jpg").scaledToWidth(MAX_THUMB_SIZE));
layout->addWidget(thumbnail);
//-----------------------------------------------------------
/* Preparing additional info */
QLabel *description = new QLabel("Description");
description->setObjectName("description");
//description->setToolTip("Custom info tip");
description->setContentsMargins(4, 2, 4, 2);
layout->addWidget(description);
//-----------------------------------------------------------
view->setLayout(layout);
_customView = view;
_titleView = title;
_descriptionView = description;
setSizeHint(_customView->sizeHint());
updateView();
}
TileWidgetItem::~TileWidgetItem()
{
}
void TileWidgetItem::setData(int role, const QVariant &value)
{
QListWidgetItem::setData(role, value);
if (value.type() == QVariant::Type::String)
{
if (role == TileRole::TitleRole)
{
this->_titleView->setText(value.toString());
}
else if (role == TileRole::DescriptionRole)
{
this->_descriptionView->setText(value.toString());
}
setSizeHint(_customView->sizeHint());
}
}
void TileWidgetItem::updateView()
{
if (listWidget() != nullptr)
{
listWidget()->setItemWidget(this, this->_customView);
}
}
平台:Windows 10Qt版本:5.14.2
IDE:visualstudio2019(使用Qt与工具)最后,我使用了自定义委托,解决了问题
我想过度使用这个系统,结果我被打败了:)最后我只使用了自定义委托,解决了问题
我想过度使用系统,但在中我被打败了:)欢迎来到stackoverflow,请特别阅读并提供一个我不会做的:
QWidget*view=new QWidget()
没有QWidgetList
,只有QListWidget
。这意味着,不会列出小部件,而是列出项目的小部件。这两者之间有着根本的区别。小部件使用布局来自动调整其子部件的大小和位置,这很方便,但它们也参与事件循环并对其加载,这使得应用程序的性能迅速恶化。这就是这些物品的用途。它们是轻量级对象,与小部件不同,它们使用委托来显示内容。它们不应该加载小部件,因为这会破坏它们的用途。我必须坚持模型/视图方法并创建自定义委托。我相信这是实现您想要的目标的正确方法。欢迎使用stackoverflow,请特别阅读并提供一个我不会做的:QWidget*view=new QWidget()
没有QWidgetList
,只有QListWidget
。这意味着,不会列出小部件,而是列出项目的小部件。这两者之间有着根本的区别。小部件使用布局来自动调整其子部件的大小和位置,这很方便,但它们也参与事件循环并对其加载,这使得应用程序的性能迅速恶化。这就是这些物品的用途。它们是轻量级对象,与小部件不同,它们使用委托来显示内容。我必须坚持模型/视图方法,创建自定义委托。我相信这是实现您想要的目标的正确方法。
// Header file
class TileWidgetItem : public QListWidgetItem
{
public:
enum TileRole
{
TitleRole = Qt::UserRole + 1,
DescriptionRole,
ThumbnailRole
};
public:
TileWidgetItem(QListWidget *listview = nullptr);
~TileWidgetItem();
void setData(int role, const QVariant &value) override;
void updateView();
QWidget *customView() const { return _customView; };
QString getTitle() const { return _titleView->text(); };
QString getInfo() const { return _descriptionView->text(); };
private:
QWidget *_customView;
QLabel *_titleView;
QLabel *_descriptionView;
};