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. I would like to use searchString in Dialog.

I would like to use searchString in Dialog.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 640 Views 2 Watching
  • 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
    #1

    I always get empty string in the following codes.
    Could you modift my codes to get searchStirng ?

    //mainwindow.cpp
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDialog>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QDialog *Dialog = new QDialog;
        QVBoxLayout  *Layout = new QVBoxLayout();
        QLineEdit *line = new QLineEdit;
        QPushButton *button = new QPushButton(tr("Find"));
        Layout->addWidget(line);
        Layout->addWidget(button);
        Dialog->setLayout(Layout);
    
        Dialog->exec();
        searchString = line->text();
    
        connect(button, SIGNAL(clicked()), this, SLOT(find()));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::find()
    {
       QMessageBox msgBox;
       msgBox.setText(tr("Find String : ") + searchString);
       msgBox.exec();
    }
    
    //mainwindow.h
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void find();
    
    private:
        Ui::MainWindow *ui;
        QString searchString;
    };
    
    #endif // MAINWINDOW_H
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You take the text in the constructor
      searchString = line->text();
      and at that point, nothing is likely typed into it.

      I think you want something more like

      void MainWindow::find()
      {
         QMessageBox msgBox;
         msgBox.setText(tr("Find String : ") +  line->text());
         msgBox.exec();
      }
      

      so it takes the newest text.
      could also be

         searchString = line->text();
         QMessageBox msgBox;
         msgBox.setText(tr("Find String : ") + searchString);
         msgBox.exec();
      
      M 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        You take the text in the constructor
        searchString = line->text();
        and at that point, nothing is likely typed into it.

        I think you want something more like

        void MainWindow::find()
        {
           QMessageBox msgBox;
           msgBox.setText(tr("Find String : ") +  line->text());
           msgBox.exec();
        }
        

        so it takes the newest text.
        could also be

           searchString = line->text();
           QMessageBox msgBox;
           msgBox.setText(tr("Find String : ") + searchString);
           msgBox.exec();
        
        M Offline
        M Offline
        masayoshi
        wrote on last edited by
        #3

        @mrjj

        Thank you for your reply.
        I get error message . "I get use of undeclared identifier 'line'. "

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Because you declared line in your constructor. Make it a member variable of your class. However, since you are using Designer, why not use it to add that QLineEdit to your UI ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            Because you declared line in your constructor. Make it a member variable of your class. However, since you are using Designer, why not use it to add that QLineEdit to your UI ?

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

            @SGaist

            Thank you for your help.

            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