QListWidgetItem - 调整内容的宽度和高度

Nik*_*las 5 c++ qt qwidget qlabel qlistwidgetitem

我有一个QListWidgetItem,有一个QWidget和一些QLabels.标签的高度(imageLabel,titleLabeldescriptionLabel)取决于文本长度变化.QWidget的高度也是如此,它可以提供不同的尺寸QListWidgetItem.到目前为止,参数setSizeHint是静态的:

QListWidgetItem* listWidgetItem = new QListWidgetItem();
listWidgetItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
listWidgetItem->setSizeHint(200, 180));

QWidget* widget = new QWidget();

QVBoxLayout* rootLayout = new QVBoxLayout();
rootLayout->setAlignment(Qt::AlignTop);

QHBoxLayout* contentLayout = new QHBoxLayout();
contentLayout->setAlignment(Qt::AlignLeft);

QLabel* imageLabel = new QLabel();
imageLabel->setPixmap(pixmap);

contentLayout->addWidget(imageLabel, 0, Qt::AlignTop);

QVBoxLayout* informationLayout = new QVBoxLayout();
informationLayout->setAlignment(Qt::AlignTop);

QLabel* titleLabel = new QLabel("<b>" + title  + "</b>");
titleLabel->setWordWrap(true);
informationLayout->addWidget(titleLabel);

QLabel* descriptionLabel = new QLabel(description);
descriptionLabel->setWordWrap(true);
informationLayout->addWidget(descriptionLabel);

QLabel* dateLabel = new QLabel(date.toString());
informationLayout->addWidget(dateLabel);

contentLayout->addLayout(informationLayout);

rootLayout->addLayout(contentLayout);

QHBoxLayout* buttonLayout = new QHBoxLayout();
QPushButton* buttonOne = new QPushButton(tr("Button 1"));
QObject::connect(buttonOne, SIGNAL(clicked()), mButtonOneSignalMapper, SLOT(map()));
mButtonOneSignalMapper->setMapping(buttonOne, index);
buttonLayout->addWidget(buttonOne);

QPushButton* buttonTwo = new QPushButton(tr("Button 2"));
QObject::connect(buttonTwo, SIGNAL(clicked()), mButtonTwoSignalMapper, SLOT(map()));
mButtonTwoSignalMapper->setMapping(buttonTwo, index);
buttonLayout->addWidget(buttonTwo);

rootLayout->addLayout(buttonLayout);

widget->setLayout(rootLayout);

mListWidget->addItem(listWidgetItem);
mListWidget->setItemWidget(listWidgetItem, widget);
Run Code Online (Sandbox Code Playgroud)

有什么办法来正确设置sizeHint关于在使用的显示内容的宽度和高度QLabelsQWidget

例如,第一个QListWidgetItem可以具有descriptionLabel300个字符的文本长度,第二个QListWidgetItem可以具有descriptionLabel1000个字符的文本长度.到目前为止,两者QListWidgetItems都具有相同的尺寸(200px宽度和180px高度).虽然它可能适合第一个QListWidgetItem,因为它只有300个字符,它可能不适合第二个QListWidgetItem,因为1000个字符.因此,我想以某种方式动态调整QListWidgetItem关于所需空间的大小(第一个将需要少于第二个).

Joh*_*unk 0

在我看来,您将无法获得标签的正确边界矩形,除非您知道它的未来宽度,以便您可以计算显示内容所需的行数。在计算其他小部件的布局之前,您不会获得宽度。

另一种方法可能是使用项目委托。它的 sizeHint 方法有一个带有初步矩形的选项参数,您可以从中使用宽度并使用字体度量计算高度。

关于您的其他小部件,您可以切换到 QTableWidget 并将它们放在其他列中。

以下代码不是一个有效的示例..只是一些帮助您入门的线索..

class ItemDelegate : public QStyledItemDelegate
{
public:

    void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
        painter->save();

        QStyledItemDelegate::paint(painter,option,index);

        QString title = index.data(Qt::UserRole).toString();
        QFont f = option.font;
        painter->setFont(f);
        QFontMetrics fm(f);

        QRect r = option.rect;
        // r = r.adjusted(0, fm.lineSpacing(), 0, 0);
        painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, title, &r);

        painter->restore();
    }

    QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
    {
        QFont f = option.font;
        QRect r = option.rect;
        QFontMetrics fm(f);
        QString title = index.data(Qt::UserRole).toString();
        QRect br = fm.boundingRect(r,Qt::AlignTop|Qt::AlignLeft | Qt::TextWordWrap,title);
        return QSize(option.rect.width(),br.height());
    }
};
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你,

约翰内斯