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 to make a Find Dialog quickly

How to make a Find Dialog quickly

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 5.5k Views 1 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.
  • F Offline
    F Offline
    freesix
    wrote on last edited by freesix
    #1

    Hi guys,

    I’m using Qt 4.6 and I need help to finish my Find dialog.
    I’m reading that book : C ++ GUI Programming with Qt 4 / 2006 (Jasmin Blanchette and Mark Summerfield) and I’m trying to follow what I see on page 13 and 61 (Subclassing) but it’s not working.

    Actually I’ve got two Forms : the Main window and Find Dialog.
    My find dialog looks like this : http://francisnd.fr.gd/galerie-photo.htm
    And I’ve written the below code but it still not working.
    Please can you help?

    Main Window.h :
    private slots:
    void find();

    public slots:
    void findNext(const QString & str, Qt::CaseSensitivity cs);
    void findPrevious(const QString & str, Qt::CaseSensitivity cs);

    private:
    fenFindWord *fenSearch;

    Find Dialog.h
    signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);

    private slots:
    void findClicked();
    void enableFindButton(c
    onst QString &text);

    Main Window.cpp
    connect(fenSearch, SIGNAL(findNext(const QString &,
    Qt::CaseSensitivity)), this , SLOT(findNext(const QString &, Qt::CaseSensitivity)));

    connect(fenSearch, SIGNAL(findPrevious(const QString &,
                                            Qt::CaseSensitivity)), this , SLOT(findPrevious(const QString &,Qt::CaseSensitivity)));
    

    Find Dialog.CPP

    //Constructor
    connect(ui->textSearch, SIGNAL(textChanged(const QString &)),this, SLOT(enableFindButton(const QString &)));
    connect(ui->btnFind, SIGNAL(clicked()), this, SLOT(findClicked()));

    =====

    void fenFindWord::findClicked()
    {
    QString text = ui->textSearch->text();

    Qt::CaseSensitivity cs =
    ui->caseCheckBox->isChecked() ? Qt::CaseSensitive
                                  : Qt::CaseInsensitive;
    if (ui->backwardCheckBox->isChecked()) {
        emit findPrevious(text, cs);
    }
    else
    {
        emit findNext(text, cs);
    }
    

    }

    PS : Logic will get you from A to B, but Imagination will take you everywhere...

           Albert Einstein
    
    K 1 Reply Last reply
    0
    • F freesix

      Hi guys,

      I’m using Qt 4.6 and I need help to finish my Find dialog.
      I’m reading that book : C ++ GUI Programming with Qt 4 / 2006 (Jasmin Blanchette and Mark Summerfield) and I’m trying to follow what I see on page 13 and 61 (Subclassing) but it’s not working.

      Actually I’ve got two Forms : the Main window and Find Dialog.
      My find dialog looks like this : http://francisnd.fr.gd/galerie-photo.htm
      And I’ve written the below code but it still not working.
      Please can you help?

      Main Window.h :
      private slots:
      void find();

      public slots:
      void findNext(const QString & str, Qt::CaseSensitivity cs);
      void findPrevious(const QString & str, Qt::CaseSensitivity cs);

      private:
      fenFindWord *fenSearch;

      Find Dialog.h
      signals:
      void findNext(const QString &str, Qt::CaseSensitivity cs);
      void findPrevious(const QString &str, Qt::CaseSensitivity cs);

      private slots:
      void findClicked();
      void enableFindButton(c
      onst QString &text);

      Main Window.cpp
      connect(fenSearch, SIGNAL(findNext(const QString &,
      Qt::CaseSensitivity)), this , SLOT(findNext(const QString &, Qt::CaseSensitivity)));

      connect(fenSearch, SIGNAL(findPrevious(const QString &,
                                              Qt::CaseSensitivity)), this , SLOT(findPrevious(const QString &,Qt::CaseSensitivity)));
      

      Find Dialog.CPP

      //Constructor
      connect(ui->textSearch, SIGNAL(textChanged(const QString &)),this, SLOT(enableFindButton(const QString &)));
      connect(ui->btnFind, SIGNAL(clicked()), this, SLOT(findClicked()));

      =====

      void fenFindWord::findClicked()
      {
      QString text = ui->textSearch->text();

      Qt::CaseSensitivity cs =
      ui->caseCheckBox->isChecked() ? Qt::CaseSensitive
                                    : Qt::CaseInsensitive;
      if (ui->backwardCheckBox->isChecked()) {
          emit findPrevious(text, cs);
      }
      else
      {
          emit findNext(text, cs);
      }
      

      }

      K Offline
      K Offline
      KeithS
      wrote on last edited by
      #2

      @freesix

      What's not working?

      You have posted only bits of your code, it's hard to tell what's wrong from what you posted.

      1 Reply Last reply
      1
      • F Offline
        F Offline
        freesix
        wrote on last edited by
        #3

        Thanks for replying to me.

        I don't know if i'm doing it right.

        This is the only code I've written so far.

        I took it from (Jasmin Blanchette and Mark Summerfield) ebook.
        and I don't know if that code is too old for Qt 4.6 maybe that's why it's not working !!!

        Please if you can help me to complete it.

        Actually, it doesn't search the text on the QLineEdit.
        It's not doing anything when I press on "Find" button.

        Thanks in advance

        PS : Logic will get you from A to B, but Imagination will take you everywhere...

               Albert Einstein
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @freesix said:

          Hi , you must debug it :)
          step 1 is to put
          qDebug() << "con1" << connect(xxxx
          in front of all connect and see they return true.
          step 2 would
          to see if
          findClicked is indeed called.
          to use qDebug() , you must #include it
          Then in fenFindWord, you single step and see if it does what you think.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            freesix
            wrote on last edited by
            #5

            Thanks for helping me.

            I put qDebug as suggested and i finally found and solved the problem.
            Now it's working perfectly.

            but i still have another concern about when you show that Find Dialog.

            Actually, i use the below code to show my Find Dialog.

            SearchWin->show();
            

            and it's working perfectly, but when you click on the Mainwindow, the dialog goes behind the main window, thing that I don't like !!

            I tried to use the below code, it's really open/show that Find Dialog but to find a word, it doesn't work, even if I click on Find button.

            fenFindWord *dlg = new fenFindWord(this);
            dlg.show();
            

            Please what the difference between both methods of showing a Dialog??
            I'd like to use the second method as it's still holding the dialog and the main window both together!

            Please can you help??

            PS : Logic will get you from A to B, but Imagination will take you everywhere...

                   Albert Einstein
            
            mrjjM 1 Reply Last reply
            0
            • F freesix

              Thanks for helping me.

              I put qDebug as suggested and i finally found and solved the problem.
              Now it's working perfectly.

              but i still have another concern about when you show that Find Dialog.

              Actually, i use the below code to show my Find Dialog.

              SearchWin->show();
              

              and it's working perfectly, but when you click on the Mainwindow, the dialog goes behind the main window, thing that I don't like !!

              I tried to use the below code, it's really open/show that Find Dialog but to find a word, it doesn't work, even if I click on Find button.

              fenFindWord *dlg = new fenFindWord(this);
              dlg.show();
              

              Please what the difference between both methods of showing a Dialog??
              I'd like to use the second method as it's still holding the dialog and the main window both together!

              Please can you help??

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @freesix said:

              Hi
              it should be dlg->show();

              • Please what the difference between both methods of showing a Dialog??
                You show only one.
                do you mean between using NEW or not?
                like
                fenFindWord dlg;
                dlg.show();
                and
                fenFindWord *dlg = new fenFindWord(this);
                dlg->show();
                ?
              1 Reply Last reply
              0
              • F Offline
                F Offline
                freesix
                wrote on last edited by freesix
                #7

                Sorry it's my mistake when writing that post.
                it's as below in my project

                fenFindWord *dlg = new fenFindWord(this);
                dlg->show();
                

                Please what the difference between both below methods of showing a Dialog??

                **Method 1 : **
                fenSearch = new fenFindWord();
                fenSearch->show();
                
                **Method 2 : **
                fenFindWord *dlg = new fenFindWord(this);
                dlg->show();
                

                My first question is not to know the difference between both methods,
                but to make it work like Microsoft Windows Notepad Find button does.
                I mean when you want to search for a word in Win Notepad, it still holding the Find Dialog even if you click on the mainwindow, I mean the Find Dialog is still in front.

                Unfortunately for my Find Dialog, when you click on the Mainwindow, it goes behind.

                Please, How to solve this?

                PS : Logic will get you from A to B, but Imagination will take you everywhere...

                       Albert Einstein
                
                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  hi
                  The difference between the two methods is that the (this) version give the dialog a parent which
                  is important for clean up and also for what be "over".

                  If "this" in your **Method 2 : **
                  is mainwindow im not sure why it goes behind main window.

                  you can try with
                  setWindowFlags( Qt::WindowStaysOnTopHint );
                  for the dialog.
                  maybe
                  setWindowFlags(( Qt::WindowStaysOnTop | Qt::Dialog);

                  1 Reply Last reply
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Why not just use exec() instead of show()?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      freesix
                      wrote on last edited by freesix
                      #10

                      ( Why not just use exec() instead of show()? )
                      Cause I don't want it to be modal.

                      Anyway, I solved the problem !!!
                      I was confusing between the class name and the object name in my .cpp file

                      Thanks for helping me.

                      ======

                      I have another concern, please.

                      I can save text into a txt file using the two below methods and I can open it too,
                      but I want it to be opened with the format/style I saved it,
                      I mean if my font had size 16 and font color blue when I saved it, I want that my App be able to open it the way it has been saved.

                      bool fenprinc::saveAs()
                      {
                          QString fileName = QFileDialog::getSaveFileName(this);
                          if (fileName.isEmpty())
                              return false;
                      
                          return saveFile(fileName);
                      
                      }
                      
                      bool fenprinc::saveFile(const QString &fileName)
                       {
                           QFile file(fileName);
                           if (!file.open(QFile::WriteOnly | QFile::Text)) {
                               QMessageBox::warning(this, tr("Application"),
                                                    tr("Cannot write file %1:\n%2.")
                                                    .arg(fileName)
                                                    .arg(file.errorString()));
                               return false;
                           }
                      
                           QTextStream out(&file);
                       #ifndef QT_NO_CURSOR
                           QApplication::setOverrideCursor(Qt::WaitCursor);
                       #endif
                           out << ui->textEdit->toPlainText();
                       #ifndef QT_NO_CURSOR
                           QApplication::restoreOverrideCursor();
                       #endif
                      
                           setCurrentFile(fileName);
                           return true;
                       }
                      

                      PS : Logic will get you from A to B, but Imagination will take you everywhere...

                             Albert Einstein
                      
                      1 Reply Last reply
                      0
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Hi
                        Not sure if you mean one font + color or many different fonts and colors:

                        1: font+color
                        You must save the font size and font color in the file also.
                        You could use a fixed size header line
                        and store it first in file.
                        Then when you read it, then remove the header line
                        set font + color and use rest of the text as text.

                        2: many fonts +color
                        You throw away the HTML with >toPlainText();
                        Dont save it without the html as fonts and colors are there.

                        F 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          Hi
                          Not sure if you mean one font + color or many different fonts and colors:

                          1: font+color
                          You must save the font size and font color in the file also.
                          You could use a fixed size header line
                          and store it first in file.
                          Then when you read it, then remove the header line
                          set font + color and use rest of the text as text.

                          2: many fonts +color
                          You throw away the HTML with >toPlainText();
                          Dont save it without the html as fonts and colors are there.

                          F Offline
                          F Offline
                          freesix
                          wrote on last edited by
                          #12

                          Thanks for helping me.

                          I need it to be like Windows Note Pad.
                          I mean for example :
                          I have two lines on my QTextEdit.
                          on my first line I set the font "Verdana" + the font color "Blue"
                          and my second line, I set the font "Arial Black" + the font color "Red".
                          That's what I need.
                          I think that's what you call "many fonts +color"??

                          You throw away the HTML with >toPlainText();
                          Dont save it without the html as fonts and colors are there.
                          

                          I dont think I get you !!!

                          How not to save it without the html??? (How to do that)??

                          PS : Logic will get you from A to B, but Imagination will take you everywhere...

                                 Albert Einstein
                          
                          mrjjM 1 Reply Last reply
                          0
                          • F freesix

                            Thanks for helping me.

                            I need it to be like Windows Note Pad.
                            I mean for example :
                            I have two lines on my QTextEdit.
                            on my first line I set the font "Verdana" + the font color "Blue"
                            and my second line, I set the font "Arial Black" + the font color "Red".
                            That's what I need.
                            I think that's what you call "many fonts +color"??

                            You throw away the HTML with >toPlainText();
                            Dont save it without the html as fonts and colors are there.
                            

                            I dont think I get you !!!

                            How not to save it without the html??? (How to do that)??

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @freesix
                            Hi, u are welcome.
                            yes that would be case 2 where u have formatting in whole file.
                            call toHtml() instead.
                            U will save all html which includes any font or color change.
                            Should be exactly the same you load it again and call
                            setHtml()

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              freesix
                              wrote on last edited by
                              #14

                              Thanks a lot.
                              I can now do that.

                              but I still have another concern that is :

                              When I change the font or color font of my selected text,
                              it applies that font and color font to the all text in the QTextEdit.
                              I wanted to only be applied on the selected text.

                              I don't know : I use "QSettings" to save and restore my application settings.
                              Can I take it out???

                              PS : Logic will get you from A to B, but Imagination will take you everywhere...

                                     Albert Einstein
                              
                              mrjjM 1 Reply Last reply
                              0
                              • F freesix

                                Thanks a lot.
                                I can now do that.

                                but I still have another concern that is :

                                When I change the font or color font of my selected text,
                                it applies that font and color font to the all text in the QTextEdit.
                                I wanted to only be applied on the selected text.

                                I don't know : I use "QSettings" to save and restore my application settings.
                                Can I take it out???

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @freesix

                                • it applies that font and color font to the all text in the QTextEdit.
                                  But does your code try to set it only on the selected text?

                                • "QSettings" to save and restore my application settings. Can I take it out???
                                  yes ? that is normally to save window position and other setting using this class.
                                  If you dont need it, you can remove it.

                                F 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @freesix

                                  • it applies that font and color font to the all text in the QTextEdit.
                                    But does your code try to set it only on the selected text?

                                  • "QSettings" to save and restore my application settings. Can I take it out???
                                    yes ? that is normally to save window position and other setting using this class.
                                    If you dont need it, you can remove it.

                                  F Offline
                                  F Offline
                                  freesix
                                  wrote on last edited by freesix
                                  #16

                                  @mrjj

                                  "But does your code try to set it only on the selected text?"
                                  I don't know !!!
                                  I thought as we use QTextEdit combined with .toHTML, it'll do that automatically !!!

                                  Please Sir, How to do that???

                                  About "QSetting", when I was using 'toPlainText',
                                  I mean before I hear about that 'toHTML', I was using QSetting to save and restore the font and color font of the QTextEdit Text. It was also working great !!!
                                  Now we use "toHTML" like you said I can remove it.

                                  and I also noticed something in the txt file I saved using "toHTML".
                                  when I try to read/open it using Win NotePad, I see HTML formatting. Why????

                                  PS : Logic will get you from A to B, but Imagination will take you everywhere...

                                         Albert Einstein
                                  
                                  mrjjM 1 Reply Last reply
                                  0
                                  • F freesix

                                    @mrjj

                                    "But does your code try to set it only on the selected text?"
                                    I don't know !!!
                                    I thought as we use QTextEdit combined with .toHTML, it'll do that automatically !!!

                                    Please Sir, How to do that???

                                    About "QSetting", when I was using 'toPlainText',
                                    I mean before I hear about that 'toHTML', I was using QSetting to save and restore the font and color font of the QTextEdit Text. It was also working great !!!
                                    Now we use "toHTML" like you said I can remove it.

                                    and I also noticed something in the txt file I saved using "toHTML".
                                    when I try to read/open it using Win NotePad, I see HTML formatting. Why????

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #17

                                    @freesix said:

                                    • "But does your code try to set it only on the selected text?" - I don't know !!!
                                    • I thought as we use QTextEdit combined with .toHTML, it'll do that automatically !!!

                                    hmm. not sure i understand here.
                                    How do you color the selected text?

                                    • when I try to read/open it using Win NotePad, I see HTML formatting. Why????
                                      because the fonts and the text is only possible as HTML.
                                      You can not have different color or font size pr line in a true text file.
                                    F 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @freesix said:

                                      • "But does your code try to set it only on the selected text?" - I don't know !!!
                                      • I thought as we use QTextEdit combined with .toHTML, it'll do that automatically !!!

                                      hmm. not sure i understand here.
                                      How do you color the selected text?

                                      • when I try to read/open it using Win NotePad, I see HTML formatting. Why????
                                        because the fonts and the text is only possible as HTML.
                                        You can not have different color or font size pr line in a true text file.
                                      F Offline
                                      F Offline
                                      freesix
                                      wrote on last edited by
                                      #18

                                      @mrjj
                                      "How do you color the selected text?"
                                      Once I run my App, I select it using my mouse and I click on "Color" or "Font" button that possess the below method.

                                      void MainWindow::color()
                                       {
                                          bool ok = true;
                                      
                                          QColor couleur = QColorDialog::getColor(Qt::white, this);
                                          if (couleur.isValid())
                                          {
                                              QPalette palette;
                                              palette.setColor(QPalette::Text, couleur);
                                      
                                              if (ok)
                                              {
                                                  textEdit->setPalette(palette);
                                              }
                                          }
                                      
                                       }
                                      

                                      Below is for setting a font on a text :

                                      void MainWindow::font()
                                       {
                                          bool ok = false;
                                          QFont police = QFontDialog::getFont(&ok, textEdit->font(), this, "Choisissez une police");
                                          if (ok)
                                          {
                                          textEdit->setFont(police);
                                          }
                                       }
                                      

                                      but unfortunately it applies it on the all QTextEdit txt.

                                      PS : Logic will get you from A to B, but Imagination will take you everywhere...

                                             Albert Einstein
                                      
                                      1 Reply Last reply
                                      0
                                      • mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @freesix said:
                                        yes is for whole text. it seems.

                                        U need to read into using
                                        QTextCharFormat
                                        QTextCursor

                                        it has been ask many times
                                        http://stackoverflow.com/questions/5427572/how-to-change-current-line-format-in-qtextedit-without-selection

                                        prepare to spend some time to understand it.

                                        1 Reply Last reply
                                        1
                                        • F Offline
                                          F Offline
                                          freesix
                                          wrote on last edited by freesix
                                          #20

                                          Thanks. will do.

                                          why when I try to read/open the txt file I previously saved using "toHTML" I see HTML formatting inside that txt.

                                          Is there anyway to bypass it, and make it normally like Win Notepad??

                                          ==

                                          Can you please explain properly/clearly the below code?
                                          I know it's a condition similar to "If...else" but I really don't know exactly how/when/where to use it.

                                          I like that way of doing Qt Conditions. it's really impressive!!!

                                          I just copied it from a book and started using it on my App.

                                          Qt::CaseSensitivity cs =
                                          ui->caseCheckBox->isChecked() ? Qt::CaseSensitive
                                                                        : Qt::CaseInsensitive;
                                          

                                          PS : Logic will get you from A to B, but Imagination will take you everywhere...

                                                 Albert Einstein
                                          
                                          mrjjM 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