Difference between revisions of "Qt5/Qt-Creator 3-Widget Hello World Application"

From dftwiki3
Jump to: navigation, search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 11:12, 1 February 2014 (EST)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 11:12, 1 February 2014 (EST)
 
----
 
----
 +
<br />
 +
<bluebox>
 +
This tutorial illustrates how to create a GUI application with Qt-5.  This is slightly different from the approach taken with Qt-4.  The resulting app sports 3 widgets: two push-buttons and 1 text-edit widget.  One button, when clicked, displays the string "Hello Word!" in the text-edit box.  The other button, when clicked, clears the text-edit.  Qt's '''signal''' and '''slot''' mechanism is illustrated.  Check [http://cs.smith.edu/dftwiki/index.php/Tutorials this page] for more Qt-related tutorials.
 +
</bluebox>
 +
<br />
 +
 
=Video=
 
=Video=
 +
 
<br />
 
<br />
<center><videoflash>1ILvH24PYHg </videoflash></center>
+
<center><videoflash>1ILvH24PYHg </videoflash><br />http://www.youtube.com/watch?v=1ILvH24PYHg</center>
 
<br />
 
<br />
 +
 
=Project Files=
 
=Project Files=
[11:10:34] ~/Desktop/Dropbox/Qt/Project3Widgets$: for i in `ls -1`; do echo "= $i ="; echo "<br /><br /><source lang="cpp">"; cat $i; echo "</source><br /><br />"; done
+
<br />
 
== Project3Widgets.pro ==
 
== Project3Widgets.pro ==
 
<br /><br /><source lang="text">
 
<br /><br /><source lang="text">
Line 30: Line 38:
 
FORMS    += mainwindow.ui
 
FORMS    += mainwindow.ui
 
</source><br /><br />
 
</source><br /><br />
= Project3Widgets.pro.user =
+
   
<br /><br /><source lang=cpp>
+
<br /><br />
<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE QtCreatorProject>
 
<!-- Written by QtCreator 3.0.0, 2014-02-01T10:22:28. -->
 
<qtcreator>
 
<data>
 
  <variable>ProjectExplorer.Project.ActiveTarget</variable>
 
  <value type="int">0</value>
 
</data>
 
<data>
 
  <variable>ProjectExplorer.Project.EditorSettings</variable>
 
  <valuemap type="QVariantMap">
 
  <value type="bool" key="EditorConfiguration.AutoIndent">true</value>
 
  <value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
 
  <value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
 
  <valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
 
    <value type="QString" key="language">Cpp</value>
 
    <valuemap type="QVariantMap" key="value">
 
    <value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
 
    </valuemap>
 
  </valuemap>
 
  <valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
 
    <value type="QString" key="language">QmlJS</value>
 
    <valuemap type="QVariantMap" key="value">
 
    <value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
 
    </valuemap>
 
  </valuemap>
 
  <value type="int" key="EditorConfiguration.CodeStyle.Count">2</value>
 
  <value type="QByteArray" key="EditorConfiguration.Codec">UTF-8</value>
 
  <value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
 
  <value type="int" key="EditorConfiguration.IndentSize">4</value>
 
  <value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
 
  <value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
 
  <value type="int" key="EditorConfiguration.PaddingMode">1</value>
 
  <value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
 
  <value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
 
  <value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
 
  <value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
 
  <value type="int" key="EditorConfiguration.TabSize">8</value>
 
  <value type="bool" key="EditorConfiguration.UseGlobal">true</value>
 
  <value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
 
  <value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
 
  <value type="bool" key="EditorConfiguration.cleanIndentation">true</value>
 
  <value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
 
  <value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
 
  </valuemap>
 
</data>
 
<data>
 
  <variable>ProjectExplorer.Project.PluginSettings</variable>
 
  <valuemap type="QVariantMap"/>
 
</data>
 
<data>
 
  <variable>ProjectExplorer.Project.Target.0</variable>
 
  <valuemap type="QVariantMap">
 
  <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.2.0 clang 64bit</value>
 
  <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.2.0 clang 64bit</value>
 
  <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.520.clang_64.essentials_kit</value>
 
  <value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
 
  <value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
 
  <value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
 
  <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
 
    <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/Users/thiebaut/Desktop/Dropbox/Qt/build-Project3Widgets-Desktop_Qt_5_2_0_clang_64bit-Debug</value>
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
 
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">qmake</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
 
      <value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">false</value>
 
      <value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibraryAuto">true</value>
 
      <value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
 
      <value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
 
    </valuemap>
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
 
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
 
      <valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.AutomaticallyAddedMakeArguments">
 
      <value type="QString">-w</value>
 
      <value type="QString">-r</value>
 
      </valuelist>
 
      <value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
 
    </valuemap>
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
 
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
 
      <valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.AutomaticallyAddedMakeArguments">
 
      <value type="QString">-w</value>
 
      <value type="QString">-r</value>
 
      </valuelist>
 
      <value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
 
    <value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
 
    <valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Debug</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
 
    <value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
 
    <value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
 
  </valuemap>
 
  <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
 
    <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/Users/thiebaut/Desktop/Dropbox/Qt/build-Project3Widgets-Desktop_Qt_5_2_0_clang_64bit-Release</value>
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
 
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">qmake</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
 
      <value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary">false</value>
 
      <value type="bool" key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibraryAuto">true</value>
 
      <value type="QString" key="QtProjectManager.QMakeBuildStep.QMakeArguments"></value>
 
      <value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
 
    </valuemap>
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
 
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
 
      <valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.AutomaticallyAddedMakeArguments">
 
      <value type="QString">-w</value>
 
      <value type="QString">-r</value>
 
      </valuelist>
 
      <value type="bool" key="Qt4ProjectManager.MakeStep.Clean">false</value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments"></value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
 
    </valuemap>
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
 
      <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Make</value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
      <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
 
      <valuelist type="QVariantList" key="Qt4ProjectManager.MakeStep.AutomaticallyAddedMakeArguments">
 
      <value type="QString">-w</value>
 
      <value type="QString">-r</value>
 
      </valuelist>
 
      <value type="bool" key="Qt4ProjectManager.MakeStep.Clean">true</value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
 
      <value type="QString" key="Qt4ProjectManager.MakeStep.MakeCommand"></value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
 
    <value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
 
    <valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Release</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
 
    <value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
 
    <value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
 
  </valuemap>
 
  <value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">2</value>
 
  <valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
 
    <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
 
    <value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
 
    </valuemap>
 
    <value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy locally</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
 
  </valuemap>
 
  <value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
 
  <valuemap type="QVariantMap" key="ProjectExplorer.Target.PluginSettings"/>
 
  <valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
 
    <valuelist type="QVariantList" key="Analyzer.Valgrind.AddedSuppressionFiles"/>
 
    <value type="bool" key="Analyzer.Valgrind.Callgrind.CollectBusEvents">false</value>
 
    <value type="bool" key="Analyzer.Valgrind.Callgrind.CollectSystime">false</value>
 
    <value type="bool" key="Analyzer.Valgrind.Callgrind.EnableBranchSim">false</value>
 
    <value type="bool" key="Analyzer.Valgrind.Callgrind.EnableCacheSim">false</value>
 
    <value type="bool" key="Analyzer.Valgrind.Callgrind.EnableEventToolTips">true</value>
 
    <value type="double" key="Analyzer.Valgrind.Callgrind.MinimumCostRatio">0.01</value>
 
    <value type="double" key="Analyzer.Valgrind.Callgrind.VisualisationMinimumCostRatio">10</value>
 
    <value type="bool" key="Analyzer.Valgrind.FilterExternalIssues">true</value>
 
    <value type="int" key="Analyzer.Valgrind.LeakCheckOnFinish">1</value>
 
    <value type="int" key="Analyzer.Valgrind.NumCallers">25</value>
 
    <valuelist type="QVariantList" key="Analyzer.Valgrind.RemovedSuppressionFiles"/>
 
    <value type="int" key="Analyzer.Valgrind.SelfModifyingCodeDetection">1</value>
 
    <value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
 
    <value type="bool" key="Analyzer.Valgrind.ShowReachable">false</value>
 
    <value type="bool" key="Analyzer.Valgrind.TrackOrigins">true</value>
 
    <value type="QString" key="Analyzer.Valgrind.ValgrindExecutable">valgrind</value>
 
    <valuelist type="QVariantList" key="Analyzer.Valgrind.VisibleErrorKinds">
 
    <value type="int">0</value>
 
    <value type="int">1</value>
 
    <value type="int">2</value>
 
    <value type="int">3</value>
 
    <value type="int">4</value>
 
    <value type="int">5</value>
 
    <value type="int">6</value>
 
    <value type="int">7</value>
 
    <value type="int">8</value>
 
    <value type="int">9</value>
 
    <value type="int">10</value>
 
    <value type="int">11</value>
 
    <value type="int">12</value>
 
    <value type="int">13</value>
 
    <value type="int">14</value>
 
    </valuelist>
 
    <value type="int" key="PE.EnvironmentAspect.Base">2</value>
 
    <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Project3Widgets</value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
 
    <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/Users/thiebaut/Desktop/Dropbox/Qt/Project3Widgets/Project3Widgets.pro</value>
 
    <value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
 
    <value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">Project3Widgets.pro</value>
 
    <value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
 
    <value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal">false</value>
 
    <value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
 
    <value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
 
    <value type="bool" key="RunConfiguration.UseCppDebugger">true</value>
 
    <value type="bool" key="RunConfiguration.UseCppDebuggerAuto">false</value>
 
    <value type="bool" key="RunConfiguration.UseMultiProcess">false</value>
 
    <value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
 
    <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
 
  </valuemap>
 
  <value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
 
  </valuemap>
 
</data>
 
  <data>
 
  <variable>ProjectExplorer.Project.TargetCount</variable>
 
  <value type="int">1</value>
 
</data>
 
<data>
 
  <variable>ProjectExplorer.Project.Updater.EnvironmentId</variable>
 
  <value type="QByteArray">{7480a9c0-7378-4136-91e6-bca38dac549f}</value>
 
</data>
 
<data>
 
  <variable>ProjectExplorer.Project.Updater.FileVersion</variable>
 
  <value type="int">15</value>
 
</data>
 
</qtcreator>
 
</source><br /><br />
 
 
== main.cpp ==
 
== main.cpp ==
 
<br /><br /><source lang=cpp>
 
<br /><br /><source lang=cpp>
Line 307: Line 54:
 
}
 
}
 
</source><br /><br />
 
</source><br /><br />
 +
 
= mainwindow.cpp =
 
= mainwindow.cpp =
 
<br /><br /><source lang=cpp>
 
<br /><br /><source lang=cpp>
Line 479: Line 227:
 
   
 
   
 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
[[Category:Qt]][[Category:C++]]
+
[[Category:Tutorials]][[Category:Qt]][[Category:C++]]

Latest revision as of 17:55, 1 February 2014

--D. Thiebaut (talk) 11:12, 1 February 2014 (EST)



This tutorial illustrates how to create a GUI application with Qt-5. This is slightly different from the approach taken with Qt-4. The resulting app sports 3 widgets: two push-buttons and 1 text-edit widget. One button, when clicked, displays the string "Hello Word!" in the text-edit box. The other button, when clicked, clears the text-edit. Qt's signal and slot mechanism is illustrated. Check this page for more Qt-related tutorials.


Video



http://www.youtube.com/watch?v=1ILvH24PYHg


Project Files


Project3Widgets.pro



#-------------------------------------------------
#
# Project created by QtCreator 2014-02-01T10:21:42
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Project3Widgets
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui




main.cpp



#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}


mainwindow.cpp



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    ui->textEdit->append( "Hello World!");
}


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 = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


mainwindow.ui



<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>664</width>
    <height>501</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Hello World!</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <layout class="QVBoxLayout" name="verticalLayout">
      <item>
       <layout class="QHBoxLayout" name="horizontalLayout">
        <item>
         <spacer name="horizontalSpacer">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>40</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton">
          <property name="text">
           <string>Hello</string>
          </property>
         </widget>
        </item>
        <item>
         <widget class="QPushButton" name="pushButton_2">
          <property name="text">
           <string>Clear</string>
          </property>
         </widget>
        </item>
        <item>
         <spacer name="horizontalSpacer_2">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
          </property>
          <property name="sizeHint" stdset="0">
           <size>
            <width>40</width>
            <height>20</height>
           </size>
          </property>
         </spacer>
        </item>
       </layout>
      </item>
      <item>
       <widget class="QTextEdit" name="textEdit"/>
      </item>
     </layout>
    </item>
   </layout>
   <zorder>pushButton</zorder>
   <zorder>pushButton_2</zorder>
   <zorder>horizontalSpacer</zorder>
   <zorder>horizontalSpacer_2</zorder>
   <zorder>pushButton_2</zorder>
   <zorder>textEdit</zorder>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>664</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections>
  <connection>
   <sender>pushButton_2</sender>
   <signal>clicked()</signal>
   <receiver>textEdit</receiver>
   <slot>clear()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>380</x>
     <y>64</y>
    </hint>
    <hint type="destinationlabel">
     <x>488</x>
     <y>111</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>