Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use Abstract Factory Pattern with Qt got a problem!

Use Abstract Factory Pattern with Qt got a problem!

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paulwarburg
    wrote on last edited by
    #1

    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_OBJECT

    protected:
    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&#40;&#41;;
    

    }
    @

    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)!

    1 Reply Last reply
    0
    • P Offline
      P Offline
      paulwarburg
      wrote on last edited by
      #2

      !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.

      1 Reply Last reply
      0
      • JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        [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?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            paulwarburg
            wrote on last edited by
            #5

            [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. :)

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hint, you have a typo in a string

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  paulwarburg
                  wrote on last edited by
                  #8

                  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]

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Search on i.e amazon for "design pattern qt4"

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved