Montag, 29. Oktober 2012

Notes from Qt's Calculator Example

Glossary

SLOT(...)

Used to convert the argument, which is a member function, to a const char *

Q_OBJECT

Macro used at the beginning of a class definition that inherits from a Q... class.

QFont

pointSize(void) : returns int
setPointSize(int i)

QGridLayout

Layout which provides a tabular (grid) spacing of widgets

addWidget(MyWidgetObject obj, row, col, rowSpan=1, colSpan=1);

Adds a widget (e.g. a QEditLine) to the grid layout

setSizeConstraint(QLayout::SetFixedSize);

QLineEdit

Provides a single-line edit control

font(void) : returns QFont

setAlignment(Qt::AlignRight)

setFont(QFont qf)

setMaxLength(int i)

setReadOnly(bool b)

setText(string s);


QString

General replacement for the C++ String class

QString::number(int i);

Seems to convert a number i into a string

QToolButton

Provides a pushable button

setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred)

Changes the way the object resizes so that it expands automatically

setText(string s);

QSize sizeHint() declaration

Declared as a member of a QToolButton subclass to control the way it sizes itself

QWidget

Provides a generic Qt GUI Element. Most custom elements will be subclasses of this

private slots:

Used in the declaration of the custom GUI class to declare member functions which will receive events like mouse clicks

sender();  

Member function inherited from QObject which identifies the object that generated an event like a mouse click

setLayout(QGridLayout qgl);

Member function inherited from QWidget which sets the layout of the GUI element.

setWindowTitle(string s);

Member function inherited from QWidget which sets the window title of the GUI element.

tr(string s)   

Macro or helper function which seems to localize strings

qobject_cast<Button *>(sender());

Similar to C++'s static_cast, but which is guaranteed to return NULL if sender() is not the correct type

qMax(X, Y)   

Macro or helper function which returns the max of X and Y

connect(button, SIGNAL(clicked()), this, member);

Inherited from QWidget?

clicked()

Inherited from QWidget?


Declaring a class for your GUI Element

A GUI Element in Qt is declared as a subclass of QWidget, and it has a constructor which looks like this: 
Foo::Foo(QWidget *parent = NULL);

An example constructor should call the superconstructor's constructor and might be defined like this:
Foo::Foo(QWidget *parent)
    : QWidget(parent)
{
    fooPrivateVar1 = 0;
    fooPrivateVar2 = 0;
    SomeLayoutType *myLayout = new SomeLayoutType;
    setLayout(myLayout);
    setWindowTitle(tr("Foo Application"));
}

Creating a project for your GUI Program

1. Create a .pro file (Qt project file), which is a configuration file used by qmake.exe that writes out a make-compatible Makefile.
2. The Qt project file declares (usually) one or more HEADERS and one or more SOURCES, one of which is called main.cpp and presumably contains the main() method.

Main method

The main() method used in Qt projects seems idiomatic and short, and may look something like this:

#include <QApplication>
#include "myapplication.h"
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MyApplicationClass foo;
    foo.show();  // Method which is inherited by QWidget, which shows the GUI Element
    return app.exec();
}

Converting Qt Designer's UI files into H files

After creating a .ui file myform.ui in Qt Designer, use the following command to convert it into a header file:

uic -o myform.h myform.ui

Keine Kommentare:

Kommentar veröffentlichen