Difference between revisions of "Qt5/Qt-Creator 3-Widget Hello World Application"
(→Project3Widgets.pro.user) |
|||
(3 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= | ||
− | + | <br /> | |
== Project3Widgets.pro == | == Project3Widgets.pro == | ||
<br /><br /><source lang="text"> | <br /><br /><source lang="text"> | ||
Line 219: | 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.
Contents
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>