EzDevInfo.com

qt interview questions

Top qt frequently asked interview questions

Checking if a folder exists (and creating folders) in Qt, C++

In Qt, how do I check if a given folder exists in the current directory?
If it doesn't exist, how do I then create an empty folder?


Source: (StackOverflow)

STL or Qt containers?

What are the pros and cons of using Qt containers (QMap, QVector, etc.) over their STL equivalent?

I can see one reason to prefer Qt:

  • Qt containers can be passed along to other parts of Qt. For example, they can be used to populate a QVariant and then a QSettings (with some limitation though, only QList and QMap/QHash whose keys are strings are accepted).

Is there any other?

Edit: Assuming the application already relies on Qt.


Source: (StackOverflow)

Advertisements

QLabel: set color of text and background

How do I set color of text and background of a QLabel ?


Source: (StackOverflow)

Adding external library into Qt Creator project

How can I add external library into a project built by Qt Creator RC1 (version 0.9.2)? For example, the win32 function EnumProcesses() requires Psapi.lib to be added in the project to build.


Source: (StackOverflow)

What does : mean?

I have 2 classes:

class base {
    virtual void foo() {};
};

class derived : public base {
    void foo() { base::foo(); }
};

I made a mistake and wrote base:foo(); instead of base::foo();. The code was compiled and run, but segfaulted.

I don't know how I can Google it and don't know what it is, but I'm very interested: what does that mean?

base:foo();

If it is important:

class base : public QAbstractGraphicsShapeItem

Source: (StackOverflow)

How to print to console when using Qt

I'm using Qt4 and C++ for making some programs in computer graphics. I need to be able to print some variables in my console at run-time, not debugging, but cout doesn't seem to work even if I add the libraries. Is there a way to do this?


Source: (StackOverflow)

How to convert int to QString?

Is there a QString function which takes an int and outputs it as a QString?


Source: (StackOverflow)

How to specify different Debug/Release output directories in QMake .pro file

I have a Qt project and I would like to output compilation files outside the source tree.

I currently have the following directory structure:

/
|_/build
|_/mylib
  |_/include
  |_/src
  |_/resources

Depending on the configuration (debug/release), I will like to output the resulting files inside the build directory under build/debug or build/release directories.

How can I do that using a .pro file?


Source: (StackOverflow)

How to change string into QString?

What is the most basic way to do it?


Source: (StackOverflow)

Why Qt is misusing model/view terminology?

I think that the terminology used in Qt with model/view controls is flawed. On their explanation page they state, that they simplified the MVC to MV by merging View and Controller and they are giving the following picture:

picture explaining Qt MVC

However I think, they misnamed the roles of objects and I think that,

  1. What they call View with merged Controller is in fact a View only.
  2. What they call Model is in fact Controller only.
  3. If you really want to have a model it would be somewhere where their "Data" is.

I am speaking about usual and sane way you would use Qt model/view component in your app. Here are the reasons:

  1. This is typically Qt component which is used as is, without adding any Controller logic specific to your objects)
  2. This is hardly a Model, just because you should implement several Qt methods like rowCount, columnCount, data etc. which have nothing to do with your model. In fact there are typical model methods found in Controllers. Of course, you can implement both Controller and Model logic here, but first it would be quite bad code design and secondly you would merge Controller and Model not Controller and View as they state.
  3. As said in reason 2. if you want to separate Model logic that it is surely not the blue box on the picture, but rather the dashed "Data" box (communicating to real Data of course).

Is Qt wrong in their terminology, or it is just me who does not understand? (BTW: The reason why it is not academic question is that I have started to code my project following their naming and I have soon found out, that the code clearly is not right. It was only after that when I realized, that I should not try put Model logic in what they call Model ;-) )


Source: (StackOverflow)

Memory management in Qt?

I'm quite new to Qt and am wondering on some basic stuff with memory management and the life of objects. When do I need to delete and/or destroy my objects? Is any of this handled automatically?

In the example below, which of the objects I create do I need to delete? What happens to the instance variable myOtherClass when myClass is destroyed? What happens if I don't delete and/or destroy my objects at all; will that be a problem to memory?

MyClass.h

class MyClass
{

public:
    MyClass();
    ~MyClass();
    MyOtherClass *myOtherClass;
};

MyClass.cpp

MyClass::MyClass() {
    myOtherClass = new MyOtherClass();

    MyOtherClass myOtherClass2;

    QString myString = "Hello";
}

As you can see this is quite newbie-easy stuff but where can I learn about this in an easy way?


Source: (StackOverflow)

Configuring the GCC compiler switches in Qt, QtCreator, and QMake

I recently tried to use Qt Creator 1.3.2, Qt 4.6.2, and GCC 4.4.0 (32-bit version) on Windows 7 (64-bit) to compile an application using some of the experimental C++0x extensions and encountered the following (fatal) error:

This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

In my search for a solution, I came across the thread qmake and compiler flags?, and added the following to the .pro file:

CXXFLAGS += -std=c++0x

but that didn't seem to make a difference.

So, I expect there's some tag I need to add to the .pro (project) file, but I've never messed with the GCC compiler switches in Qt, QMake, and QtCreator before, and I am uncertain about the proper invokation / incantation. So, my question is how do you set GCC compiler switches when using QtCreator, QMake, and Qt?


Source: (StackOverflow)

How do I configure Qt for cross-compilation from Linux to Windows target?

I want to cross compile the Qt libraries (and eventually my application) for a Windows x86_64 target using a Linux x86_64 host machine. I feel like I am close, but I may have a fundamental misunderstanding of some parts of this process.

I began by installing all the mingw packages on my Fedora machine and then modifying the win32-g++ qmake.conf file to fit my environment. However, I seem to be getting stuck with some seemingly obvious configure options for Qt: -platform and -xplatform. Qt documentation says that -platform should be the host machine architecture (where you are compiling) and -xplatform should be the target platform for which you wish to deploy. In my case, I set -platform linux-g++-64 and -xplatform linux-win32-g++ where linux-win32-g++ is my modified win32-g++ configuration.

My problem is that, after executing configure with these options, I see that it invokes my system's compiler instead of the cross compiler (x86_64-w64-mingw32-gcc). If I omit the -xplatform option and set -platform to my target spec (linux-win32-g++), it invokes the cross compiler but then errors when it finds some Unix related functions aren't defined.

Here is some output from my latest attempt: http://pastebin.com/QCpKSNev.

Questions:

  1. When cross-compiling something like Qt for Windows from a Linux host, should the native compiler ever be invoked? That is, during a cross compilation process, shouldn't we use only the cross compiler? I don't see why Qt's configure script tries to invoke my system's native compiler when I specify the -xplatform option.

  2. If I'm using a mingw cross-compiler, when will I have to deal with a specs file? Spec files for GCC are still sort of a mystery to me, so I am wondering if some background here will help me.

  3. In general, beyond specifying a cross compiler in my qmake.conf, what else might I need to consider?


Source: (StackOverflow)

Qt Events and Signal/Slots

In the Qt world what is the difference of events and signal/slots?

Does one replace the other? Are events an abstraction of signal/slots?


Source: (StackOverflow)

Correct way to quit a Qt program?

How should I quit a Qt Program, e.g when loading a data file, and discovered file corruption, and user need to quit this app or re-initiate data file?

Should I:

  1. call exit(EXIT_FAILURE)
  2. call QApplication::quit()
  3. call QCoreApplication::quit()

And difference between (2) and (3)?


Source: (StackOverflow)