Important: Please read the Qt Code of Conduct -

Getting variable from QDialog class in main window.

  • Hi, I have made a QDialong class import importdata, and when the user accepts the the results I would like to pass them variables to the mainwindow class with a QObject conenction with the following code:

    std::vector<std::string> names;
        std::vector<Vector3> start_pos;
        std::vector<double> radii;
        auto *importDataDialog = new importdata(this);
        QObject::connect(this->ui->importDataButton, &QPushButton::clicked,
                         importDataDialog, &QDialog::open);
        QObject::connect(importDataDialog, &QDialog::accepted,
                            start_pos = importDataDialog->get_pos();
                            radii = importDataDialog->get_radii();
                            names = importDataDialog->get_names();

    However when I try to compile I get this error

    In lambda function:
    /xxx/mainwindow.cpp:45:63: error: passing ‘const std::vector<Vector3>’ as ‘this’ argument discards qualifiers [-fpermissive]
                             start_pos = importDataDialog->get_pos();

    The get_pos() and other functions are defined as:

    namespace Ui {
    class importdata;
    class importdata : public QDialog
        explicit importdata(QWidget *parent = 0);
        std::vector<Vector3> get_pos() const { return  body_positions;};
        std::vector<double> get_radii() const { return body_radii;};
        std::vector<std::string> get_names() const { return body_names;};
        Ui::importdata *ui;
        std::vector<Vector3> body_positions;
        std::vector<double > body_radii;
        std::vector<std::string> body_names;

    I am using Qt5.9.1 and C++11
    Thnaks in advance!

  • This seems to work:

    QObject::connect(importDataDialog, &QDialog::accepted,
        [=]() {
            this->ui->comboBoxCamCenter->setEnabled(true); }

Log in to reply