簡體   English   中英

QListWidgetItem - 調整內容的寬度和高度

[英]QListWidgetItem - adjust width and height to content

我有一個QListWidgetItem ,它有一個QWidget和一些QLabels 標簽的高度( imageLabeltitleLabeldescriptionLabel )取決於文本長度。 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);

有沒有辦法正確設置sizeHint有關QWidgetQLabels中使用的顯示內容的寬度和高度?

例如,第一QListWidgetItem可以具有descriptionLabel為300個字符的文本的長度和所述第二QListWidgetItem可以具有descriptionLabel具有1000個字符的文本的長度。 到目前為止,兩個QListWidgetItems都具有相同的大小(200px寬度和180px高度)。 雖然它可能適合第一個QListWidgetItem ,因為它只有300個字符,但由於1000個字符,它可能不適合第二個QListWidgetItem 因此,我想以某種方式動態調整QListWidgetItem的大小,關於所需的空間(第一個將需要少於第二個)。

我看到它的方式,你將無法獲得標簽的正確邊界矩形,除非你知道它的未來寬度,以便你可以計算顯示內容所需的行數。 並且在計算其他小部件的布局之前,您將無法獲得寬度。

另一種方法可能是使用項目委托。 它的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());
    }
};

希望能幫助到你,

約翰內斯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM