微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在QMessageBox中显示QListView

如何解决在QMessageBox中显示QListView

我对QT完全陌生,感到很困惑。

我创建了一个QListView(称为“ listview”),并希望在我的QMessageBox显示它:

const int resultInfo = QMessageBox::information(this,tr("Generate Software"),tr("The following files will be changed by the program:"),=> Here the QListView should show up!
    QMessageBox::Yes | QMessageBox::No);
if (resultInfo != QMessageBox::Yes) {
    return;
}

有可能吗?

解决方法

QMessageBox设计为仅提供文本和按钮。参见link

如果您只想显示“更多详细信息”文本,请尝试使用detail text property。在这种情况下,您将必须使用其构造函数创建消息框,并显式设置图标和文本,而不是使用便捷的information()函数。

如果仍要在消息框中显示列表视图,则应考虑使用QDialog,它是QMessageBox的基类。下面的小例子:

#include "mainwindow.h"

#include <QDialog>
#include <QListView>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QDialog *dialog = new QDialog{this};
    dialog->setWindowTitle(tr("Fancy title"));

    auto button = new QPushButton{tr("OK"),this};
    connect(button,&QPushButton::clicked,dialog,&QDialog::accept);

    QVBoxLayout *layout = new QVBoxLayout{dialog};
    layout->addWidget(new QLabel{tr("Description"),this});
    layout->addWidget(new QListView{this});
    layout->addWidget(button);

    dialog->show();
}

MainWindow::~MainWindow()
{
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。