Wednesday, April 15, 2009

Refactoring QtRuby Example #2

(continued from phosphorescence: Refactoring QtRuby Example #1)

In Qt application using UI file, there are 3 approaches.
  1. The Direct Approach
  2. The Single Inheritance Approach
  3. The Multiple Inheritance Approach
See more at Using a Designer .ui File in Your Application.

I planned to adopt The Single Inheritance Approach for two reasons. First, Ruby Language doesn't support multiple inheritance. Second, project creation wizard of Qt Creator suggests The Single Inheritance Approach like below:

samplewidget.h
#ifndef SAMPLEWIDGET_H
#define SAMPLEWIDGET_H

#include <QtGui/QWidget>

namespace Ui
{
class SampleWidgetClass; //UI form class
}

class SampleWidget : public QWidget //top widget class
{
Q_OBJECT

public:
SampleWidget(QWidget *parent = 0);
~SampleWidget();

private:
Ui::SampleWidgetClass *ui; //instantiate UI form class
};

#endif // SAMPLEWIDGET_H

samplewidget.cpp
#include "samplewidget.h"
#include "ui_samplewidget.h"

SampleWidget::SampleWidget(QWidget *parent) //instantiate top widget class
: QWidget(parent), ui(new Ui::SampleWidgetClass)
{
ui->setupUi(this);
}

SampleWidget::~SampleWidget()
{
delete ui;
}

main.cpp
#include <QtGui/QApplication>
#include "samplewidget.h"

int main(int argc, char *argv[]) //entry point
{
QApplication a(argc, argv);
SampleWidget w;
w.show();
return a.exec();
}

Then I wrote Ruby scripts corrsponding to Qt's UI form class, top widget class and entry point.

No comments:

Post a Comment