How can I use other class value?



  • I have two cpp files.
    How can I fetch searchString form dailog.cpp?
    The following code does not work.

    QMessageBox::information(this, tr("searchString from dialog.cpp"), searchString);

    // mainwindow.cpp
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_actionFind_triggered()
    {
        Dialog d;
        d.exec();
    
    }
    
    void SearchtextEdit() {
    // I would like to use searchString here.
    // I don't know how to write program to do it.
     QMessageBox::information(this, tr("searchString from dialog.cpp"), searchString);
    
    
    }
    
    
    

    // mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include "dialog.h"

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_actionFind_triggered();

    private:
    Ui::MainWindow *ui;
    void SearchtextEdit();
    };

    
    

    // dialog.cpp

    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QString>

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);
    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    void Dialog::on_pushButton_clicked()
    {
    searchString = ui->lineEdit->text();
    }

    
    
    

    // dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    #include <QString>
    #include <QDialog>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    QString searchString;

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::Dialog *ui;
    };

    #endif // DIALOG_H



  • I think you must connect your main window to the dialog's signal or give it some other means to get the dialog's searchString member data.



  • @masayoshi
    Looking at you code again... since you do this and the dialog member data is public you could do this.
    void MainWindow::on_actionFind_triggered()
    {
    Dialog d;
    d.exec();
    QString searchString = d.searchString;
    }
    since the dialog is this still in scope after calling d.exec();
    I have not actually tested this though, so give it a try :-)



  • @kenchan

    Thank you. I could fetch other class value.
    Now, I will think how to use it effectively later.



  • @masayoshi
    Glad to be of help.
    If you want to keep dialog up while processing the string, like a find and replace or something, you can make it modeless and call show() instead of exec().
    Then you would need to use the signals and slots to to get the data from the dialog or the text box directly when it changes or when the user clicks on a "search" button. You can find plenty of information about this in the documentation. here


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.