Results 1 to 10 of 10

Thread: QTextEdit drag and drop problem

  1. #1
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QTextEdit drag and drop problem

    Hi everyone! I'm completly new to qt4 and while writng a simple text editor I encountered a problem. I wanted to enable copying and pasting of images in my program. I found a sample code in QTextEdit documentation but I couldn't implement it in my program. I've been trying to figure this out but unfortunatelly without any success. Here is the code(I've pasted only a code which is responsible for drag and drop of images):
    Qt Code:
    1. //mainwindow.h
    2. class MainWindow : public QMainWindow, private Ui::MainWindow
    3. {
    4. Q_OBJECT
    5.  
    6. public:
    7. explicit MainWindow(QWidget *parent = 0);
    8. void insertFromMimeData( const QMimeData *source );
    9. bool canInsertFromMimeData( const QMimeData *source ) const;
    10.  
    11. //mainwindow.cpp
    12. bool MainWindow::canInsertFromMimeData( const QMimeData *source ) const
    13. {
    14. if (source->hasImage())
    15. return true;
    16. else
    17. return textEdit->canInsertFromMimeData(source);
    18. }
    19. void MainWindow::insertFromMimeData( const QMimeData *source )
    20. {
    21. if (source->hasImage())
    22. {
    23. QImage image = qvariant_cast<QImage>(source->imageData());
    24. QTextCursor cursor = textEdit->textCursor();
    25. QTextDocument *document = textEdit->document();
    26. document->addResource(QTextDocument::ImageResource, QUrl("image"), image);
    27. cursor.insertImage("image");
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    compiler says that this line
    return textEdit->canInsertFromMimeData(source);
    is wrong. I guess that it's a very simple mistake but I would be really grateful if someone could help me with it.
    Cheers!
    Last edited by high_flyer; 21st February 2011 at 10:13. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit drag and drop problem

    compiler says that this line
    return textEdit->canInsertFromMimeData(source);
    is wrong.
    Post the error please, usually the compiler tells you exactly what is the problem.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit drag and drop problem

    Here you are. The error is:
    virtual bool QTextEdit::canInsertFromMimeData(const QMimeData*) const is protected

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit drag and drop problem

    Great.
    Do you know what a protected method is?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit drag and drop problem

    I guess so. I've been searching for more information on this subject but still i don't know why the error is here. It it is protected the instance of QTextEdit - textEdit should have access to this method, shouldn't it?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit drag and drop problem

    I guess so.
    Well, with programming you should not 'guess' - you should know!
    Make sure you understand what public, protected and private are.

    t it is protected the instance of QTextEdit - textEdit should have access to this method, shouldn't it?
    Yes, QTextEdit can access it, but you are accessing it from 'MainWindow'.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit drag and drop problem

    hmmm I see. I will try to think of how it should be. Thanks for help!

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit drag and drop problem

    Have a look at the drag and drop documentation, there are code snippets as well for demonstration:
    http://doc.trolltech.com/4.7/dnd.html
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Feb 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit drag and drop problem

    I have one more question concerning this topic..I changed approach a little bit and i tried subclassing QTextEdit this way(The body of this functions is taken directly from drag-and-drop documentation of QTextEdit):

    Qt Code:
    1. #ifndef TEXTEDITOR_H
    2. #define TEXTEDITOR_H
    3. #include <QtGui>
    4. class TextEditor: public QTextEdit
    5. {
    6. Q_OBJECT
    7.  
    8. protected:
    9. bool canInsertFromMimeData( const QMimeData *source ) const;
    10. void insertFromMimeData( const QMimeData *source );
    11. };
    12.  
    13. #endif // TEXTEDITOR_H
    To copy to clipboard, switch view to plain text mode 

    Then I put my new widget- TextEditor, using QT Creator, to my project but unfotunatelly there's error when compiling it:

    debug/mainwindow.o:mainwindow.cpp.text$_ZN13Ui_MainWindow7setupUiEP11QMainWindow[Ui_MainWindow::setupUi(QMainWindow*)]+0xcc0): undefined reference to `TextEditor::TextEditor(QWidget*)'

    collect2: ld returned 1 exit status

    Does anybody know why such error occurs and how to fix it?
    Regards!
    Last edited by high_flyer; 24th February 2011 at 08:52. Reason: code tags

  10. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextEdit drag and drop problem

    undefined reference to `TextEditor::TextEditor(QWidget*)'
    Declare and implement this constructor in TextEditor class, then run make clean, qmake and make on your project.

Similar Threads

  1. QTextEdit Drag and Drop
    By guiQt in forum Qt Programming
    Replies: 5
    Last Post: 29th August 2010, 16:36
  2. Drag Drop Images from WebPage into QTextEdit
    By Nemo in forum Qt Programming
    Replies: 7
    Last Post: 15th December 2009, 09:35
  3. QTextEdit drag and drop
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 7th February 2008, 16:41
  4. Drag and drop image into a QTextEdit ?
    By balazsbela in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2007, 14:47
  5. Drag n Drop problem
    By ScoOteR in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2007, 10:52

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.