Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qtableview mouse move



  • the picture is I use QStyledItemDelegate when mouse move table cell size.
    123.jpg



  • @charry

    What do you want to tell us with this? There is no question in your post.



  • @Pl45m4 Drag the mouse to change the spacing of the table, but it will produce a shadow for QStyledItemDelegate


  • Lifetime Qt Champion

    @charry said in Qtableview mouse move:

    but it will produce a shadow for QStyledItemDelegate

    And what's the question now? How should we know what you're doing? Esp. when there is no code at all?



  • #pragma once
    #include <QApplication>
    #include <QTableView>
    #include <QStandardItemModel>
    #include <QStyledItemDelegate>
    #include <QHeaderView>
    #include <QPushButton>
    #include <QStyleOptionViewItem>

    class TailButtonsDelegate : public QStyledItemDelegate
    {
    Q_OBJECT
    Q_DISABLE_COPY(TailButtonsDelegate)

    public:
    explicit TailButtonsDelegate(QObject *parent = Q_NULLPTR);
    ~TailButtonsDelegate();

    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE;
    
    QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE;
    
    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE;
    
    void setEditorData(QWidget* editor, const QModelIndex& index) const Q_DECL_OVERRIDE;
    
    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const Q_DECL_OVERRIDE;
    
    void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE;
    
    bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) Q_DECL_OVERRIDE;
    

    };

    #include "TailButtonsDelegate.h"
    #include <QMouseEvent>

    TailButtonsDelegate::TailButtonsDelegate(QObject* parent)
    : QStyledItemDelegate(parent)
    {

    }

    TailButtonsDelegate::~TailButtonsDelegate()
    {
    }

    void TailButtonsDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    Q_ASSERT(index.isValid());
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);
    const QWidget* widget = option.widget;
    QStyle* style = widget ? widget->style() : QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    if (!(option.state & QStyle::State_Selected))
    return;
    QStyleOptionButton editButtonOption;
    editButtonOption.text = QString(QChar(0x270D)); //use emoji for text, optionally you can use icon + iconSize
    editButtonOption.rect = QRect(option.rect.left() + option.rect.width() - (2 * option.rect.height()), option.rect.top(), option.rect.height(), option.rect.height());
    editButtonOption.features = QStyleOptionButton::None;
    editButtonOption.direction = option.direction;
    editButtonOption.fontMetrics = option.fontMetrics;
    editButtonOption.palette = option.palette;
    editButtonOption.styleObject = option.styleObject;
    QStyleOptionButton removeButtonOption(editButtonOption);
    removeButtonOption.text = QString(QChar(0x274C)); //use emoji for text, optionally you can use icon + iconSize
    removeButtonOption.rect = QRect(option.rect.left() + option.rect.width() - option.rect.height(), option.rect.top(), option.rect.height(), option.rect.height());
    style->drawControl(QStyle::CE_PushButton, &editButtonOption, painter, widget);
    style->drawControl(QStyle::CE_PushButton, &removeButtonOption, painter, widget);

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

    }

    QSize TailButtonsDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {
    const QSize baseSize = QStyledItemDelegate::sizeHint(option, index);
    return QSize(baseSize.width() + (2 * baseSize.height()), baseSize.height());
    }

    QWidget* TailButtonsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    QWidget* result = new QWidget(parent);
    result->setGeometry(option.rect);
    result->setStyleSheet("background:white;");

    QWidget* baseEditor = QStyledItemDelegate::createEditor(result, option, index);
    baseEditor->setObjectName("baseEditor");
    baseEditor->setStyleSheet("border: none;");
    
    baseEditor->setGeometry(0, 0, option.rect.width() - (2 * option.rect.height()), option.rect.height());
    QPushButton* editButton = new QPushButton(QChar(0x270D), result);
    editButton->setObjectName("editButton");
    editButton->setGeometry(option.rect.width() - (2 * option.rect.height()), 0, option.rect.height(), option.rect.height());
    connect(editButton, &QPushButton::clicked, []() {qDebug("Edit"); });
    QPushButton* removeButton = new QPushButton(QChar(0x274C), result);
    removeButton->setObjectName("removeButton");
    removeButton->setGeometry(option.rect.width() - option.rect.height(), 0, option.rect.height(), option.rect.height());
    connect(removeButton, &QPushButton::clicked, []() {qDebug("Remove"); });
    
    return result;
    

    }

    void TailButtonsDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const {
    QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
    Q_ASSERT(baseEditor);
    QStyledItemDelegate::setEditorData(baseEditor, index);
    }

    void TailButtonsDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const {
    QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
    Q_ASSERT(baseEditor);
    QStyledItemDelegate::setModelData(baseEditor, model, index);
    }

    void TailButtonsDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    Q_UNUSED(index)
    editor->setGeometry(option.rect);
    QWidget* baseEditor = editor->findChild<QWidget*>("baseEditor");
    Q_ASSERT(baseEditor);
    baseEditor->setGeometry(0, 0, option.rect.width() - (2 * option.rect.height()), option.rect.height());
    QWidget* editButton = editor->findChild<QWidget*>("editButton");
    Q_ASSERT(editButton);
    editButton->setGeometry(option.rect.width() - (2 * option.rect.height()), 0, option.rect.height(), option.rect.height());
    QWidget* removeButton = editor->findChild<QWidget*>("removeButton");
    Q_ASSERT(removeButton);
    removeButton->setGeometry(option.rect.width() - option.rect.height(), 0, option.rect.height(), option.rect.height());
    editor->setGeometry(option.rect);
    }

    bool TailButtonsDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
    {
    switch (event->type())
    {
    case QEvent::MouseButtonRelease:
    {
    QMouseEvent* pME = static_cast<QMouseEvent*>(event);
    if (pME->button() == Qt::LeftButton)
    {
    QAbstractItemView* pView = qobject_cast<QAbstractItemView*>(const_cast<QWidget*>(option.widget));
    if (pView != nullptr)
    {
    pView->findChild<QWidget*>("baseEditor")->setFocus();

    			//	pView->currentIndex();
    		//		pView->findChild<QWidget*>("editButton")->setStyleSheet("background:white;");
    		//		pView->findChild<QWidget*>("removeButton")->setStyleSheet("background:white;");
    		}
    		//		return true;
    	}
    }
    case QEvent::MouseMove:
    {
    	QAbstractItemView* pView = qobject_cast<QAbstractItemView*>(const_cast<QWidget*>(option.widget));
    	if (pView != nullptr)
    	{
    		
    //		pView->findChild<QWidget*>("baseEditor")->clearFocus();
    	}
    }
    default:
    	break;
    }
    
    return QStyledItemDelegate::editorEvent(event, model, option, index);
    

    }

    #include <QtCore/QCoreApplication>
    #include "TailButtonsDelegate.h"
    #include <QTableWidgetItem>

    int main(int argc, char *argv[])
    {
    // QCoreApplication a(argc, argv);

    // return a.exec();

    QApplication app(argc, argv); 
    
    QStandardItemModel baseModel;
    baseModel.insertRows(0, 20);
    baseModel.insertColumns(0, 4);
    for (int i = 0; i < 20; ++i) {
        for (int j = 0; j < 4; ++j) {
            baseModel.setData(baseModel.index(i, j), QStringLiteral("%1,%2").arg(i).arg(j));
        }
    }
    
    
    
    QTableView view;
    
    QPalette palette = view.palette();
    palette.setBrush(QPalette::Highlight, QColor(0, 120, 215));
    palette.setBrush(QPalette::HighlightedText, QBrush(Qt::black));
    palette.setBrush(QPalette::Shadow, QBrush(Qt::transparent));
    view.setPalette(palette);
    
    view.setModel(&baseModel);
    
    view.setEditTriggers(QTableView::AllEditTriggers);
    
    view.setSelectionMode(QAbstractItemView::SingleSelection);
    

    // view.setSelectionBehavior(QTableView::SelectItems);
    view.setSelectionBehavior(QTableView::SelectRows);

    view.horizontalHeader()->setStretchLastSection(true);
    view.setItemDelegateForColumn(3, new TailButtonsDelegate(&view));
    
    view.resize(800,600);
    
    view.show();
    return app.exec();
    

    }



  • @Christian-Ehrlicher sorry,I not englisher. my problem: Click the fourth column and drag the mouse to change the spacing of the table, the window is not paint in time


  • Lifetime Qt Champion

    Please take a look at your post above - do you really think we can read something meaningful out of it. Please use the <code> - tags to make it more readable and reduce your code as much as possible so only the problem and nothing else is in there.