请教QTextEdit的问题
我想做一个小的聊天应用。界面大致如下:
History List是只读的,请问如何将Input中的内容加入到History List中去,
Input中的内容包括文字和图片。谢谢。
[最优解释]
看看Qt的例子 : Text Object Example
[其他解释]
QTextEdit 能插入图片吗???
没试过,但我觉得放图片的话,你应该改用 QPlainTextEdit 吧
[其他解释]
void MainWindow::OnInsertPicture()
{
QString file = QFileDialog::getOpenFileName(this, tr("Select an image"),
".", tr("Images (*.png *.gif *.jpg *jpeg)"));
QUrl Uri ( QString ( "file://%1" ).arg ( file ) );
QImage image = QImageReader ( file ).read();
QTextDocument * textDocument = m_pInputEdit->document();
textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant ( image ) );
QTextCursor cursor = m_pInputEdit->textCursor();
QTextImageFormat imageFormat;
imageFormat.setWidth( image.width() );
imageFormat.setHeight( image.height() );
QString string = Uri.toString() ;
imageFormat.setName(string);
cursor.insertImage(imageFormat);
}
void MainWindow::onInsertPicture()
{
QString file = QFileDialog::getOpenFileName(this, tr("Select an image"),
".", tr("Images (*.png *.gif *.jpg *jpeg)"));
if (file.isEmpty())
{
return;
}
QTextImageFormat imageFormat;
imageFormat.setName(file);
QTextCursor cursor = m_pInputEdit->textCursor();
cursor.insertImage(imageFormat);
}
void MainWindow::onSend()
{
QString strInputContent = m_pInputEdit->toHtml();
m_pMsgListor->append(strInputContent);
m_pInputEdit->clear();
}