QTableWidget Drag and Drop



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.