1

I'm completely new with QT and find it quite confusing.

I created a QListView (called "listview") and would like to show that in my 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;
}

Is that possible somehow?

1 Answers1

0

QMessageBox is designed to serve texts and buttons only. See link.

If you just want to have a "More details" text, try using detail text property. In that case, you will have to create the message box using its constructor and set the icons, texts explicitly, not using the convenient information() function.

If you still want to have a list view in the message box, you should consider using QDialog, which is the base class of QMessageBox. Small example below:

#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()
{
}
Minh
  • 1,630
  • 1
  • 8
  • 18
  • Hi, thank you, will try that. Do you have any example code of a QDialog with a QListView in it? – purple_flamingo Oct 13 '20 at 10:51
  • I added an example. – Minh Oct 13 '20 at 11:22
  • Thank you so much, super helpful! One last question, how do I get my actual QListView (called "listview"), that contains the data, into it now? – purple_flamingo Oct 13 '20 at 15:03
  • You have to have a model to store the data (suggesting QStandardItemModel), then call `listview.setModel()`. Please consider marking the answer correct and upvote if your question is answered. – Minh Oct 13 '20 at 15:30