Getting information about dragged item - QTreeWidgetItem

  • Hello,

    I have a QTreeWidget with only one column.
    This column has one top item with some subitem, and some of these subitem have some subsubitem:


    Only subsubitem are draggable.

    QByteArray e = event->mimeData()->data("application/x-qabstractitemmodeldatalist");
    		QDataStream stream(&e, QIODevice::ReadOnly);
    		QString dropped;
    		while (!stream.atEnd()) {
    			int row, col;
    			QMap<int, QVariant> roleDataMap;
    			stream >> row >> col >> roleDataMap;
    			dropped = roleDataMap[0].toString();

    This get the text of the item, col is always 0 but row isn't enough to tell which item is parent.
    If i drag subsubitem3, row = 2, being an index only tied to the parent item, how to get the parent, or how to solve this?
    Note that I'm dropping into a QTextEdit of another class, I can't access my QTreeWidget from there

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What exactly do you want to get in your QTextEdit ?

  • The handling of the drag and drop is done already, but depending on the parent item, i need to use a different member
    The QTextEdit doesn't matter, that part is working, i just need to get the parent of the item

  • Lifetime Qt Champion

    In that case, shouldn't you rather provide everything in the dragged data ? Otherwise, you'll be starting a tight coupling between widgets who shouldn't need to know anything from each other.

  • Well, I wouldn't be asking here if I knew what I should do.
    Let's breakdown this more, the app is an editor, the QTreeWidget item which are draggable are filename (not complete, only filename) but i do have a vector of complete filename to be used when we need to open that file, I do not want to display the complete filename in the tree.
    While working on the drag and drop into the Qtextedit, i only used the filename i had in the tree as testing purpose (dropping on a tabbar of a QTabWidget which contains the QTextedit creates a new tab, but if you drop on the qtextedit at some specific part, it splits the view, and create a new qtabwidget with a tab where the text of that tab is 'dropped' from the code in my first post.
    But to access the corresponding complete filename in my vector to open the file, i need to use the index of item in the tree.
    However I do have a problem of 'communication' between my class. At first i was just playing around so i made a main class inheriting qmainwindow which has my tree and the different vectors, but I made a second class inheriting QTabwidget and all the drag and drop event of the QTextedit are then handled from that class. Are you suggesting that I get rid of that second class and that i put everything in my main class? I admit that when i started I was unsure about signals and i thought i had to create another class to access the event such as drag&drop here, but i could use signals from my main class, correct?

  • Lifetime Qt Champion

    In that case, it looks like a QTreeView with a custom model would be better.

    From that model, you can then provide all the data that your various widgets need through QMimeData and thus make them independent of the source.

Log in to reply