- In Qt Creator, start a new project; click File -> New File or Project.
- Select Qt Application Project -> Mobile Qt Application
- Enter the name and location of you project.
- Accept the default selection on the targets for your project, all selected.
- Review your Class Information, & click Next.
- Click Finish
- Your first code will be generated, and the screen will be switched to Edit mode.
Extend Forms in the Projects pane, double click on mainwindow.ui to switch to Design Mode.
- Drag a Label to the from, adjust the location and size, double click on it and enter the text to be display, "Who are you?". Change the objectName under QObject property on the right to labelWhoAreYou.
- Add a Line Edit with objectName lineEditName, leave the text empty.
- Add a Push Button with objectName pushButtonOK and change the text to "OK".
- Add a Label with objectName labelHello and chnage the text to "I'm Maemo".
- Click on Edit to switch to Edit Mode, double click on mainwindow.h.
Add a slot to mainwindow.h
private slots:
void buttonClicked();
- Double click on mainwindow.cpp to connect with pushButtonOK signal clicked().
connect(ui->pushButtonOK, SIGNAL(clicked()), this, SLOT(buttonClicked()));
- And implement buttonClicked()
void MainWindow::buttonClicked()
{ ui->label_Hello->setText("Hello:" + ui->lineEdit_Name->text());
}
- Save All, and click on the green arrow to Run.
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();
protected:
void changeEvent(QEvent *e);
private:
Ui::MainWindow *ui;
private slots:
void buttonClicked();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButtonOK, SIGNAL(clicked()), this, SLOT(buttonClicked()));
}
void MainWindow::buttonClicked()
{ ui->labelHello->setText("Hello:" + ui->lineEditName->text());
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}