我对 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
旨在仅提供文本和按钮。请参阅链接。
如果您只想拥有“更多详细信息”文本,请尝试使用详细文本属性。在这种情况下,您必须使用其构造函数创建消息框并显式设置图标、文本,而不是使用方便的
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()
{
}
尽管前面已经说过,向 QMessageBox 添加任何小部件都很容易。您需要做的就是创建您的小部件,例如 QComboBox。然后创建一个QMessageBox的实例。然后获取它的布局,即 QGridLayout。将 QLayout 值转换为 QGridLayout,然后您可以这样添加小部件:
QComboBox *myWidget = new QComboBox();
.. add items as necessary
QMessageBox* box = new QMessageBox(QMessageBox::Question, "Select value", "Select the value",QMessageBox::Ok);
QLayout *layout = box.layout();
((QGridLayout*)layout)->addWidget(myWidget, 1,2);
int ret = box.exec();
从QMessageBox返回后。 myWidget 设置为用户选择的值。