Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qtableview mouse move
QtWS25 Last Chance

Qtableview mouse move

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstyleditemdeletableview
7 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    charry
    wrote on last edited by
    #1

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

    Pl45m4P 1 Reply Last reply
    -1
    • C charry

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

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @charry

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


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      C 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @charry

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

        C Offline
        C Offline
        charry
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          C 1 Reply Last reply
          1
          • C Offline
            C Offline
            charry
            wrote on last edited by
            #5

            #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();
            

            }

            1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @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?

              C Offline
              C Offline
              charry
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved