PDA

View Full Version : QTextEdit drag and drop problem



konradm
20th February 2011, 15:51
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):

//mainwindow.h
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
void insertFromMimeData( const QMimeData *source );
bool canInsertFromMimeData( const QMimeData *source ) const;

//mainwindow.cpp
bool MainWindow::canInsertFromMimeData( const QMimeData *source ) const
{
if (source->hasImage())
return true;
else
return textEdit->canInsertFromMimeData(source);
}
void MainWindow::insertFromMimeData( const QMimeData *source )
{
if (source->hasImage())
{
QImage image = qvariant_cast<QImage>(source->imageData());
QTextCursor cursor = textEdit->textCursor();
QTextDocument *document = textEdit->document();
document->addResource(QTextDocument::ImageResource, QUrl("image"), image);
cursor.insertImage("image");
}
}

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!

high_flyer
21st February 2011, 11:14
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.

konradm
21st February 2011, 13:27
Here you are. The error is:
virtual bool QTextEdit::canInsertFromMimeData(const QMimeData*) const is protected

high_flyer
21st February 2011, 13:44
Great.
Do you know what a protected method is?

konradm
21st February 2011, 14:16
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?

high_flyer
21st February 2011, 14:28
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'.

konradm
21st February 2011, 14:38
hmmm I see. I will try to think of how it should be. Thanks for help!

high_flyer
21st February 2011, 14:52
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

konradm
24th February 2011, 00:42
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):


#ifndef TEXTEDITOR_H
#define TEXTEDITOR_H
#include <QtGui>
class TextEditor: public QTextEdit
{
Q_OBJECT

protected:
bool canInsertFromMimeData( const QMimeData *source ) const;
void insertFromMimeData( const QMimeData *source );
};

#endif // TEXTEDITOR_H

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_MainWin dow7setupUiEP11QMainWindow[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!

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