Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Create class



  • Hi

    i'm a begginer and i want to learn how to create my own class and use it in another project later.
    i did like this and it didn't work i create new project in which i made my new class for exemple "mywindow" ,i compile it and it works. now i open another project and i call it #include<mywindow> i got an error "#include<mywindow> no such file or directory "



  • The old class has to be in the same folder, if it's not there, you'll have to add the path in the .pro file.
    I hope it helps.



  • Hi all what i can advice to you for now is start learn C++ language and Qt.
    For C++ language:
    "http://www.cplusplus.com/doc/tutorial/":http://www.cplusplus.com/doc/tutorial/
    or many other free resources(use google)
    for Qt:
    C++ GUI Programming with Qt 4 – By Jasmin Blanchette, Mark Summerfield
    "doc":http://qt-project.org/doc/[qt-project.org]
    best one and free videos: "http://voidrealms.com/tutorials.aspx?filter=qt":http://voidrealms.com/tutorials.aspx?filter=qt



  • just tell me about the syntax of the path for ex
    i have folder in desktop called my project in which i have 2 project one is my class "mywindow" the second is the new project
    so if i follow u i should write the path in .pro file but how



  • qxoz i'm following a tuto of c++ and Qt
    look what he did
    First he made a project FenPrincipale
    header
    @
    #ifndef FENPRINCIPALE_H
    #define FENPRINCIPALE_H
    #include <QtGui>
    #include <QMainWindow>
    class FenPrincipale : public QMainWindow
    {
    public:
    FenPrincipale();
    private:
    };

    #endif // FENPRINCIPALE_H

    main
    #include <QApplication>
    #include <QtGui>
    #include <QMainWindow>
    #include "FenPrincipale.h"
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    FenPrincipale fenetre;
    fenetre.show();
    return app.exec();
    }
    @
    .cpp
    @
    #include "FenPrincipale.h"
    FenPrincipale::FenPrincipale()
    {
    }
    @
    it works fine for me, but later in the tuto he make a second project
    with the following main
    @
    #include <QApplication>
    #include <QtGui>
    #include "FenPrincipale.h"
    FenPrincipale::FenPrincipale()
    {
    QVBoxLayout *layout = new QVBoxLayout;
    QDirModel *modele = new QDirModel;
    QTreeView *vue = new QTreeView;
    vue->setModel(modele);
    layout->addWidget(vue);
    setLayout(layout);
    }
    @

    i made new project and paste FenPrincipale.h in the same folder with the new project but this is what i got
    @
    C:\Qt\Qt5.0.1\5.0.1\mingw47_32\lib\libqtmaind.a(qtmain_win.o):-1: In function `WinMain@16':

    Q:\qt5_workdir\w\s\qtbase\src\winmain\qtmain_win.cpp:131: erreur : undefined reference to `qMain(int, char**)'

    collect2.exe:-1: erreur : error: ld returned 1 exit status
    @

    Edit: please spend a little effort to make your post readable. Using @ tags around code sections already helps a lot, but properly indented code would be even better; Andre



  • Can you give a link to that tutorial or post your project on some file hosting? So i can understand what you doing wrong and help.
    and please edit your previos post use @ tag for code.



  • it's long 600 pages u need to follow it from begin i don't think it's a good idea but u can help me by explaining step by step how to create a class and call it in another project i would appreciate it



  • Sure. Lets see your code. You say
    [quote author="nymar" date="1361958556"]
    it works fine for me, but later in the tuto he make a second project
    with the following main [/quote]

    Try change your class as:
    .h
    @#ifndef FENPRINCIPALE_H
    #define FENPRINCIPALE_H
    #include <QtGui>
    #include <QMainWindow>

    class FenPrincipale : public QMainWindow
    {
    Q_OBJECT
    public:
    explicit FenPrincipale(QWidget *parent = 0);
    FenPrincipale();
    private:
    QVBoxLayout *layout;
    QDirModel *modele;
    QTreeView *vue;
    };

    #endif // FENPRINCIPALE_H@
    .cpp
    @FenPrincipale::FenPrincipale(QWidget *parent) :
    QMainWindow(parent)
    {
    layout = new QVBoxLayout;
    modele = new QDirModel;
    vue = new QTreeView;
    vue->setModel(modele);
    layout->addWidget(vue);
    setLayout(layout);
    }@

    leave main() as in first project
    @#include <QApplication>
    #include <QtGui>
    #include <QMainWindow>
    #include "FenPrincipale.h"
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    FenPrincipale fenetre;
    fenetre.show();
    return app.exec();
    }@

    Try this and tell me what you got.



  • i'm slow i know it but i have 2 projects now
    1-FenPrincipale contains (FenPrincipale.h +main.cpp+ FenPrincipale.cpp)

    2-test contains (main.cpp + FenPrincipale.h (which i imported from the first project))
    and if i'm not wrong ur making a new project in which u define a new class and u use it inside the same project isn't it ?



  • Well you can also use existing class in many projects. It's doesn't matter you create or include new class.


Log in to reply