Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to make a Find Dialog quickly



  • 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);
    }
    

    }



  • @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.



  • 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


  • Lifetime Qt Champion

    @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.



  • 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??


  • Lifetime Qt Champion

    @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();
      ?


  • 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?


  • Lifetime Qt Champion

    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);


  • Lifetime Qt Champion

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



  • ( 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;
     }
    

  • Lifetime Qt Champion

    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.



  • 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)??


  • Lifetime Qt Champion

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



  • 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???


  • Lifetime Qt Champion

    @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.



  • @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????


  • Lifetime Qt Champion

    @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.


  • @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.


  • Lifetime Qt Champion

    @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.



  • 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;
    

  • Lifetime Qt Champion

    @freesix

    • Is there anyway to bypass it, and make it normally like Win Notepad??
      Not really - since you need the HTML format to do colors and fonts inside the text.
      If plaintext, its not possible to change font or colors inside the text, ONLY for all of the text.

    • Can you please explain properly/clearly the below code?
      Its actually pure c++- can be used with non Qt too
      Its called a Ternary operator
      http://www.cprogramming.com/reference/operators/ternary-operator.html

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

    it says
    let cs be set to "Qt::CaseSensitive" OR "Qt::CaseInsensitive" depending on the value of "ui->caseCheckBox->isChecked()"

    its just another form of if as you say and you can use when ever you you would make a "if then else"

    The format is
    value = (condition) ? if condition true : if condition false


Log in to reply