1

对于我的程序,我正在为 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/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>

问题是我不知道什么是可能的,什么是不可能的。

例如,我想将许可协议放在带有边框和溢出的文本区域中。h1 和 h4 有效,但许可协议的样式无效。

所以许可协议只是纯文本。

有没有办法在 QMessageBox 中设置 html 的样式?

4

1 回答 1

0

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);
}
于 2014-08-07T10:58:10.010 回答