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. [SOLVED] Writing to QDataStream causes segmentation fault
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Writing to QDataStream causes segmentation fault

Scheduled Pinned Locked Moved General and Desktop
22 Posts 6 Posters 10.8k 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.
  • B Offline
    B Offline
    butterface
    wrote on last edited by
    #13

    The "funny" thing is if I step through with the debugger the value is written to the stream in the subclass but if the program runs free it is not. I can see the qDebug message right before the write to the datastream in any case but the data is only writte in debug mode?!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #14

      Well, so far you are still not showing us how class A and B are related, so it is very hard to get an idea of what is actually going on in your code.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        butterface
        wrote on last edited by
        #15

        Sorry for asking, but is there another relation between them then subclassing, i. e. B is a subclass of A.
        I can post the exact class definition when I am back home.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          butterface
          wrote on last edited by
          #16

          And here comes definition of class B

          @
          class B : public A
          {
          Q_OBJECT
          public:
          B(QWidget *parent = NULL, bool acceptDrops = true);
          virtual ~B();

          protected:
          void paintEvent(QPaintEvent *);
          void mousePressEvent(QMouseEvent *);
          };
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #17

            In class A's mousePressEvent function, you set the mime data in the QDrag object in line 11:

            @
            mimeData->setData("application/x-dnditemdata", *itemData);
            @

            At this point in time, only the QPoint data has been written to the QDataStream and its underlying byte array.

            Once you come back to class B's mousePressEvent function, you write some more data to the byte array, but that never reaches the QDrag object! QDrag takes a copy of the data. At the moment you write to the array in class B, the implicit sharing creates a deep copy of the array and in turn, you end up with two QByteArrays: one that's within the drag object, and one in class B, which is never used again.

            As a side note: Your split of code looks very strange to me and in my opinion it cries for additional trouble in the future.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • B Offline
              B Offline
              butterface
              wrote on last edited by
              #18

              Okay, thanks for that so far.

              What would you suggest for the setting of the QByteArray? The point is, I want to make sure that subclasses don't need to take care about those things. I want the base class to take care of the clicked points and all the things which come again and again (like the definition of when I make a base class). If there is no other way, I can move the whole dnd code to the subclasses of course.
              What do you mean by "it looks strange"? I actually took the dnd example from the website and changed a few things like adding my own object.

              So please let me know how to make it better and what you would suggest for the problem of the subclass datastream writing.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #19

                I would split out the preparation of the mime data into some other virtual functions. Only implement the mousepress event handler in class A and call the new functions from that. Basicall like this:

                @
                void A::mousePressEvent(QMouseEvent *event)
                {
                QByteArray itemData;
                QDataStream dataStream(&itemData, QIODevice::WriteOnly);

                // the new function:
                prepareDragData(event, dataStream);
                
                QMimeData *mimeData = new QMimeData;
                mimeData->setData("application/x-dnditemdata", itemData);
                drag = new QDrag(this);
                drag->setMimeData(mimeData);
                drag->setPixmap(QPixmap::grabWidget(this));
                drag->setHotSpot(event->pos());
                
                if (acceptDrops())
                    drag->exec(Qt::MoveAction);
                else
                    drag->exec(Qt::CopyAction);
                

                }

                void A::prepareDragData(QMouseEvent *event, QDataStream &stream)
                {
                dataStream << QPoint(event->pos());
                }

                void B::prepareDragData(QMouseEvent *event, QDataStream &stream)
                {
                A::prepareDragData(event, stream);
                dataStream << 25;
                }
                @

                Additional notes:

                • no more need of class variables for the dataStream
                • no need for a QBuffer, the dataStream can work directly on a QByteArray

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #20

                  I think Volkers suggestion makes a lot of sense. However, perhaps you should even considder taking it a step further. Does your drag target really need to know the QPoint where the drag started? Or is it really only interested in some kind of handle to the object that is being dragged, and the type of that object? Usually, in my experience, it is the latter...

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    butterface
                    wrote on last edited by
                    #21

                    Andre, I am actually not really interested in this specific point. It was just the implementation from the example but there will be other data which I really need.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      butterface
                      wrote on last edited by
                      #22

                      FYI: I changed the whole thing now to use QGraphicsScene which is much easier to use and dose all the d'n'd stuff and mouse handling by itself.

                      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