Qt Centre Forum FAQ


How do I compile static Qt libs?

Pass "-static" switch to configure and rebuild Qt. It is advised to build static libraries in release mode only, as they won't boost the resulting applications size that much.

How to compile Qt debug libraries?

Pass one of -debug or -debug-and-release options to configure and recompile Qt or choose an appropriate entry in your Start menu Qt group if working on Windows.

How do I static compile my application?

First make sure Qt is compiled statically.

If you have only static libs compiled (without shared ones), this is all you need – if shared libraries are not found, the compiler should use static ones by default.

If you have both static and shared libraries on your system, you have to tell your compiler to use static libs only. To do this, you have to pass an switch apropriate for your compiler to QMAKE_LFLAGS variable. For GCC compilers (g++, MinGW) the switch is "-static". For other compilers, check their documentation.

To pass the option, add a following line to your project file:

QMAKE_LFLAGS += -static
and run qmake. Remember that such a compilation will fail if you don't have any of the libraries needed as static.

I have created a widget/dialog and it is not visible when I call show(). Why?

You probably created the widget on stack meaning something like this:

void someFunction{
  QDialog dialog;

In this situation dialog is a local variable (local to the block it was declared in) and will be destroyed when the scope of the block ends. Because of that your dialog will be destroyed before it even has a chance to reveal itself (as show() doesn't cause an immediate action but posts an event to the event queue that a widget wants to be seen).

To prevent that from happening, you should place your widget on heap instead:

void someClass::someMethod(){
  QDialog *dialog = new QDialog(this);

In this situation only a pointer is a local variable, so only a pointer gets destroyed when the function returns, but the object it points to will remain untouched.

Remember to make sure the memory is freed when the object is no longer needed or you'll experience memory leaks.

I have two forms. How to open a second form after a button is clicked in the first one?

Assuming you have two form classes: Form1 and Form2 and that Form1 has a pushbutton "button" child and Form2 is derived from QDialog.

Add a custom slot to Form1 and fill it with:

void Form1::openForm2(){
  static Form2 *form2 = new Form2(this);
  form2->activateWindow(); // or form2->setActiveWindow() in Qt3

If you want Form2 to be modal change it to:

void Form1::openForm2(){
  Form2 form(this);

Then you have to connect a proper signal to this slot. In the constructor of Form1 add:

connect(button, SIGNAL(clicked()), this, SLOT(openForm2()));
If the form is not based on QDialog, you can still use the first approach, just don't pass "this" as a parent pointer.
void Form1::openForm2(){
  static Form2 *form2 = new Form2;
Note, the form will not be deleted when closed.

How to fix an error message similar to "cannot find -lQtGuid" ?

You are trying to compile your application in debug mode, but you don't have the debug libraries available. Either build them or compile your application in release mode by removing a CONFIG+=debug and/or adding a CONFIG+=release entry to your qmake project file.

How to change the colour of a button or any other Qt widget?

For things like buttons it is best to use stylesheets either in code:

button->setStyleSheet("color: #0000dd; background-color: red;");
or in Designer by choosing an appropriate option from the context menu of the widget and entering the stylesheet text.

For other widgets you may start by changing the palette of the widget:

QPalette pal = widget->palette();
pal.setColor(widget->backgroundRole(), Qt::red);
If that fails, fall back to the stylesheet approach.

Search FAQ


