{"id":814,"date":"2022-02-11T11:00:16","date_gmt":"2022-02-11T19:00:16","guid":{"rendered":"https:\/\/journal.copperspice.com\/?p=814"},"modified":"2022-04-11T08:29:16","modified_gmt":"2022-04-11T16:29:16","slug":"source-code-gui-example-13","status":"publish","type":"post","link":"https:\/\/journal.copperspice.com\/?p=814","title":{"rendered":"Source Code &nbsp;(Gui Example 13)"},"content":{"rendered":"\n<p>This example shows how to use two other static methods in QFileDialog for the user to select a directory or file name.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;QtCore&gt;\n#include &lt;QtGui&gt;\n\nclass MainWindow : public QWidget\n{\n public:\n   MainWindow();\n\n private:\n   void setExistingDirectory();\n   void setOpenFileName();\n\n   QLineEdit *dir_text;\n   QLineEdit *file_text;\n};\n<\/pre><\/div>\n\n\n<p>This declaration for MainWindow declares two slot methods and two line edit widgets which will be updated by the corresponding slot methods. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [36,39]; title: ; notranslate\" title=\"\">\nMainWindow::MainWindow()\n{\n   setMinimumSize(700, 350);\n\n   QPushButton *dir_pb = new QPushButton(&quot;Directory Name&quot;);\n   dir_text = new QLineEdit();\n\n   QPushButton *file_pb = new QPushButton(&quot;File Name&quot;);\n   file_text = new QLineEdit();\n\n   QPushButton *close_pb = new QPushButton();\n   close_pb-&gt;setText(&quot;Close&quot;);\n\n   QGridLayout *grid1 = new QGridLayout();\n   grid1-&gt;setContentsMargins(75, 45, 75, 25);\n   grid1-&gt;setHorizontalSpacing(20);\n   grid1-&gt;setVerticalSpacing(45);\n\n   grid1-&gt;addWidget(dir_pb, 0, 0);\n   grid1-&gt;addWidget(dir_text, 0, 1);\n\n   grid1-&gt;addWidget(file_pb, 1, 0);\n   grid1-&gt;addWidget(file_text, 1, 1);\n\n   QHBoxLayout *layout1 = new QHBoxLayout();\n   layout1-&gt;addStretch();\n   layout1-&gt;addWidget(close_pb);\n   layout1-&gt;addStretch();\n\n   QVBoxLayout *layoutMain = new QVBoxLayout(this);\n   layoutMain-&gt;addLayout(grid1);\n   layoutMain-&gt;addSpacing(75);\n   layoutMain-&gt;addLayout(layout1);\n\n   connect(dir_pb, &amp;QPushButton::clicked, \n         this, &amp;MainWindow::setExistingDirectory);\n\n   connect(file_pb,  &amp;QPushButton::clicked, \n         this, &amp;MainWindow::setOpenFileName);\n\n   connect(close_pb, &amp;QPushButton::clicked, \n         this, &amp;QWidget::close);\n}\n<\/pre><\/div>\n\n\n<p>The setup for this code is very similar to the last few examples. The user interface will display two push buttons with two associated line edits. <\/p>\n\n\n\n<h3>Signal \/ Slot Connections<\/h3>\n\n\n\n<p>Lines 36 and 39 set up what action should be taken when either the &#8220;Directory Name&#8221; or &#8220;File Name&#8221; push buttons are clicked.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [8,19]; title: ; notranslate\" title=\"\">\nvoid MainWindow::setExistingDirectory()\n{\n   QFileDialog::FileDialogOptions options = \n         QFileDialog::DontResolveSymlinks | \n         QFileDialog::ShowDirsOnly;\n\n   QString directory = \n         QFileDialog::getExistingDirectory(this,\n         &quot;Directory Name&quot;, dir_text-&gt;text(), options);\n\n   if (! directory.isEmpty()) {\n      dir_text-&gt;setText(directory);\n   }\n}\n\nvoid MainWindow::setOpenFileName()\n{\n   QString fileName = \n         QFileDialog::getOpenFileName(this, \n         &quot;File Name&quot;, file_text-&gt;text(), \n         &quot;All Files (*);;Text Files (*.txt)&quot;);\n\n   if (! fileName.isEmpty()) {\n      file_text-&gt;setText(fileName);\n   }\n}\n<\/pre><\/div>\n\n\n<p>This block of code shows the implementation of the two slot methods specified in the call to connect(). Line 8 calls the static method QFileDialog::getExistingDirectory() which is implemented in the CsGui library.  When the &#8220;Directory Name&#8221; button is clicked a new dialog box will be displayed with a system file dialog. When the user selects a directory and then clicks the Select Folder button, the path will be displayed in the line edit. The passed parameters set up the Window title, a prompt for the user, the initial directory, and a filter which limits the type of folders displayed. <\/p>\n\n\n\n<p>Line 19 calls the static method QFileDialog::getOpenFileName() which is also implemented in the CsGui library. When the &#8220;File Name&#8221; button is clicked a new dialog box will be displayed where the user can select the name of an existing file. In the passed parameters on line 21 you can pass a string indicating what type of files should be displayed. This string also populates the drop down box restricting the types of files shown.  In this code we are allowing either <span style=\"color:#6a5acd\" class=\"has-inline-color\"><strong>*.*<\/strong><\/span> or <span style=\"color:#6a5acd\" class=\"has-inline-color\"><strong>*.txt<\/strong><\/span>.<\/p>\n\n\n\n<p>For both of these, when the user selects the directory or file a verification is made to ensure the value is not empty before updating the line edit widget. <\/p>\n\n\n\n<h3>Main Function<\/h3>\n\n\n\n<p>Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.<\/p>\n\n\n\n<h3>Running the Example<\/h3>\n\n\n\n<p>To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change &#8220;example_1&#8221; to &#8220;example_13&#8221;. <\/p>\n\n\n\n<p><a href=\"https:\/\/download.copperspice.com\/journal\/example_13.zip\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/download.copperspice.com\/journal\/example_13.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to use two other static methods in QFileDialog for the user to select a directory or file name.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/814"}],"collection":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=814"}],"version-history":[{"count":25,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/814\/revisions"}],"predecessor-version":[{"id":1299,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=\/wp\/v2\/posts\/814\/revisions\/1299"}],"wp:attachment":[{"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/journal.copperspice.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}