Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Using QAction::setData , in order to pass my own defined object , can not get the object

    QML and Qt Quick
    3
    3
    2505
    Loading More Posts
    • 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.
    • S
      stabber last edited by

      In my project , we must pass our own defined object when menu clicked.

      this is demo code:
      @
      #include "customdefinedemo.h"
      #include "qnewitem.h"

      Q_DECLARE_METATYPE(QNewItem)

      customDefineDemo::customDefineDemo(QWidget *parent, Qt::WFlags flags)
      : QMainWindow(parent, flags)
      {
      ui.setupUi(this);
      connect(ui.pushButton , SIGNAL(clicked()),this,SLOT(onBtnClick()));
      }

      customDefineDemo::~customDefineDemo()
      {

      }

      void customDefineDemo::onBtnClick()
      {
      QMenu* pMenu = new QMenu();
      QNewItem* pNewItem = new QNewItem();
      qint16 nAge = 34;
      QVariant qv;
      do
      {
      QAction* pPortraitView = new QAction(tr("New"), this);
      QAction* pAddGroup = new QAction(tr("Open"), this);
      QAction* pDeleteGroup = new QAction(tr("Save"), this);

      pNewItem->setUserInfo(tr("Eric"),nAge);

      pPortraitView->setData( QVariant::fromValue((QObject*)pNewItem) ) ;

      connect(pPortraitView,SIGNAL(triggered()),this,SLOT(triggeredMenuSendMsgContact()));

      pMenu->addAction(pPortraitView);
      pMenu->addAction(pAddGroup);
      pMenu->addAction(pDeleteGroup);
      pMenu->exec(QCursor::pos());
      } while (false);
      delete pMenu;
      }

      void customDefineDemo::triggeredMenuSendMsgContact()
      {
      QAction* pSendMsg= NULL;
      QNewItem* pObj = NULL;
      do
      {
      pSendMsg=qobject_cast<QAction*>(sender());

      *pObj = pSendMsg->data().value<QNewItem>();
      } while (false);
      }
      @

      QNewItem class is own defined , I wanna pass it when menu clicked,but I can not get it ! Where is error? thanx

      1 Reply Last reply Reply Quote 0
      • D
        dmcr last edited by

        Please use the code tags for a better visibility....
        Also is this really related with QML ?

        dmcr

        1 Reply Last reply Reply Quote 0
        • L
          lgeyer last edited by

          Either <code>QNewItem</code> inherits <code>QObject</code> in which case <code>Q_DECLARE_METATYPE(QNewItem)</code> is wrong, because the type is non-copyable or <code>QNewItem</code> does not inherit <code>QObject</code> in which case the cast to <code>QObject*</code> is wrong (do not use C-style casts in C++).

          Generally you set a <code>QVariant</code> containing a <code>QObject*</code>, expect to get a <code>QVariant</code> containing a <code>QNewItem</code> and try to copy-construct it in a <code>nullptr QNewItem</code> object; this isn't going to happen.

          @
          class QNewItem : public QObject { ... };

          Q_DECLARE_METATYPE(QNewItem*)

          pPortraitView->setData(QVariant::fromValue(pNewItem));

          pObj = pSendMsg->data().value<QNewItem*>();
          delete pObj;
          @

          1 Reply Last reply Reply Quote 0
          • First post
            Last post