首页 > 解决方案 > 在 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;
}

这有可能吗?

标签: qtqlistviewqmessagebox

解决方案


QMessageBox旨在仅提供文本和按钮。见链接

如果您只想拥有“更多详细信息”文本,请尝试使用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()
{
}

推荐阅读