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. How to add content of text/sql file in QPlainTextEdit on QDropEvent.
Forum Update on Monday, May 27th 2025

How to add content of text/sql file in QPlainTextEdit on QDropEvent.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 526 Views
  • 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.
  • D Offline
    D Offline
    deleted385
    wrote on last edited by deleted385
    #1

    in the CodeEditor, a subclass of QPlainTextEdit I've these:

    void CodeEditor::dragEnterEvent(QDragEnterEvent *event)
    {
        QPlainTextEdit::dragEnterEvent(event);
    }
    void CodeEditor::dropEvent(QDropEvent *event)
    {
        QFile file (event->mimeData()->text().remove("file:///"));
        file.open(QIODevice::ReadOnly);
        QTextStream in(&file);
        qDebug() << in.readAll();
        insertPlainText(in.readAll());
        //setPlainText(in.readAll());
        file.close();
    }
    

    when I drop .txt/..sql file on the CodeEditor, nothing happens:
    x1.gif

    How to add the content? In addition to that I also want to block drop (and change the cursor icon from copy (+) to block) if the file isn't .txt/.sql .

    jsulmJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #7

      I think the issue is/was that you're calling QTextstream::readAll() twice and there's nothing left to read after the first call. QTextstream's documentation doesn't explicitly say so, but
      QIODevice::readAll() says it 'Reads all remaining data from the device'.

      D 1 Reply Last reply
      2
      • D deleted385

        in the CodeEditor, a subclass of QPlainTextEdit I've these:

        void CodeEditor::dragEnterEvent(QDragEnterEvent *event)
        {
            QPlainTextEdit::dragEnterEvent(event);
        }
        void CodeEditor::dropEvent(QDropEvent *event)
        {
            QFile file (event->mimeData()->text().remove("file:///"));
            file.open(QIODevice::ReadOnly);
            QTextStream in(&file);
            qDebug() << in.readAll();
            insertPlainText(in.readAll());
            //setPlainText(in.readAll());
            file.close();
        }
        

        when I drop .txt/..sql file on the CodeEditor, nothing happens:
        x1.gif

        How to add the content? In addition to that I also want to block drop (and change the cursor icon from copy (+) to block) if the file isn't .txt/.sql .

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Emon-Haque said in How to add content of text/sql file in QPlainTextEdit on QDropEvent.:

        nothing happens

        What did you do already to fine out why it does not work?
        Please use debugger and add a break point inside dropEvent and go through step by step.
        Also check what file.open(QIODevice::ReadOnly) returns.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 2 Replies Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by
          #3

          Is the widget set to accept drop events? Do you know if anything in your dropEvent() is being run? Was the file open successful?

          D 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Emon-Haque said in How to add content of text/sql file in QPlainTextEdit on QDropEvent.:

            nothing happens

            What did you do already to fine out why it does not work?
            Please use debugger and add a break point inside dropEvent and go through step by step.
            Also check what file.open(QIODevice::ReadOnly) returns.

            D Offline
            D Offline
            deleted385
            wrote on last edited by
            #4

            @jsulm, this qDebug() << in.readAll() prints out all content in the QtCreator output, I can see content there.

            1 Reply Last reply
            0
            • M mchinand

              Is the widget set to accept drop events? Do you know if anything in your dropEvent() is being run? Was the file open successful?

              D Offline
              D Offline
              deleted385
              wrote on last edited by deleted385
              #5

              @mchinand, widget that contains the CodeEditor or the CodeEditor itself? In the constructor of the CodeEditor I've this setAcceptDrops(true);. Tried the same in the QWidget that contains the CodeEditor, doesn't work.

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Emon-Haque said in How to add content of text/sql file in QPlainTextEdit on QDropEvent.:

                nothing happens

                What did you do already to fine out why it does not work?
                Please use debugger and add a break point inside dropEvent and go through step by step.
                Also check what file.open(QIODevice::ReadOnly) returns.

                D Offline
                D Offline
                deleted385
                wrote on last edited by deleted385
                #6

                @jsulm, somehow your trick works without any change of code. Just clicked the Start debugging of startup project button and kept hitting F5 and now it works!

                I always used qDebug to observe and use Ctrl+R to run the project. Looks like those Play buttons actually have some use.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mchinand
                  wrote on last edited by mchinand
                  #7

                  I think the issue is/was that you're calling QTextstream::readAll() twice and there's nothing left to read after the first call. QTextstream's documentation doesn't explicitly say so, but
                  QIODevice::readAll() says it 'Reads all remaining data from the device'.

                  D 1 Reply Last reply
                  2
                  • M mchinand

                    I think the issue is/was that you're calling QTextstream::readAll() twice and there's nothing left to read after the first call. QTextstream's documentation doesn't explicitly say so, but
                    QIODevice::readAll() says it 'Reads all remaining data from the device'.

                    D Offline
                    D Offline
                    deleted385
                    wrote on last edited by
                    #8

                    @mchinand, correct. I've tried again with qDebug and that's the case.
                    Thanks.

                    1 Reply Last reply
                    1

                    • Login

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