(E) Find dialogΒΆ

Goal: I will learn to create a simple dialog using Qt Designer, and to understand the signals & slots mechanism of Qt.

Instructions: Create a new project: student/12/find_dialog/.

Create the new project according to to the instructions in the previous material sections.

Your task is to use Qt Designer to implement a dialog that looks like the figure below.

../../_images/find_dialog.png

You can write the name of the file, from which you will search for certain words, into the lineEdit component, located next to the label Find from file. Similarly, you can write the word you want to search for into the lineEdit component, located next to the label Find what. When you click the Find button, the given word will be searched for from the given file. You can use the textBrowser component (located below the Search status label) to print the information on whether the file was found or not. If the file was found, you can print the information on whether the target word was found or not.

You are only allowed to print exactly one of the following texts:

  • File not found
  • File found
  • Word not found
  • Word found

The program prints File not found, if the given file cannot be found. If the file can be found but the user gives no word to search for (i.e. the word is empty or its length is zero), the program prints File found. If the file can be found and if the given word is non-empty, the program prints one of the two remaining choices according to the fact, whether the word occurs in the file or not.

You can use the Match case checkbox to define whether or not capitalization matters. Therefore, this choice has also effect on which of the two last choices the program prints. You need not store the made choice into a boolean variable, since the class QCheckBox has the function isChecked, which returns the made choice.

You can close the MainWindow with the Close button.

Attention

Use the following object names for lineEdit widgets: fileLineEdit and keyLineEdit. For these widgets, use the signal editingFinished. For buttons, use the object names: findPushButton and closePushButton. For the remaining widgets, use the object names: textBrowser and matchCheckBox.

If desired you can complete the assignment in the following phases:

  • First, construct a dialog resembling the figure above. When you have created the project according to the instructions in the previous sections, Qt generates some of the program code of the classes. You can see the point Forms in the file list on the left, and below that, the file mainwindow.ui. By clicking the name of this file, you turn to Qt Designer where you can build the dialog by dragging the necessary user interface components to suitable places. By selecting a component (either in the drawing area or in the Object list on the right) and by right-pressing the mouse, you can see a menu, from which you should select Change objectName to give the names mentioned in the Note (Huom) box.

  • If you want to test the created functionalities as soon as possible, you can now implement the closing of the dialog. You can do this in Qt Designer by opening the Signals & Slots Editor. Click the plus sign to use the menu to add signals and slots. The selections you need:

    • Sender: Close button
    • Signal: clicked()
    • Receiver: MainWindow
    • Slot: close()

    Here, we already defined one signal-slot connection. It is not this simple with the other connections.

  • Now, you can try how to read the name of the input file or the target word. Here, you again need the Signals & slots mechanism. When you click the lineEdit component, it opens a menu where you can select Go to slot .... Now, Qt generates the declaration of the slot function in the file MainWindow.hh and the corresponding empty function in the file MainWindow.cpp. Your task is to write the body of this function.

    You can check if the reading was successful by printing the text that was read into the qDebug() stream that can found from the QDebug library.

    Please note that you can write any C++ code in the generated classes. You can, for example, add attributes and methods.

  • After you have made sure that you can store the input word, you can implement the search algorithm. You need usual C++ code to do that. The code should read text from a file and compare it to an another text.

  • Finally, implement the Match case functionality.

Tips for completing the assignment:

  • It should be possible for you to complete this assignment using the example we showed you in the lecture. However, if you did not understand the phases above, it is recommended that you take part in the weekly exercises or go to Kooditorio.
  • You can implement opening a file, as well as reading and closing it in a usual way (graphical user interfaces require no changes in thes actions).
  • You can implement string search in any way you wish, it is enough to use e.g. the method find from string class.

A+ presents the exercise submission form here.