问题描述
我对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()
{
}