具有HTML布局格式的QMessageBox
对于我的程序,我正在为about部分创建一个QMessageBox,并导入一个html文件来设置所述框的布局:具有HTML布局格式的QMessageBox,html,css,qt,qmessagebox,Html,Css,Qt,Qmessagebox,对于我的程序,我正在为about部分创建一个QMessageBox,并导入一个html文件来设置所述框的布局: // Program.cpp QMessageBox about; about.setWindowTitle(tr("About")); // Enable the HTML format. about.setTextFormat(Qt::RichText); // Getting the HTML from the file std::ifstream file("html/ab
// Program.cpp
QMessageBox about;
about.setWindowTitle(tr("About"));
// Enable the HTML format.
about.setTextFormat(Qt::RichText);
// Getting the HTML from the file
std::ifstream file("html/about.html");
std::string html, line;
if (file.is_open())
{
while (std::getline(file, line))
html += line;
}
about.setText(html.c_str());
about.exec();
about.html如下所示:
<!-- about.html -->
<div>
<h1>The Program</h1>
<p> Presentation </p>
<p> Version : 0.1.2 </p>
<p> <a href="www.wipsea.com">User Manual</a> </p>
<h4>Licence Agreement</h4>
<p style="border: 1px solid black; overflow: y;">
Law thingy, bla and also bla, etc ...
</p>
</div>
问题是我不知道什么是可能的,什么是不可能的
例如,我想把许可协议放在一个有边界和溢出的textarea中。
h1和h4可以工作,但许可协议的样式不能
因此,许可协议只是纯文本
有没有办法在QMessageBox中设置html的样式?QMessageBox只是一个方便的类,不提供此功能。您必须构建自己的对话框:
class HTMLMessageBox : public QDialog
{
Q_OBJECT
public:
explicit HTMLMessageBox(QWidget *parent = 0);
void setHtml(QString html);
private:
QTextEdit *m_textEdit;
};
HTMLMessageBox::HTMLMessageBox(QWidget *parent) :
QDialog(parent)
{
m_textEdit = new QTextEdit(this);
m_textEdit->setAcceptRichText(true);
QPushButton *okButton = new QPushButton(tr("Ok"));
searchButton->setDefault(true);
QPushButton *cancelButton = new QPushButton(tr("Cancel"));
QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal);
buttonBox->addButton(searchButton, QDialogButtonBox::AcceptRole);
buttonBox->addButton(cancelButton, QDialogButtonBox::RejectRole);
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
QVBoxLayout *lt = new QVBoxLayout;
lt->addWidget(m_textEdit);
lt->addWidget(buttonBox);
setLayout(lt);
}
void HTMLMessageBox::setHtml(QString html)
{
m_textEdit->setHtml(html);
}
我相信短信盒更适合短信。如果您需要显示扩展文本,例如许可协议,我认为使用QTextEdit是更好的方法。它仍然没有达到我的预期。textEdit HTML位于溢出的容器中,但它包含整个HTML。此外,许可证语料库没有边界。文本编辑并不能更好地解析HTML。