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. QTableWidget Drag and Drop
Forum Updated to NodeBB v4.3 + New Features

QTableWidget Drag and Drop

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.2k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ASxa86
    wrote on last edited by
    #1

    First off, I'm aware of the many posts asking how to do this both here and on stack overflow. Unfortunately none of them have been able to help me get to where I need.

    I have 2 QTableWidgets. PlayerTable and RosterTable. I'm trying to drag a row of player data from the PlayerTable to the RosterTable. This row of data is represented by a shared_ptr to a Player object.

    I have inherited QMimeData and made my own class PlayerMimeData to make life easier and skipping over the need to serialize my data.

    The only thing I've seen capable of loading my own custom QMimeData would possibly be a QDrag object. This would require me to overload keypress events and drag event which i would like to avoid if possible.

    @
    class PlayerMimeData : public QMimeData
    {
    public:
    PlayerMimeData(std::shared_ptr<Player> player);

          void setPlayer(std::shared_ptr<Player> player);
          std::shared_ptr<Player> getPlayer() const;
    

    };
    @

    I tried overloading ::mimeData(). This does not seem to work and in fact disables dropping. I assume this is due to missing information in my mimeData.
    @
    QMimeData* PlayerTable::mimeData(const QList<QTableWidgetItem*> items) const
    {
    // Simple example grabbing first item in list
    const auto player = this->getPlayer(this->row(items[0]));
    const auto playerMimeData = new PlayerMimeData(player);

    // MimeData holds a reference to player
    this->removePlayer(player);
    
    return playerMimeData;
    

    }
    @

    End goal:
    @
    void RosterTable::dropEvent(QDropEvent* event)
    {
    const auto playerMimeData = qobject_cast<const PlayerMimeData*>(event->mimeData());
    const auto player = playerMimeData->getPlayer();

    if(player != nullptr)
    {
    this->addPlayer(player);
    }
    }
    @

    Any suggestions for loading my custom PlayerMimeData so that dropEvent will receive it? Or any suggestions for getting a shared_ptr<Player> into a dropEvent?

    1 Reply Last reply
    0

    • Login

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