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
());
cursor.insertImage("image");
}
}
//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");
}
}
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!
Bookmarks