2017-08-13 12 views
-2

私はプログラムがあります。ctrl + VでクリップボードからHTMLをQTextEditに挿入するには?

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

しかし、私はテキストをコピーする場合

:私は別のプログラムを持っている

QTextEdit with html

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(); 
} 

このプログラムは、次のウィンドウを作成します

<!DOCTYPE html> 
<html> 
<body style="background-color:powderblue;"> 
<p>This is a paragraph.</p> 
<p>This is another paragraph.</p> 
</body> 
</html> 
プログラムによって作成されたウィンドウの中に、Ctrl + Vを押してクリップボードから

は、私は以下を参照してください。

window with plain html text

どのように私は、最初の画像のようにHTMLを表示するために私のプログラムを書き換えることができますか?

+0

容易ではありません。テキストを貼り付けるのではなく、テキストを入力するとその動作が異なりますか? – KjMag

+0

http://doc.qt.io/qt-5/qclipboard.htmlから読んでください。 – hyde

+0

http://doc.qt.io/qt-5/qtextedit.html#insertFromMimeData – hyde

答えて

1

はこれを試してください:あなたは、コードを表示しない場合は言って

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

HTMLコードをインデントされたスペースで直接貼り付けることはできません。最初にMS単語にコピーしてから、それ以外の場所に貼り付けてください。

関連する問題