Friday, December 4, 2009

Hello! My First Widget, a very simple Qt application with a PushButton.

In this work, I will create a very simple Qt Application with a dummy button (inherite from QPushButton), start from Empty Qt Project.

Start QtCreator, click File -> New project or Project... on the top menu, Select Empty Qt Project under Project.


Fill in the Name (MyFirstWidget here) of your project and browse to the locate where to create your project on, and click Next


Then Finish.


Because what you created is a Empty Qt Project, only a dummy MyFirstWidget.pro will be created.

Create a main.cpp, click File -> New project or Project..., select C++ Source File under C++, and Click OK.


Fill in the Name of the source file, main, click Next, and then Finish.




Insert the following code in main.cpp


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.setWindowTitle("My First Widget");
window.show();

QPushButton *button = new QPushButton("Hello", &window);
button->move(100, 100);
button->show();
return app.exec();
}


Save All and Click the Run button (Green Arrow).


Up to now, we created a Qt application, with a no function button.