QT notes

Compiling and linking

qmake -project ← This is not needed if using qt designer
qmake project_name.pro
gmake

Basic main program

#include < qapplication >
#include <whatevermainwidget >

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  Whatevermainwidget *widge = new Whatevermainwidget(init_var, 0);
  QObject::connect(widge, SIGNAL(clicked()),
                   &app, SLOT(quit()));
  app.setMainWidget(widge);
  widge->show();
  return app.exec();
}
   

Globalization: put tr( ) around a string to mark it for translation.

Signals and Slots:

Use shift-Click to select more than one property to set to a common value.

An example using fortran subroutines

If linking to a fortran routine that takes a string as an argument:
tao_init( init_file )
Then in C++ you need something like:

extern "C" {
  void tao_init_(  const char*, int );
}
   

And in the calling routine:

char init_file[] = "tao.init";
int init_file_size = sizeof( init_file );
    
tao_init_( init_file, init_file_size );
   

In this preceding example tao_init does not modify the passed string, thus the const modifier. Also note that the extern declaration and the call from c++ have an underline appended to the fortran subroutine name.

Using an event filter to capture key presses:

It's possible to monitor events going to a particular QObject and handle them specifically. This following example captures keypresses and sends them to a separate subroutine.

bool Tao_Gui::eventFilter( QObject *target, QEvent *event )
{
    if (target == singleModeButton)
    {
	if (event->type() == QEvent::KeyPress)
	{
	    QKeyEvent *keyEvent = (QKeyEvent *)event;
	    if ( keyEvent->text() == "z" ) 
	    {
		singleModeButton->toggle();
		return true;
	    }
	    cmdSubmit( keyEvent->text() );
	    return true;
	}
    }
    return QMainWindow::eventFilter( target, event );
}
   

It's important to return true if you capture an event; this signals that you have handled the event. Otherwise, the event will pass through and get handled again by the default handler.

void Tao_Gui::startSingleModeFilter()
{
    singleModeButton->installEventFilter(this);
    mainStatusLabel->setText( "Now in Single-mode.");
}
   

The above subroutine implements the event filter. Similarly, the event filter can be stopped by removeEventFilter(this)

QT Designer's role in project building:

QT Designer reads and writes and creates .ui files and .ui.h files.

Ui files are created for each form and dialog in the project.

Ui.h files (using the extension approach, see designer approach) contain implementations of custom slots. These slots are user-created in qt designer. You should always add, delete or rename slots within Qt Designer, but you can add the implementation code with the editor of your choice. The ui.h files can also be used to customize the constructor or destructor of the form. This is done by adding slots called YourForm::init() or YourForm::destroy().

QT Designer also generates a .pro file which keeps track of all the forms and dialogs, as well as images and databases used in the project.

qmake takes the .pro file and generates a Makefile that will build the project.

When building the project, uic (user interface compiler) is used to generate header (.h) files and implementation (.cpp) files for all the forms in the project. The implementation files will "include" any appropriate ui.h files.

The main.cpp file only needs to include the header files generated by uic during the build process (i.e. form.h). It should not have to include .ui.h files.

Common Mistakes

Make sure the init() contructor function is lowercase.

Uncommon Mistakes

The configure command for installing qt is for some reason defaulting to an incorrect prefix directory. Need to ./configure -prefix CORRECT_DIR for plugins and assistant to work correctly. I'm actually using: ./configure -prefix CORRECT_DIR -qt-gif -continue to include gif support.


Mike Forster

Valid XHTML 1.0! Valid CSS!