Friday, July 3, 2009

GUI Hello World: Add the code

Finally, add some code to the source, to make it work.

Add a slot to mainwindow.h

private slots: void buttonClicked();


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;

private slots:
void buttonClicked();

};

#endif // MAINWINDOW_H

mainwindow.h

Modify mainwindow.cpp to implement buttonClicked() and connect with okButton signal clicked().


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

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

connect(ui->okButton, SIGNAL(clicked()), this, SLOT(buttonClicked()));
}

void MainWindow::buttonClicked()
{ ui->label_Hello->setText("Hello:" + ui->lineEdit_Name->text());
}


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

mainwindow.cpp

Save All, Build and Run again.



prev. -> GUI Hello World: Create GUI using Qt Designer