Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can I use other class value?
QtWS25 Last Chance

How can I use other class value?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    masayoshi
    wrote on last edited by masayoshi
    #1

    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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kenchan
        wrote on last edited by
        #3

        @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 :-)

        M 1 Reply Last reply
        2
        • K kenchan

          @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 :-)

          M Offline
          M Offline
          masayoshi
          wrote on last edited by
          #4

          @kenchan

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

          1 Reply Last reply
          1
          • K Offline
            K Offline
            kenchan
            wrote on last edited by
            #5

            @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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved