C++ GUI Programming with Qt 4

Jasmin Blanchette, Mark Summerfield

Mentioned 34

Accompanied by a CD-ROM containing the open source editon of Qt 4.3, as well as examples and source code from the book, an updated guide to Qt 4 programming provides information on such topics as creating dialog boxes, file menus, user interfaces, graphical user interface programming, changes from Qt 4.2 and 4.3, custom widgets for applications, SVG file generation, and more. Original. (Intermediate)

More on Amazon.com

Mentioned in questions and answers.

I am new to this Model/View Framework of Qt. In my application I want to have 1000 X 1000 cells. There should be minimum memory requirement & it should be fast. I don't know what this Model terminology is for. But I have my own class which knows how to deal with the double variables stored in the table. Currently I am using QLineEdit's with a Validator to create the array of cells. But it was way too slow for cells > 50 X 50. So I decided to go the good old MS Excel way.

So which Widget should I use: QTableWidget or QTableView?

And can anybody please explain in short what this Model/View framework is? I am not a Computer Science guy hence I am finding it tough to understand...

cmannett85's recommendation is a good one. Read the docs about a dozen times.

Then, if performance and memory issues are your primary concern and you think you can out-perform the QTableWidget implementation, then a QTableView interface on top of a QAbstractTableModel or QStandardItemModel is what you're looking for.

Since you're new to Qt's model-view architecture, I'd recommend using the QStandardItemModel until you feel like you're getting the hang of it. If your performance still isn't good enough, avoid a lot of the memory duplication and wasted objects by implementing your custom model. Plus, get yourself a good textbook and read its chapter on the model-view framework about 12 times. That section alone was worth its weight in gold, imho.

Here are the basics for Qt's custom model-view framework:

  • Your actual data is stored in a list/tree somewhere
  • The model provides a standard framework for queries to and edits for your data
  • Proxy models allow you to sort/filter your data without affecting the original model
  • The view provides a means to visually observe and interact with your data
  • Delegates (often optional) tweak the appearance of your data and provide custom editors to the data

If you're feeling both cheap and brave, check out this excerpt on implementing your own custom model. Work at it one function at a time and play with it as you go.

I want to learn Qt programming, where do I get the good stuff? I want to develop in Windows and Linux.

Which are the main IDEs?

What are the main things I want to take care of or understand before starting to learn?

Qt Creator is probably the IDE you'll want to use. It comes with excellent documentation, but you can also read it online, or look over The Independent Qt Tutorial. There is also the book Foundations of Qt Development if you prefer the dead tree format.

Qt Creator screenshot:

Qt development book on Amazon:

I also would like to recommend this book: C++ GUI Programming with Qt 4.

It is my favorite !

In C++, what is an easy-to-use graphics library? It doesn't have to be advanced, just drawing 2D shapes is enough.

If possible, something comparable to the paint-paintcomponent-repaint stuff in Java would be awesome.

The only thing I want to use it for is to draw small circles on a white screen, which refreshes every time, like:

calculate positions
    refresh and draw circles at the new positions

That's all.

Qt is a powerful, easy-to-use and cross-platform C++ library.

There is also a very good book for beginners.

How to migrate to *nix platform after spending more than 10 years on windows? Which flavor will be easy to handle to make me more comfortable and then maybe I can switch over to more stadard *nix flavors? I have been postponing for a while now. Help me with the extra push.

Linux is the most accessible and has the most mature desktop functionality. BSD (in its various flavours) has less userspace baggage and would be easier to understand at a fundamental level. In this regard it is more like a traditional Unix than a modern Linux distribution. Some might view this as a good thing (and from certain perspectives it is) but will be more alien to someone familiar with Windows.

The main desktop distributions are Ubuntu and Fedora. These are both capable systems but differ somewhat in their userspace architecture The tooling for the desktop environment and default configuration for system security works a bit differently on Ubuntu than it does on most other Linux or Unix flavours but this is of little relevance to development. From a user perspective either of these would be a good start.

From a the perspective of a developer, all modern flavours of Unix and Linux are very similar and share essentially the same developer tool chain. If you want to learn about the system from a programmer's perspective there is relatively little to choose.

Most unix programming can be accomplished quite effectively with a programmer's editor such as vim or emacs, both of which come in text mode and windowing flavours. These editors are very powerful and have rather quirky user interfaces - the user interfaces are ususual but contribute significantly to the power of the tools. If you are not comfortable with these tools, this posting discusses several other editors that offer a user experience closer to common Windows tooling.

There are several IDEs such as Eclipse that might be of more interest to someone coming off Windows/Visual Studio.

Some postings on Stackoverflow that discuss linux/unix resources are:

If you have the time and want to do a real tour of the nuts and bolts Linux From Scratch is a tutorial that goes through building a linux installation by hand. This is quite a good way to learn in depth.

For programming, get a feel for C/unix from K&R and some of the resources mentioned in the questions linked above. The equivalent of Petzold, Prosise and Richter in the Unix world are W Richard Stevens' Advanced Programming in the Unix Environment and Unix Network Programming vol. 1 and 2.

Learning one of the dynamic languages such as Perl or Python if you are not already familiar with these is also a useful thing to do. As a bonus you can get good Windows ports of both the above from Activestate which means that these skills are useful on both platforms.

If you're into C++ take a look at QT. This is arguably the best cross-platform GUI toolkit on the market and (again) has the benefit of a skill set and tool chain that is transferrable back into Windows. There are also several good books on the subject and (as a bonus) it also works well with Python.

Finally, Cygwin is a unix emulation layer that runs on Windows and gives substantially unix-like environment. Architecturally, Cygwin is a port of glibc and the crt (the GNU tool chain's base libraries) as an adaptor on top of Win32. This emulation layer makes it easy to port unix/linux apps onto Cygwin. The platform comes with a pretty complete set of software - essentially a full linux distribution hosted on a Windows kernel. It allows you to work in a unix-like way on Windows without having to maintain a separate operating system installations. If you don't want to run VMs, multiple boots or multiple PCs it may be a way of easing into unix.

I have just arrived at new company and have never worked with Qt before, but my task is to learn Qt in 2 weeks, so i can give training to others. So i got 2 weeks to learn Qt and prepare for 2 weeks long Qt teaching. I am so dead! Please point out some common mistakes, tricks, styles so i can make that training a bit better!

Thank you!

EDIT: I used this book - C++ GUI Programming with Qt 4 and it was very good. Examples were really good. Also I had some training materials from previos training. That was 2 week traning and we covered almost all Qt aspects! Nice one! ;)

Get C++ GUI Programming with Qt 4, (the official Qt book) and start reading!

  1. Book
  2. Examples and Demos Launcher

Doing all advises from that links, will gave you the right direction to move.

PS: I think 2 weeks not the problem to start using Qt...

I'm going to learn Qt and I just want to know what parts of C++, OO design and other things I must have background in? Templates, RAII, Patterns, ....?

I would suggest reading the book C++ GUI Programming with Qt4.

It covers almost all features of Qt, is easy to read for a beginner, and also includes an introduction to C++ and Java, explaining the basic concepts required for developing with Qt.

I really enjoyed this book.

What resources do you recommend for getting someone up and running in database programing using Qt?


First of all the Qt documentation, which is a really good source for anything related to Qt.

I also found this book really useful. It is the definite book on Qt and covers nearly every topic, including database handling.

Interview is descibed very nicely in this book

(Advanced Qt Programming: Creating Great Software with C++ and Qt 4)

I am using QBuffer in ReadWrite mode. One worker QThread pushes data in the buffer and another QThread reads from it.

Does QBuffer guarantee thread-safety or do I need to derive from QBuffer and add mutex stuff?

To quote Mark Summerfield's book C++ GUI Programming with Qt 4:

Qt's thread-safe classes include QMutex, QMutexLocker, QReadWriteLock, QReadLocker, QWriteLocker, QSemaphore, QThreadStorage, and QWaitCondition. In addition, parts of the QThread API and several other functions are thread-safe, notably QObject::connect(), QObject::disconnect(), QCoreApplication::postEvent(), and QCoreApplication::removePostedEvents().

Qt expects that you will use locking mechanisms around most of it's classes. The docs will say "All functions are thread-safe" if they are, and the individual functions will also specify "is thread-safe".

Notes on Qt Classes

Many Qt classes are reentrant, but they are not made thread-safe, because making them thread-safe would incur the extra overhead of repeatedly locking and unlocking a QMutex. For example, QString is reentrant but not thread-safe. You can safely access different instances of QString from multiple threads simultaneously, but you can't safely access the same instance of QString from multiple threads simultaneously (unless you protect the accesses yourself with a QMutex).

Some Qt classes and functions are thread-safe. These are mainly the thread-related classes (e.g. QMutex) and fundamental functions (e.g. QCoreApplication::postEvent()).

Because QBuffer is a direct subclass of QIODevice I would especially expect it not to be thread-safe, but there are container classes that are thread-safe for read-access, but would require locking for write access:

Container Classes

The container classes are implicitly shared, they are reentrant, and they are optimized for speed, low memory consumption, and minimal inline code expansion, resulting in smaller executables. In addition, they are thread-safe in situations where they are used as read-only containers by all threads used to access them.

Here is a list of Qt books from qt.nokia.com. Looks like as of today the most recent Qt version that's covered by any book in the list is 4.3, which was released in 2007. I'm using version 4.8, where there seem to be significant improvements from 4.3 according to wikipedia. Especially Qt Creator, the designated IDE for Qt, which I'm the most concerned about, was introduced in v4.5.

Is the book still helpful for a 4.8 user without terrible confusion?

Only one of the books that you've listed is specific to Qt (C++ GUI Programming with Qt 4) and it has a sequel which covers Qt 4.6 (Advanced Qt Programming: Creating Great Software with C++ and Qt 4). I haven't read either, but being familiar with Qt 4.3 won't be too much of a disadvantage to you when working with Qt 4.8.

That being said, Qt has some excellent documentation, which includes some very good tutorials. You may wish to dive into the tutorials before you make the decision to purchase a book. Most programmers that I've worked with develop a fairly good understanding of Qt from the tutorials alone.

I see that the designer generated UI classes be embedded using any of the following methods in Qt,

  1. Aggregation as a pointer member
  2. Aggregation
  3. Multiple, Private inheritance

enter image description here

but it is said that the second method doesn't support custom slots. Can someone elaborate on this? Why can't we implement custom slots, while using aggregation?

Also, elaborate on the advantages and disadvantages in each of the methods.

I don't think it's true that the second option doesn't support custom slots.

The choice is discussed in the official Qt documentation. See http://doc.qt.io/qt-4.8/designer-using-a-ui-file.html#compile-time-form-processing

However, note that the three approaches presented in this document do not correspond to the three options presented in Qt Creator. The first approach presented in the document, The Direct Approach, is not one of the three choices here -- this approach is not available through the Qt Creator feature which this setting controls. The second option (aggregation, or "the single inheritance approach") is available in two varieties, the slight variation being whether the ui class member is as a data member (the second option) or as a pointer member (the first option).

My preference is the third option, multiple inheritance. This is also the way used throughout C++ GUI Programming with Qt 4 (first edition available for free online), which calls this approach the cleanest. When I'm writing my class, I'm not really thinking in terms of two objects, one with the UI and the rest with the functionality, I'm thinking about just one, and multiple inheritance matches that the best. But the document gives the reasons why "aggregation as a pointer member" is the default.

I tried to make a program with Qt that counts how many days are between two dates. The problem is that I am novice in Qt and I haven't got it working.

I guess QDateTime is easy, but I don't understand the structure of a program.

Could somebody please make an example for me. Just a simple program that shows how many days it is until Christmas for example.

Your problem is very simple.

Create console application in QtCreator, and edit your main.cpp this way:

#include <QApplication>
#include <QDate>
#include <QDebug>

int main(int argc, char *argv[])
    QApplication a(argc, argv);

    // get current date
    QDate dNow(QDate::currentDate());
    // create other date
    //  by giving date 12.21.2012 (joke about end of the world)
    QDate dEndOfTheWord(2012, 12, 21);
    qDebug() << "Today is" << dNow.toString("dd.MM.yyyy")
             << "Days to end of the world: "
             << dNow.daysTo(dEndOfTheWord);

    return a.exec();

And you'll got output like:

Today is "18.12.2012" Days to end of the world: 3

P.S. But my advice to you is learn C++ (add to your favorite this topic -- The Definitive C++ Book Guide and List), and then learn Qt (I recommend C++ GUI Programming with Qt 4 by Jasmin Blanchette & Mark Summerfield and Summerfields other books). Good luck!

I'm quite new to this mobile development. I just download the Qt SDK from the Nokia web site.

My background is C,C++ and I am not very familiar with the visual/IDE kind of stuff. Normally I do things using the console and the vi editor, etc. I have been spending almost 10 years of development mainly on web applications (PHP/Python/Flex/ActionScript/JavaScript/C/etc).

I am interested in jumping into this new wagon, of applications for mobiles. I need some guide to start with, any references such online documents/readings/examples or books for a starter like me.

There is a official Qt book here. There are also other good books you can check out. You can find one that best suits your needs, given the background you came from and the knowledge you possess.

This would be a good site if you want to get started right away.

Documentation sites:

I want to know what is the standard way to organize Qt code using Qt Creator? I have this question because I am a Qt beginner & learn Qt following a c++ based tutorial i.e. Qt addressbook tutorial. Code is split very nice in that tutorial in header files & cpp files. But when I tried to build same app using Qt Creator, its quite different. I mean like there is even no declaration of controls nether in header file nor cpp file. They just dump everything to a ui file.

Here's what I do:

Generate the code from Qt Creator. This gives you a class with some name like Ui::MyDialog. Then, create a class that derives from Ui::MyDialog. Create the slots you need for the dialog in the derived class.

In the constructor, call the setupUi() method, and then connect the slots to the signals in the dialog's widgets. Elsewhere in your program (in the main() function, maybe) construct the derived class and then call its show() method.

I notice that Qt Creator doesn't show you the code it generates from your dialog's UI file. It might be instructive to use Qt Designer to see the C++ code it produces. This might give you a better idea of what to do with Creator.

A good book for learning Qt is C++ GUI Programming with Qt 4 by Blanchette and Summerfield: http://www.amazon.com/gp/product/0132354160/ref=ase_trolltech/. If you're interested in Qt, I highly recommend this.

Anyone have any idea how I can implement this? I'd like to have a function basically exactly like impoly in matlab or the "polygon sections" tool in imageJ, where you click to form a polygonal section and then each node can be adjusted, etc. I'd also like to have access to this function from Qt since I'm trying to make a gui for a small program I wrote.

Also, I'd like to avoid making calls to the matlab function because it's part of the image processing toolbox which isnt free. Thanks.

I think you would need to code this yourself. There is an excellent example in the C++ GUI Programming with Qt 4 book (there's a PDF copy floating around online; I think it's legal) where they show you how to create a diagram with nodes and links. The chapter is called "Item-based rendering with Graphics View".

The basic idea is that you have some draggable nodes, which are QGraphicsItems with the ItemIsMovable flag set to true, and then some links that connect them, which are QGraphicsLineItems. All of these would go into a composite QGraphicsItem representing the ROI, and all of those would go into a QGraphicsScene, which would be displayed by a QGraphicsView.

Bottom line: there isn't a built-in copy of the MATLAB function, but all the tools are there for you.

As an intermediate-level programmer of C++ (that have been working on C++ for console apps and used only FLTK for GUI apps), I heard much about Qt5 to be suitable and very powerful for creating apps for many platforms such as Windows, Linux, iOS and also Android.

I'm now a novice on Qt and to create apps for PC, I'm going to start reading this book (which is of Qt4).
But I, too, like to start reading a good book on Qt5 that makes me able to create apps for iOS and Android. Is there such a good book?

Any suggestion is fully appreciated.

I am new with Qt and Qt Creator. I have created some sub-directories in order to separate the classes of the project. Now, I am trying to include the headers in their respective sources but unsuccesfully.

I have separated the project as follow (-> means subdir):

   -> common
      -> tools

   -> common
      -> tools

in tools directories I have abc class. In my .pro file, it shows the following:



Even though I cleaned and runned again qmake, there is a problem: when I want to include "abc.h" into "abc.cpp" in this way:


 #include "Header/common/tools/abc.h"

Qt Creator shows this problem:

 C1083: Cannot open include file:  Headers/common/tools/abc.h: No such file or directory

I guess it is a problem of path which I can't find out. Can you? Thank you in advance

If I were you I would get a QT book. There are many of these little idiosyncrazies, and a book makes sense of it all when you read it in sequence.

I read this QT3 book, and it was the best tech-book I ever read, but it is out of date now. Here is one by the same author - I got that one too, and it is good too. Haven't read all of it.

I can install translator as myApp.installTranslator(&translator)

Is it possible to have multiple translation files and load them from different parts of my application? How can I do it?

Yes, you can. As doc said:

Adds the translation file translationFile to the list of translation files to be used for translations.

Multiple translation files can be installed. Translations are searched for in the reverse order in which they were installed, so the most recently installed translation file is searched first and the first translation file installed is searched last. The search stops as soon as a translation containing a matching string is found.

Installing or removing a QTranslator, or changing an installed QTranslator generates a LanguageChange event for the QCoreApplication instance. A QApplication instance will propagate the event to all toplevel windows, where a reimplementation of changeEvent can re-translate the user interface by passing user-visible strings via the tr() function to the respective property setters. User-interface classes generated by Qt Designer provide a retranslateUi() function that can be called.

The function returns true on success and false on failure.

You need to load some translation file, qApp macro to get instance of QApplication outside main() function and do something like:

QTranslator translator;//somewhere

void MainWindow::on_someButton_clicked()
    qApp->installTranslator( &translator );
    ui->retranslateUi(this);               //for Qt designer

Also you can remove translator with:

void MainWindow::on_someButton_2_clicked()

Internationalization is a big part, so I can suggest also next links:

Internationalization with Qt

Writing Source Code for Translation

And books:

Foundations of Qt Development (Expert's Voice in Open Source) Chapter 10

C++ GUI Programming with Qt 4 (2nd Edition) (Prentice Hall Open Source Software Development Series) Chapter 18

Qt4.8. Professionalnoe programmirovanie na C++ (Russian) Chapter 31 (for russian-speakers)

I'd like to pick up QT for some future projects and I would like to know, what are some important concepts to keep in mind when developing in QT? I can see the concept of signals and slots vs. wxwidget's events as something pretty important to get a good understanding of, so what other concept for QT are important that are not in wxWidgets?

The only real magic is signals/slots

There are a few other differences, Qt generally uses more modern C++ constructs than wx which is a bit more MFC era. But like most guis it's just a matter of learning the control->widget->window hierarchy.

It is worth getting the Qt4 book or at least reading the free Introduction to Design Patterns in C++ with Qt 4

Depends on what platform you are using but check out Qt Creator or if you are using MSVC the qt-addin. The drag-drop dialog layout still isn't as nice as wxDesigner (IMHO) I usually endup coding most of the window, toolbar etc layout in c++

I'm trying to learn how to program qt. I know c++ very well, and have been able to do the standard hellow world program. I cannot find any books or documention on qt. I did find a tutriol,at http://www.digitalfanatics.org/projects/qt_tutorial/chapter01.html but the project would not compile, said it could not find include file #include .

Does anubofy know of a book or anything that will tell you how to program the qt frame work?

The book C++ GUI Programming with Qt 4 is the definitive guide to programming Qt, I think. But you can also learn much from the Qt documentation itself, if you know where to start. The Qt documentation is really good and extensive, a perfect reference. And as Qt is also very intuitive, the doc can also help you find new functionality (If you have an idea, how some feature could be realized in Qt, it is often realized just that way).

I want to know how can I build UIs like skype using standard .Net/C#. Is it possible at all?


AFAIK skype was built using Qt4, it's rather easy to build custom gui widgets, check C++ GUI Programming with Qt4, 2nd Edition and this tutorial.

P.S. check this to see how to build qt4 on windows using MSVC 2008.

Qt (pronounced officially as cute (/kyut/) though commonly pronounced as Q.T. (/ˈkyu ti/)) is a cross-platform application development framework widely used for the development of GUI programs (where it functions like a cross-platform widget toolkit), and also used for developing non-GUI programs such as console tools and servers. wikipedia

Qt was created by Trolltech, and was acquired by Nokia in 2008. One month after the end of symbian development at Nokia, Nokia decided to sell Qt. From September 2012 and until now Qt is managed by the Qt Company which is subsidiary of Digia.

Current version:

The latest official release is 5.7. The major version indicates API and binary compatibility.


There is extensive official documentation (all classes) available on Qt's website, in addition to tutorials and examples. You will often see these tutorials and examples referenced in the questions and answers on this site. Qt also provides an integrated development environment named Qt Creator. Though it mainly aims at creating Qt applications, it can be used to create regular C++ applications.

Tagging recommendation:

You will often see questions tagged specifically as , , or to indicate that the question is related to Qt 3.x, 4.x, or 5.x respectively. Qt 3.x is no longer supported and Qt 5.x has been released a while ago, so the default when version information is lacking is to assume version 5.x of the API.

Writing recommendation

Qt is a cross-platform application development framework, but QT (QuickTime) - is an extensible multimedia framework developed by Apple Inc.


Qt is available under four different licenses:

  1. GNU General Public License (GPL) version 3.0
  2. GNU Lesser General Public License (LGPL) version 2.1
  3. GNU Lesser General Public License (LGPL) version 3.0
  4. Qt Commercial License (which comes in three versions with different features and prices: "Indie Mobile", "Professional" and "Enterprise")

Video Courses

Pluralsight has a series of three courses on Qt:

Introduction to Qt: A C++ Cross Platform Application Framework
Qt Quick Fundamentals
Integrating Qt Quick and C++

Pluralsight is a subscription based but if you're only interested in Qt you can send the author @todgentille a private tweet and request a week long VIP pass. You'll get unlimited access to the higher subscription level for a week that allows viewing online and offline and you can download the course materials.

Voidrealm released a full free series of Qt tutorials for beginners on his youtube channel.


Qt introductory books:

Integrated learning of Qt and C++:

Concepts of UI design:

Please check out the official Qt documentation for more details of recommended books about Qt programming.


Please do not answer poor questions that will likely get closed or even deleted later. We are aiming for high-quality in this tag and so we do not wish to encourage poor questions coming by feeding them with answers.

i am new to QT, i don't have any knowledge about it. from scratch i need to learn, if you people have some tutorials or some technical blogs please feel free to share with me.. i have seen some stuffs in troltech, tell me any thing else apart from it.


I have a main window class built in Qt Designer and called Ui_MainWindow which has a publicly defined QProgressBar object called *downloadProgressBar. I also have a class called HttpGet, which is supposed to download a file from a url supplied by the user. There is a function called HttpGet :: updateReadProgress that is intended to update downloadProgressBar as the file is downloaded, which would require manipulation of the progress bar from within the function, but I unable to figure out how to go about this.

My assumption would be that I would include the relevant header file and then access the progress bar object via

Ui_MainWindow :: downloadProgressBar -> setValue()

but when I try to compile this code, I get the following error:

error: ‘downloadProgressBar’ is not a member of ‘Ui_MainWindow’

which tells me that I've misunderstood what it is I'm asking the compiler to do. Can someone please tell me how I should go about updating the progress bar from within HttpGet, or if that's the wrong thing to do, can you please give me a better alternative?

Ui_MainWindow is a class. you need object of that class to deal with members. Also, you should read C++ GUI Programming with Qt 4 (2nd Edition) before writing more code. it's awailable in pdf around the web...

I'm a little new to Qt. I have been programming in C++ for quite a while.

I want to create an application in Qt/C++ just because it's very easy to create the GUIs. I wanted to know that is it possible that I can write a C++ class in native C++ and use it in my Qt application.

I want to write the GUI in Qt and do all processing using my native C++ code rather than Qt/C++. Is it possible to compile a DLL in native C++ and then load and use it in QtC++ application?

I really want to write a GUI application using C++.

You are able to use any C++ code you would like. I have have actually had questions with the same thought process in my day to day work. First you need to grok the idea that Qt uses normal C++ and special tools to create a robust set of libraries.

Later on come back and you will want to learn more about the items below:

  • Signals and Slots
    • This is how events are wired together.
    • One objects sends a signal and all connected slots get called with the given parameters.
    • Signals and slots are an example of the Observer Pattern.
  • Designer Forms
    • The form designer lets you lay out controls using simple drag and drop operations.
    • The .ui file gets transformed into C++ code as part of the build process.
  • Layouts
    • You can do a lot of things with just the horizontal, vertical and grid layouts
  • Spacers
    • You can push things up, down, left, or right using different types of spacers
    • They look like a spring on the form designer.
  • Resource Files
    • Lets you embed graphics and other content directly into the application.
  • Models, Views and Delegates
    • Hard to avoid if your have a non trivial UI

There are other things too, but knowledge in these areas will let you make some decent desktop applications. My favorite reference is older, but still a goodie: "C++ GUI Programming with Qt4" http://www.amazon.com/Programming-Edition-Prentice-Software-Development/dp/0132354160

Yes QT4 is "old" and QT5 has been out for some time... but the book provides a solid foundation that you will be able to add information to.

Im a .net developer , i have studied a course in C followed by one in C++ before as an introduction to Programming, i want to learn Qt, im not sure if it is even possible for me to start right away read a book in Qt or should i start reading a book in C++ first !

Do you think that a Senior C# developer would be able to learn Qt by reading a book in Qt right away if he has small background in C++ ?

Having done what you are describing, I would make the following suggestions -

  1. For a book, take a look at C++ GUI Programming with Qt 4 (2nd Edition) . This is best book I've seen. But there are others, so do look around at them.
  2. Pick a topic you wish to learn, then work through one of the examples. They are very good and there are a lot of them on all key topics.
  3. I've always view .net and Qt to have equivalent concepts. For example, when you see QObject or QString, think c# Object or String. This helped a lot.
  4. Read all posts here with regards to Qt. They are a great source of info. Some of the questions have forced me to do research on new areas to understand both the question and the answer.

Finally, what order should you do these. With regards to books, going off of your comments about learning c++, buy a book. I've always found them to be very helpful. For the other 3 topics I've mentioned, I would recommend doing items 2, 3 & 4 all at the same time. Pick a simple concept you wish to learn about, then work through examples until you understand it.

The two most important things to learn for a good foundation in Qt are Signals & Slots and the GUI. You really need to understand basics on these before you get very far using Qt.

Personally, I think learning c++/Qt is a good thing to do for a c# programmer. It will give you a new perspective on how to solve problems.

In general, is the order in which I add my widgets into each other, the same order I access them back?


If I have a bunch of QPushButton in a QHBoxLayout, and this layout in a Window::ui, can I access those button by simply ui->button_name? or Do I must do ui->layout->itemAt(idx)?

EDIT: My question aims to find an easy way to access elements that are deep into the hierarchy, like a label in a frame, inside a layout, inside a frame, inside the window etc...

PS: Also, I would really appreciate any documentations about good practices of GUI architecture!

Use ui->object_name you don;t need to worry about the layout!
It's possible to redesign the layout, eg. for different platforms, without changing any of the C++ code.

There are a couple of good books on Qt and the sample code is very good.

C++ GUI Programming with Qt 4

Advanced Qt Programming

So there is a great example that comes with Qt. it is in Examples Books Demonstration. I am new to c++ and even more new to Qt so I wonder what shall be done to add such functionality? I mean what code changes shall be made to it to add 2 buttons like new and submit and what functions they shall trigger?

You definitely need to read a Qt tutorial (and maybe a C++ tutorial depending on your knowledge of C++).

Have a look at this SO question : Qt Tutorials?

Doing the tutorial, you'll learn how to add buttons to a form and how a function will be triggered when a button is clicked.

I also recommend reading C++ GUI Programming with Qt 4.

Possible Duplicate:
Qt tutorials for beginners

What resources for learning Qt by example do you recommend?


I have a small project which send some data over network using QTcpSocket. The server works fine but the client(code here) seems does nothing. If I set breakpoint at tcpSocket.connectToHost("",port); it does jump in, but not any slots I defined.

I can't figure out what's wrong. I think the environment is ok because I can build 2 working examples from Qt GUI Programming

Any ideas are appreciated.

You do not have a QApplication instance and thus no event loop which does all the event / signal&slot handling.

So you at least need a QCoreApplication instance like this in main.cpp:

int main(int argc, char *argv[])
    QCoreApplication a(argc, argv);
    Client client;

    return a.exec();

I want to make a realy simple thing, but I am new to c++ and Qt.

On click on some widget I want to change background color of this clicked widget and background of another widget with specific name. Widgets don't have got onclick(). I thing I have to use something different like mousePressEvent, but I don't know how.

I am new to c++ and Qt.

You should probably start with working through a Qt book like C++ GUI Programming with Qt 4.

Widgets don't have got onclick(). I thing I have to use something different like mousePressEvent

Yes, for your particular issue, you can override the QWidget::mousePressEvent() method:

class MyWidget : public QWidget {
    void mousePressEvent ( QMouseEvent * event );
void MyWidget::mousePressEvent(QMouseEvent * event ) {
    // do whatever you want when the mouse button is clicked, e.g. 
    // modify the palette of the widget to change the background color

Here is a sample application (an Icon editor) which does something similar: http://files.itslearning.com/data/764/2405/qt4/ch05lev1sec2.html

I am doing a computer vision project where i have to recognize the text in an image and output it. So I want to develop a simple GUI for my C++ code, where I can show the image in a frame and show the recognized text next to it in some text box. I have no previous experience in developing GUIs, but i think it shouldn't be hard since the interface is not complicated. I am using windows 8 and both eclipse and netbeans are installed. Please provide me with the steps and packages I need to download. Any references to some tutorials or code will be very appreciated.

I suggest to use Qt, the cross-platform application framework.

I am new to Qt How to learn it , suggest me easy tutorials and books. Thank you.

In addition to Qt official documentation mentioned above I would also recommend you a book written by Jasmin Blanchette and Mark Summerfield called C++ GUI Programming with Qt 4. It is very useful if you just start with Qt and explains everything very good. After you more or less good Qt programmer, try Advanced Qt Programming: Creating Great Software with C++ and Qt 4. Its more advanced but will help you to understand hard topics of Qt and make you much more professional in Qt. That from my own experience.

Good luck learning Qt !

i have a compiler project and i have used c++ , flex and bison in it so bison and flex produce .cpp files to me and i can compile and run them but i want to make a gui to my project so i want to learn a technique to make a gui (in java or qt) to write input to my project and get output

There seems to be a bit of a language barrier, so let me attempt to state what I think I understand before providing my answer.

It appears that you're in a Compilers class, and your project is to write a C++ compiler. You've chosen to program this project using C++, with the help of bison and flex. Now that your project is effectively complete, you'd like to add a GUI to it (I guess in some way making steps towards it becoming an IDE).

Writing a GUI in C++ isn't as easy as I'd like for it to be. From friends that have had to do the same thing in their compilers and graphics classes, I hear that this book is a good start for using Qt to make GUI programs in C++. With regard to programming the GUI in Java, you'll probably be using the swing library; and I always just read the javadocs (linked). But again, I haven't done that since University.

Hope that helps some! Good luck!