Use Abstract Factory Pattern with Qt got a problem!
-
I use Abstract Factory Pattern in Qt. And when I run code I got this problem "The program has unexpectedly finished."
How I can fix it?
This is my code:
productfactory.h and productfactory.cpp // create abstract product
@#ifndef PRODUCTFACTORY_H
#define PRODUCTFACTORY_H#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QObject>
#include <QWidget>class ProductFactory : public QWidget
{
Q_OBJECTprotected:
QPushButton *exitButton;
QLabel *findwhatLabel;
QLineEdit *finwhatLineEdit;public:
ProductFactory(QWidget* parent = 0);
virtual void createUI();
};#endif // PRODUCTFACTORY_H
#include "productfactory.h"
ProductFactory::ProductFactory(QWidget* parent)
: QWidget(parent)
{
createUI();
}void ProductFactory::createUI()
{}
@abstractfactory.h and abstractfactory.cpp // create abstract factory
@
class AbstractFactory
{
public:
static AbstractFactory* createFactory(QString className);
virtual ProductFactory* getProduct() = 0;
virtual ~AbstractFactory();};
AbstractFactory* AbstractFactory::createFactory(QString className)
{
if(className == "FindFactory")
return new FindFactory();
if(className == "FindAndReplace")
return new FindAndReplaceFactory();
}AbstractFactory::~AbstractFactory()
{}
@
findproduct.h and findproduct.cpp // create create product's called FindProduct
@#ifndef FINDPRODUCT_H
#define FINDPRODUCT_H#include "productfactory.h"
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>class FindProduct : public ProductFactory
{
Q_OBJECT
public:
FindProduct(QWidget* parent = 0);
void createUI();private:
QPushButton *OK;};
@findfactory.h and findfactory.cpp // create factory of find product
@
#ifndef FINDFACTORY_H
#define FINDFACTORY_H
#include "abstractfactory.h"
#include "productfactory.h"
#include "findproduct.h"class FindFactory : public AbstractFactory
{
public:ProductFactory* getProduct();
};
#include "findfactory.h"
ProductFactory* FindFactory::getProduct()
{
return new FindProduct();
}
@findandreplaceproduct.h and findandreplaceprodut.cpp // create product FindAndReplace
@#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>class Find_Replace_Product : public ProductFactory
{
Q_OBJECT
private:
QPushButton *OK;
QLineEdit *rplByLineEdit;
QLabel *rplByLabel;public:
Find_Replace_Product(QWidget* parent = 0);
void createUI();};
#endif // FINDANDREPLACEPRODUCT_H
@findandreplacefactory.* create factory of findandreplace product
@
#ifndef FINDANDREPLACEFACTORY_H
#define FINDANDREPLACEFACTORY_H#include "abstractfactory.h"
#include "productfactory.h"
#include"findandreplaceproduct.h"class FindAndReplaceFactory : public AbstractFactory
{
public:
ProductFactory* getProduct();
};#endif // FINDANDREPLACEFACTORY_H
#include "findandreplacefactory.h"
ProductFactory* FindAndReplaceFactory::getProduct()
{
return new Find_Replace_Product();
}
@And I create MainWindow to use them like this:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QtGui/QMainWindow>
#include <QtGui/QGridLayout>
#include <QtGui/QWidget>
#include "abstractfactory.h"class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QGridLayout* gridLayout;public:
MainWindow(QWidget *parent = 0);
void createUI_MainWindow();
};#endif // MAINWINDOW_H
@
@#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createUI_MainWindow();
}void MainWindow::createUI_MainWindow()
{
gridLayout = new QGridLayout();
AbstractFactory* find = AbstractFactory::createFactory("FindFactory");
AbstractFactory* fRPL = AbstractFactory::createFactory("FindAndReplacer");
gridLayout->addWidget(find->getProduct());
gridLayout->addWidget(fRPL->getProduct(), 1, 0);setLayout(gridLayout);
}
@This is main.cpp
@
int main(int argc, char* argv[])
{
QApplication app(argc, argv);MainWindow mainWindow; mainWindow.show(); return app.exec();
}
@What's wrong with my code? Please help me and thanks for reading my question.
This is UML of it.
!http://i.upanh.com/vwvsfv(image)! -
!http://i.upanh.com/vwvsfv(image)!
![url=http://upanh.com/view/?id=5vw69v6sffv][img]http://i5.upanh.com/2013/0718/04//56850314.screenshotfrom20130717150650.png[/img]/url!This is UML image of it. Thank you.
-
[quote author="paulwarburg" date="1374121015"]And when I run code I got this problem "The program has unexpectedly finished."
How I can fix it?[/quote]That's a generic message that means "your program crashed". There can be many different causes
If you just post long, raw code like that, most people won't read it because it will take too long to find a problem. You need to provide more information, and/or reduce your code to a small example that still produces the crash.
What is the stack trace of your crash?
-
Your image link is broken
bq. How I can fix it?
You start by running it in your debugger, determining where it fails, and inspecting the culprit.
It is almost certainly the result of using an uninitialised, invalid or null pointer. If the program is failing in a deployed environment rather than the development environment then it is possibly a missing dependency. We could guess forever.
-
[quote author="ChrisW67" date="1374124313"]Your image link is broken
bq. How I can fix it?
You start by running it in your debugger, determining where it fails, and inspecting the culprit.
It is almost certainly the result of using an uninitialised, invalid or null pointer. If the program is failing in a deployed environment rather than the development environment then it is possibly a missing dependency. We could guess forever.[/quote]
[quote author="JKSH" date="1374124230"][quote author="paulwarburg" date="1374121015"]And when I run code I got this problem "The program has unexpectedly finished."
How I can fix it?[/quote]That's a generic message that means "your program crashed". There can be many different causes
If you just post long, raw code like that, most people won't read it because it will take too long to find a problem. You need to provide more information, and/or reduce your code to a small example that still produces the crash.
What is the stack trace of your crash?[/quote]
thank for your reply. But, if you have an example about Design Pattern With Qt like that, please share for me. Thanks. :)
-
The design pattern itself has nothing to do with the problem.
Qt probably has nothing to with it either.Your program is crashing for a reason but we cannot diagnose it, only you can.
-
Hint, you have a typo in a string
-
Thank you for your hint. It's my fault. I got it. But, if you have anything about Qt with Design Pattern, please share for me. Thank you very much.
[quote author="SGaist" date="1374146174"]Hint, you have a typo in a string[/quote] -
Search on i.e amazon for "design pattern qt4"