mardi 13 octobre 2020

How to properly enlarge a QWidget that is dropped inside a QGraphicsView using QGraphicsProxyWidget approach

How do I enlarge a QWidget that is dropped inside a QGraphicsView using QGraphicsProxyWidget approach?

I did extensive research about this topic and came across this great post and this post which I applied to my case as I have a very similar situation.

The problem I have is that I am not able to enlarge the widget (in my case a QTableWidget).

The post I mentioned proceed to subclass the widget desired and add an additional signals called sizeChanged(); and that is what I did but unfortunately nothing happens and I am wondering why the QTableWidget is not enlarging properly, actually nothing happens.

pr3

secene.h

class Scene : public QGraphicsScene {

    enum LaserTableWidget {
      Images,
      Path
    };
    Q_ENUM(LaserTableWidget)

public:
  Scene(QObject *parent = nullptr);

  void compare(const QString& comp);

  template<typename QEnum>
  std::string QtEnumToString (const QEnum value)
  {
    return std::string(QMetaEnum::fromType<QEnum>().valueToKey(value));
  }

protected:
  void dropEvent(QGraphicsSceneDragDropEvent *event);
};

secene.cpp

#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
#include <QTableWidget>
#include <QGraphicsProxyWidget>
#include <QVBoxLayout>
#include <QMetaEnum>

#include "customtablewidget.h"


Scene::Scene(QObject *parent) : QGraphicsScene(parent) {
    setBackgroundBrush(Qt::lightGray);
}


void Scene::dropEvent(QGraphicsSceneDragDropEvent *event) {

  QByteArray encoded = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
  QDataStream stream(&encoded, QIODevice::ReadOnly);

  QStringList rosTables;

  while (!stream.atEnd()) {
    int row, col;
    QMap<int, QVariant> roleDataMap;
    stream >> row >> col >> roleDataMap;
    rosTables << roleDataMap[Qt::DisplayRole].toString();
  }

  for (const QString &color : rosTables) {
      QPoint initPos(0,0);

      auto *wgt = new CustomTableWidget({});

      QGraphicsRectItem *proxyControl = addRect(0, 0, wgt->width() - 384, 20, QPen(Qt::black), QBrush(Qt::darkGreen));
      proxyControl->setPos(initPos.x(), initPos.y());
      proxyControl->setFlag(QGraphicsItem::ItemIsMovable, true);
      proxyControl->setFlag(QGraphicsItem::ItemIsSelectable, true);

      wgt->setColumnCount(2);
      wgt->setRowCount(2);
      for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
      {
          for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
          {
              QTableWidgetItem* item = new QTableWidgetItem();
              item->setText(QString("%1").arg(ridx));
              wgt->setItem(ridx,cidx,item);
          }
      }
      QGraphicsProxyWidget * const proxy = addWidget(wgt);
      proxy->setPos(initPos.x(), initPos.y()+proxyControl->rect().height());
      proxy->setParentItem(proxyControl);

      connect(wgt, &CustomTableWidget::sizeChanged, [wgt, proxyControl](){
          proxyControl->setRect(wgt->geometry().adjusted(-10, -10, 10, 10));
      });
  }
}

customtablewidget.h

#ifndef CUSTOMTABLEWIDGET_H
#define CUSTOMTABLEWIDGET_H

#include <QTableWidget>
#include <QResizeEvent>

class CustomTableWidget : public QTableWidget
{
    Q_OBJECT
public:
    CustomTableWidget(QWidget *parent){}
    void resizeEvent(QResizeEvent *event);

signals:
    void sizeChanged();
};

#endif // CUSTOMTABLEWIDGET_H

customtablewidget.cpp

#include "customtablewidget.h"

void CustomTableWidget::resizeEvent(QResizeEvent *event)
{
    QTableWidget::resizeEvent(event);
    sizeChanged();
}

Aucun commentaire:

Enregistrer un commentaire