{"id":18,"date":"2013-08-27T23:33:26","date_gmt":"2013-08-28T04:33:26","guid":{"rendered":"http:\/\/eastfist.com\/qt_tutorials\/?p=18"},"modified":"2017-08-26T03:27:11","modified_gmt":"2017-08-26T09:27:11","slug":"hello-world-with-qpainter","status":"publish","type":"post","link":"https:\/\/eastfist.com\/qt_tutorials\/hello-world-with-qpainter\/","title":{"rendered":"Hello World with QPainter"},"content":{"rendered":"<p>This is the most basic of basic tutorials when it comes to computer programming. It is virtually done to death. The basic idea is for you as the programmer to be able to send a message to the computer and have it return the message to you as output. Originally, this would be done via command-line or command-prompt, a scary black void of an interface. So in this case, I&#8217;ll show you how to actually engage the user by hooking into the default GUI mainWindow.<!--more--><\/p>\n<ol>\n<li>Open Qt Creator 2.8.0<\/li>\n<li>Go to File&gt;New File Or Project<\/li>\n<li>Choose Projects&gt;Applications&gt;Qt Gui Application<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_03_newProj.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-39\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_03_newProj-300x141.png\" alt=\"helloWorld_03_newProj\" width=\"300\" height=\"141\" \/><\/a><\/li>\n<li>For &#8220;Introduction and Project Location&#8221;, name your project &#8220;Hello World&#8221;, and browse to a memorable location where to create it in.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_04_location.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-40\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_04_location-300x126.png\" alt=\"helloWorld_04_location\" width=\"300\" height=\"126\" \/><\/a><\/li>\n<li>For &#8220;Kit Selection&#8221;, check &#8220;Qt 4.8.5 (minGW)&#8221;<br \/>\nNote: there may be other kits available to you depending on if you have those installed<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_05_selectKit.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-41\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_05_selectKit-300x156.png\" alt=\"helloWorld_05_selectKit\" width=\"300\" height=\"156\" \/><\/a><\/li>\n<li>For &#8220;Class Information&#8221;, keep defaults.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_06_classInfo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-42\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_06_classInfo-300x126.png\" alt=\"helloWorld_06_classInfo\" width=\"300\" height=\"126\" \/><\/a><\/li>\n<li>For &#8220;Project Management&#8221;, keep defaults. Click Finish.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_07_summary.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-43\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_07_summary-300x160.png\" alt=\"helloWorld_07_summary\" width=\"300\" height=\"160\" \/><\/a><\/li>\n<li>Qt Creator automatically loads newly generated &#8220;mainwindow.cpp&#8221; by default, ready to be edited.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editor00.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-50\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editor00-300x162.png\" alt=\"helloWorld_editor00\" width=\"300\" height=\"162\" \/><\/a><\/li>\n<li>Note: If you don&#8217;t have a debugger, then you won&#8217;t be able to debug.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_buildModeD.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-44\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_buildModeD-300x254.png\" alt=\"helloWorld_buildModeD\" width=\"300\" height=\"254\" \/><\/a><br \/>\nSo when you run it, set it to Release mode. If there are any serious bugs, the compiler will still throw an error.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_buildModeR.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-45\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_buildModeR-300x254.png\" alt=\"helloWorld_buildModeR\" width=\"300\" height=\"254\" \/><\/a><\/li>\n<li>Hit Run.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-53\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_run-300x204.png\" alt=\"helloWorld_run\" width=\"300\" height=\"204\" \/><\/a><\/li>\n<li>At the very minimum, you will have a window that can run. But it doesn&#8217;t really do anything useful.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_mainWin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-52\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_mainWin-300x243.png\" alt=\"helloWorld_mainWin\" width=\"300\" height=\"243\" \/><\/a><\/li>\n<li>Close the window or Stop (the red square in the debugger tool strip).<\/li>\n<li>Now, let&#8217;s actually do some real coding. Click on mainWindow.h to open it in the editor.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editHeaderA.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-47\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editHeaderA-300x273.png\" alt=\"helloWorld_editHeaderA\" width=\"300\" height=\"273\" \/><\/a><br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editHeaderB.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-48\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editHeaderB-300x162.png\" alt=\"helloWorld_editHeaderB\" width=\"300\" height=\"162\" \/><\/a><\/li>\n<li>Make the following changes to mainWindow.h:<br \/>\nAdd<\/p>\n<pre><code>protected:\r\n  void paintEvent(QPaintEvent *);<\/code><\/pre>\n<p><a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editHeaderC.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-49\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_editHeaderC-300x287.png\" alt=\"helloWorld_editHeaderC\" width=\"300\" height=\"287\" \/><\/a><\/li>\n<li>Now that you have overriden the mainWindow widget&#8217;s default paintEvent, you must provide your own method for painting. Open mainWindow.cpp in the editor.<br \/>\nAdd<\/p>\n<pre><code>#include \"QPainter\"\r\n<\/code><\/pre>\n<p>and<\/p>\n<pre><code>void MainWindow::paintEvent(QPaintEvent *)\r\n{\r\n   QPainter painter(this);\r\n   painter.drawText(QRect(20,20,200,200),\"Hello World\");\r\n   painter.end();\r\n}\r\n<\/code><\/pre>\n<p><a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_customPaint.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-46\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_customPaint-300x222.png\" alt=\"helloWorld_customPaint\" width=\"300\" height=\"222\" \/><\/a><\/li>\n<li>Save and run.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_run.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-53\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_run-300x204.png\" alt=\"helloWorld_run\" width=\"300\" height=\"204\" \/><\/a><\/li>\n<li>If everything is set properly, what you get is the same basic mainWindow plus your newfound ability to paint on the widget.<br \/>\n<a href=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_finalWin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-51\" src=\"http:\/\/eastfist.com\/qt_tutorials\/wp-content\/uploads\/2013\/08\/helloWorld_finalWin-300x243.png\" alt=\"helloWorld_finalWin\" width=\"300\" height=\"243\" \/><\/a><\/li>\n<\/ol>\n<p>I hope you found that helpful. This is your quickstart intro to <a href=\"http:\/\/qt-project.org\/doc\/qt-4.8\/qpainter.html\">QPainter<\/a>, which is quite robust. You can now experiment with background colors, fonts, placement, and other drawing features. Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the most basic of basic tutorials when it comes to computer programming. It is virtually done to death. The basic idea is for you as the programmer to be able to send a message to the computer and have it return the message to you as output. Originally, this would be done via &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/eastfist.com\/qt_tutorials\/hello-world-with-qpainter\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hello World with QPainter&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[16,21,22,20,24,18,14,17,3,19,23],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-qt-2","tag-4-8-5","tag-advanced","tag-c","tag-demo","tag-gcc","tag-hello-world","tag-mingw","tag-qpainter","tag-qt","tag-tutorial","tag-windows"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":14,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":323,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/18\/revisions\/323"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}