我要达到如图所示的效果

我要达到如图所示的效果

我的代码如下。用鼠标单击该项目后,我希望选中一个复选框,并显示指定位置的背景并设置该复选框
希望有人给个好的建议,谢谢

jobDialog::jobDialog(QWidget *parent) : QDialog(parent), ui(new Ui::jobDialog)
{
        ui->setupUi(this);
        ui->listWidget->setViewMode(QListView::IconMode);//
        ui->listWidget->setIconSize(QSize(300, 300));
        ui->listWidget->setSpacing(10);
        ui->listWidget->setResizeMode(QListWidget::Adjust);
        ui->listWidget->setMovement(QListWidget::Static);

        connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this,
            SLOT(getItems(QListWidgetItem *)));

        int n = 10;
        for (int i = 0; i < n; i++) {

            QListWidgetItem *newItem = new QListWidgetItem(
                QIcon(":/res/images/video.png"), " iamges");
            newItem->setSizeHint(QSize(140, 130));

            newItem->setTextAlignment(Qt::AlignCenter);;

            ui->listWidget->addItem(newItem);
        }
}

void jobDialog::getItems(QListWidgetItem *item)
{

        for (int i = 0; i < 10; i++) {
        if (ui->listWidget->item(i) == item) {

        }
    }
}

最佳答案

要在项目上绘制复选框,可以通过启用项目的checked属性来使用QProxyStyle,该复选框的状态通过委托(delegate)进行更改:

proxystyle.h

#ifndef PROXYSTYLE_H
#define PROXYSTYLE_H

#include <QPainter>
#include <QProxyStyle>
#include <QStyleOptionViewItem>

class ProxyStyle: public QProxyStyle{
public:
    using QProxyStyle::QProxyStyle;
    void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const{
        QProxyStyle::drawControl(element, opt, p, w);
        if(element == QStyle::CE_ItemViewItem){
            if(const QStyleOptionViewItem *vopt = qstyleoption_cast<const QStyleOptionViewItem *>(opt)){
                if(vopt->checkState == Qt::Checked)
                    drawCheckBox(*vopt, p);
            }
        }
    }
private:
    void drawCheckBox(const QStyleOptionViewItem & option, QPainter *painter) const{
        QIcon::Mode mode = QIcon::Normal;
        if (!(option.state & QStyle::State_Enabled))
            mode = QIcon::Disabled;
        else if (option.state & QStyle::State_Selected)
            mode = QIcon::Selected;
        QIcon::State state = option.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
        // load icon
        QIcon icon = standardIcon(QStyle::SP_DialogApplyButton);
        // or QIcon icon("/path/of/icon");
        const int size = 16;
        const int margin = 10;
        painter->save();
        QRect iconRect = QRect(option.rect.bottomRight() - (size + margin) * QPoint(1, 1), size * QSize(1, 1));
        icon.paint(painter, iconRect, Qt::AlignCenter, mode, state);
        painter->restore();
    }
};

#endif // PROXYSTYLE_H

styleditemdelegate.h
#ifndef STYLEDITEMDELEGATE_H
#define STYLEDITEMDELEGATE_H

#include <QEvent>
#include <QMouseEvent>
#include <QStyledItemDelegate>

class StyledItemDelegate: public QStyledItemDelegate{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index){
        Qt::ItemFlags flags = model->flags(index);
        if (!(flags & Qt::ItemIsUserCheckable) || !(option.state & QStyle::State_Enabled)
                || !(flags & Qt::ItemIsEnabled))
            return false;
        QVariant value = index.data(Qt::CheckStateRole);
        if (!value.isValid())
            return false;
        if ((event->type() == QEvent::MouseButtonRelease)
                || (event->type() == QEvent::MouseButtonDblClick)
                || (event->type() == QEvent::MouseButtonPress)) {
            if ((event->type() == QEvent::MouseButtonPress)
                    || (event->type() == QEvent::MouseButtonDblClick))
                return true;
            Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
            if (flags & Qt::ItemIsUserTristate)
                state = ((Qt::CheckState)((state + 1) % 3));
            else
                state = (state == Qt::Checked) ? Qt::Unchecked : Qt::Checked;
            return model->setData(index, state, Qt::CheckStateRole);
        }
        return false; //QStyledItemDelegate::editorEvent(event, model, option, index);
    }
protected:
    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{
        QStyledItemDelegate::initStyleOption(option, index);
        // disable draw checkbox
        option->features &= ~QStyleOptionViewItem::HasCheckIndicator;
    }
};

#endif // STYLEDITEMDELEGATE_H
// ...
#include "proxystyle.h"
#include "styleditemdelegate.h"
// ...

jobDialog::jobDialog(QWidget *parent) : QDialog(parent), ui(new Ui::jobDialog)
{
    ui->setupUi(this);
    ui->listWidget->setViewMode(QListView::IconMode);//
    ui->listWidget->setIconSize(QSize(300, 300));
    ui->listWidget->setSpacing(10);
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->listWidget->setMovement(QListWidget::Static);

    ui->listWidget->setStyle(new ProxyStyle(ui->listWidget->style()));
    ui->listWidget->setItemDelegate(new StyledItemDelegate(ui->listWidget));

    int n = 10;
    for (int i = 0; i < n; i++) {
        QListWidgetItem *newItem = new QListWidgetItem(QIcon(":/res/images/video.png"), "");
        newItem->setSizeHint(QSize(140, 130));
        newItem->setTextAlignment(Qt::AlignCenter);
        newItem->setFlags(item->flags() | Qt::ItemIsUserCheckable);
        newItem->setCheckState(Qt::Unchecked);
        ui->listWidget->addItem(newItem);
    }

}

关于c++ - 获取QListWidgetItem x y的坐标,我要达到如图所示的效果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59487683/

10-09 06:33