2017-08-13 3 views
-2

Je programme:Comment insérer du html dans QTextEdit à partir du presse-papier via ctrl + V?

int main(int argc, char *argv[]){ 
    QApplication app(argc, argv);  
    QTextEdit te; 
    te.setHtml("<!DOCTYPE html>" 
     "<html>" 
     "<body style = \"background-color:powderblue;\">" 
     "<h1>My First Heading</h1>" 
     "<p>My first paragraph.</p>" 
     "</body>" 
     "</html>"); 
    te.resize(500, 300); 
    te.show(); 
    return app.exec(); 
} 

Ce programme crée la fenêtre suivante:

QTextEdit with html

J'ai un autre programme:

int main(int argc, char *argv[]){ 
    QApplication app(argc, argv);  
    QTextEdit te; 
    te.resize(500, 300); 
    te.show(); 
    return app.exec(); 
} 

Mais si je copie le texte

<!DOCTYPE html> 
<html> 
<body style="background-color:powderblue;"> 
<p>This is a paragraph.</p> 
<p>This is another paragraph.</p> 
</body> 
</html> 

du presse-papier en appuyant sur Ctrl + V dans la fenêtre créée par le programme, je vois:

window with plain html text

Comment puis-je réécrire mon programme pour afficher html comme dans la première image?

+0

Pas facile de dire si vous ne vous présentez pas le code. Le comportement est-il différent lorsque vous tapez le texte au lieu de le coller? – KjMag

+0

Informations sur http://doc.qt.io/qt-5/qclipboard.html – hyde

+0

Vous pouvez également lire http://doc.qt.io/qt-5/qtextedit.html#insertFromMimeData – hyde

Répondre

1

Essayez ceci:

class TextEdit : public QTextEdit{ 
public: 
    TextEdit(QWidget *parent = 0): 
     QTextEdit(parent) 
    {} 
protected: 
    void insertFromMimeData(const QMimeData *source){ 
     if(source->hasText()){ 
      setHtml(source->text()); 
     } 
     else{ 
      QTextEdit::insertFromMimeData(source); 
     } 
    } 
}; 
0

Vous ne pouvez pas coller du code html directement avec des espaces indentés. Essayez de le copier sur MS Word d'abord, puis à partir de là, collez-le partout ailleurs.